1
votes

I'm using Winston as a logger for a NodeJS project. I have tried unsuccessfully to add timestamp to the log messages and configure Winston to write console log messages in a non Json fashion. My configuration is as below

const appRoot = require('app-root-path');
const winston = require('winston');

const options = {
    file: {
        level: 'info',
        filename: `${appRoot}/logs/app.log`,
        timestamp: true,
        handleExceptions: true,
        json: true,
        maxsize: 5242880, // 5MB
        maxFiles: 15,
        colorize: false,
    },
    console: {
        level: 'debug',
        timestamp: true,
        handleExceptions: true,
        json: false,
        colorize: true,
    },
};

const logger = winston.createLogger({
    transports: [
        new winston.transports.File(options.file),
        new winston.transports.Console(options.console)
    ],
    exitOnError: false,
});

module.exports = logger;

And this is the way I import Winston in other files (The winston configuration file is at the root of my project):

const winston = require('../winston');

Any idea on why it's working?

3

3 Answers

5
votes

I've had this error with winston 3. Have a look at the doc in this section that specify the format of the logger. This will solve the issue.

1
votes

Here is a sample to help printing the timestamp on the output (log file, console).

Versions used for this example:

├── [email protected] ├── [email protected] └── [email protected]

// Declare winston
const winston = require("winston");
// Import all needed using Object Destructuring
const { createLogger, format, transports } = require("winston");
const { combine, timestamp, printf } = format;
// Export the module
module.exports = function (err, req, res, next) {


  const logger = createLogger({
    level: "error",
    format: combine(
      format.errors({ stack: true }), // log the full stack
      timestamp(), // get the time stamp part of the full log message
      printf(({ level, message, timestamp, stack }) => { // formating the log outcome to show/store 
        return `${timestamp} ${level}: ${message} - ${stack}`;
      })
    ),
    transports: [
      new transports.Console(), // show the full stack error on the console
      new winston.transports.File({ // log full stack error on the file
        filename: "logfile.log",
        format: format.combine(
          format.colorize({
            all: false,
          })
        ),
      }),
    ],
  });

  logger.log({
    level: "error",
    message: err,
  });
  // Response sent to client but nothing related to winston
  res.status(500).json(err.message);
};
1
votes

Here is a working winston logger module with timestamp:

const { createLogger, format, transports } = require("winston");
const { combine, timestamp, label, printf } = format;
const appRoot = require("app-root-path");

const myFormat = printf(({ level, message, label, timestamp }) => {
    return `${timestamp} ${level}: ${message}`;
});

const options = {
    file: {
        level: "info",
        filename: `${appRoot}/logs/app.log`,
        handleExceptions: true,
        json: true,
        maxsize: 5242880, // 5MB
        maxFiles: 5,
        colorize: false,
        timestamp: true,
    },
    console: {
        level: "debug",
        handleExceptions: true,
        json: false,
        colorize: true,
    },
};
const logger = createLogger({
    format: combine(label({ label: this.level }), timestamp(), myFormat),
    transports: [new transports.Console(options.console), new transports.File(options.file)],
});

module.exports = logger;