pino-rotating-file icon indicating copy to clipboard operation
pino-rotating-file copied to clipboard

Facing issue while using `rotate-logs` with `pino-pretty`

Open vipulwairagade opened this issue 3 years ago • 0 comments

Hi, I am not able to figure how to use pino-pretty so that I can save my log to a file as well as use teeToStdout option to get that on console in a prettified way when I am in development environment.

Here is my rotate-log config :

module.exports = {
  filter(data) {
    // logic can be added here to determine if the data should be logged
    // refer to https://www.npmjs.com/package/@vrbo/pino-rotating-file

    // Do not log if data do not contain http request
    return !data.req;
  },
  output: {
    path: "app.log", // name of file
    options: {
      path: `./logs/${dest}/app`, // path to write files to
      size: "2M", // max file size
      interval: "1d", // rotate daily
      teeToStdout: process.env.NODE_ENV !== "production" // writes file content to stdout as well.
    }
  }
};

Here is my logger config :

const level = envConfig.ENV === ENVIRONMENTS.PRODUCTION ? LOG_LEVELS.INFO : LOG_LEVELS.DEBUG;
const prettyPrint = {
  colorize: "true", // --colorize: add terminal color escape sequence to the output
  levelFirst: true, // --levelFirst: display the log level name before the logged date and time
  translateTime: "SYS:standard", // --translateTime: translate the epoch time to local system's TZ, in human readable format
  ignore: "pid,hostname,module", // --ignore: ignore one or several keys
  singleLine: true, // --singleLine: print each log message on a single line
  messageFormat: "({module}) {msg}" // --messageFormat: format outpout for the message portion of the log
};

export const logger = pino(
  {
    name: "server",
    level: level || LOG_LEVELS.DEBUG,
    formatters: {
      level(label) {
        return { level: label };
      }
    },
    prettyPrint: envConfig.ENV === ENVIRONMENTS.PRODUCTION ? false : prettyPrint

  }
);

This command is being used in pipe to server start command in npm script : cross-env-shell NODE_ENV=development rotate-logs --config=$INIT_CWD/configs/app-log.config.js

What I am looking for is use this type of configuration but using this package :

import pino from "pino";
import * as rfs from "rotating-file-stream";

const fileStream = rfs.createStream("file.log", {
  path: "./logs/",
  size: "100M",
  interval: "1d",
  maxFiles: 30,
  teeToStdout: true,
});

export const logger = pino(
  {
    base: null,
    prettyPrint: {
      colorize: false,
      translateTime: true,
    },
  },
  fileStream
);

I have tried to implement this myself, but somehow I am not able to figure out. Can you please provide some inputs or help me with it.

Thanks in advance.

vipulwairagade avatar Jul 11 '21 19:07 vipulwairagade