Node.js – How to add timestamp to logs using Winston library?

Above answers did not work for me. In case you are trying to add timestamp to your logs using the latest version of Winston – 3.0.0-rc1, this worked like charm:

    const {transports, createLogger, format} = require('winston');

    const logger = createLogger({
        format: format.combine(
        transports: [
            new transports.Console(),
            new transports.File({filename: 'logs/error/error.log', level: 'error'}),
            new transports.File({filename: 'logs/activity/activity.log', level:'info'})

I used ‘format.combine()’. Since I needed timestamp on all my transports, I added the formatting option within the createLogger, rather than inside each transport. My output on console and on file (activity.log) are as follows:

{"message":"Connected to mongodb","level":"info","timestamp":"2018-02-01T22:35:27.758Z"}
{"message":"Connected to mongodb","level":"info","timestamp":"2018-02-01T22:35:27.758Z"}

We can add formatting to this timestamp in ‘format.combine()’ as usual using:


Leave a Comment