Search code examples
node.jsloggingwinston

Winston - Timestamp not added


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?


Solution

  • 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.