33

I am using winston to add log details in node.js, i used the following procedure to add the logs

 var winston = require('winston');         
 winston.remove(winston.transports.Console);
 winston.add(winston.transports.Console, {'timestamp':true,'colorize':true);
 winston.log('info','jjjj');

the output that i got is

2012-12-21T09:32:05.428Z - info: jjjj

I need to specify a format for mytimestamp , is there any provision to do so in winston any help will be much appreciated

Amanda G
  • 1,931
  • 10
  • 33
  • 43

4 Answers4

50

The timestamp option can be a function that returns what you wish it to be saved as...

Line 4:

winston.add(winston.transports.Console, {'timestamp':function() {return '111111111'; },'colorize':true});

Source here: https://github.com/flatiron/winston/pull/120

pixelpax
  • 1,435
  • 13
  • 22
Ben Evans
  • 1,567
  • 16
  • 15
  • 175
    Do you realise you answered this question on the 21/12/12 at 12:21?! And it was a question about date formatting no less :) – Alex MDC Aug 07 '13 at 20:32
  • @yuyue007 seems to work for File transport in version 0.9.0 of winston. – Default Feb 18 '15 at 22:03
29

winston@3 version

winston.createLogger({
  format: winston.format.combine(
    winston.format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
    winston.format.prettyPrint()
  ),
  transports: [
    new winston.transports.Console()
  ]
})

To support timezone, you need to change format to a function which winston will call.

const timezoned = () => {
  return new Date().toLocaleString('en-US', {
    timeZone: 'Asia/Shanghai'
  });
};

const logger = createLogger({
  format: combine(
    timestamp({
      format: timezonedTime
    })
  ),
  transport: [
    new transports.Console(),
  ]
});
Chris Peng
  • 896
  • 1
  • 10
  • 23
  • 1
    How to change the timezone. Winston by default displays the timestamp in UTC format? – Sagar Khan Jul 16 '19 at 08:24
  • I've updated the answer. Here is an old post I wrote when I was learning winston. https://medium.com/@ThreePotatoteers/winston-3-customize-timestamp-format-f510ce03b33d – Chris Peng Jul 16 '19 at 10:09
3

For a good result, you may use momentjs:

const moment = require('moment');
...
...
timestamp: () => moment().format('YYYY-MM-DD hh:mm:ss')
mb870977
  • 51
  • 8
Burhan Mubarok
  • 358
  • 2
  • 8
3

To change the timestamp format in winston log format,we can pass either a string or a function that return a string as a argument to the winston.format.timestamp(format) function which takes format parameter.In addition,we can use combine function to customize the log format

 const LOG_FORMAT = WINSTON.format.combine(
    WINSTON.format.align(),
    WINSTON.format.timestamp({format:'DD-MM-YYYY T hh:mm:ss.sss A'}),
    
    WINSTON.format.printf(({ level, message, timestamp, label }) => {
        return `[ ${level.toUpperCase()} | ${timestamp} | LOG:${message} ]`;
    })
)
    const APP_LOGGER = WINSTON.createLogger({
    format: LOG_FORMAT,
    transports: [
        new WINSTON.transports.Console(),
        new WINSTON.transports.File({
            filename: './logs/app.log'
        })
    ]
})
mohabbati
  • 1,162
  • 1
  • 13
  • 31
Mohamed MS
  • 51
  • 3