winston 3: colorize all including timestamp

Created on 2 Jul 2018  路  2Comments  路  Source: winstonjs/winston

I can't find an example of colorizing the entire log line, e.g.,

winston.createLogger({
  level: 'debug',
  transports: [
    new winston.transports.Console({
      format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.colorize({ all: true }),
        winston.format.align(),
        winston.format.printf((info) => {
          const {
            timestamp, level, message, ...args
          } = info;

          // const ts = timestamp.slice(0, 19).replace('T', ' ');
          return `${timestamp} ${level}: ${message} ${Object.keys(args).length ? JSON.stringify(args, null, 2) : ''}`;
        }),
      ),
    }),
  ],
});

I would like everything in the entire line colorized. In Winston 2, it seemed to work fine expect it didn't colorize the entire line.

How do I colorize the entire output line and include the timestamp as the first entry of the log line, not as the part of message body.

Most helpful comment

a quick workaround to colorize the whole log line is:

const colorizer = winston.format.colorize();

const logger = winston.createLogger({
  level: 'debug',
  format: combine(
    winston.format.timestamp(),
    winston.format.simple(),
    winston.format.printf(msg => 
      colorizer.colorize(msg.level, `${msg.timestamp} - ${msg.level}: ${msg.message}`)
    )
  ),
  transports: [
    new transports.Console(),
  ]

});

All 2 comments

The issue is colorize will only colorize the info object's level or message properties: https://github.com/winstonjs/logform/blob/master/colorize.js#L92 , even if opts.all is set. Tbh I think that makes opts.all a little useless as-is; you could just set opts.level and opts.message to get the current functionality. opts.all could instead e.g. colorize every key in the info object, which would include tiemstamp (the other point is that timestamp is added as a separate key in info, not prepended to info.message, so it doesn't get colorized). If you'd like to open a PR to logform that makes colorize color all the keys of info when opts.all is true, I'd be happy to consider it there. Thanks!

a quick workaround to colorize the whole log line is:

const colorizer = winston.format.colorize();

const logger = winston.createLogger({
  level: 'debug',
  format: combine(
    winston.format.timestamp(),
    winston.format.simple(),
    winston.format.printf(msg => 
      colorizer.colorize(msg.level, `${msg.timestamp} - ${msg.level}: ${msg.message}`)
    )
  ),
  transports: [
    new transports.Console(),
  ]

});
Was this page helpful?
0 / 5 - 0 ratings