Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

winston 3: colorize all including timestamp #1388

Closed
mbana opened this issue Jul 2, 2018 · 3 comments
Closed

winston 3: colorize all including timestamp #1388

mbana opened this issue Jul 2, 2018 · 3 comments

Comments

@mbana
Copy link

mbana commented Jul 2, 2018

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.

@DABH
Copy link
Contributor

DABH commented Jul 4, 2018

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!

@DABH DABH closed this as completed Aug 27, 2018
@tommuhm
Copy link

tommuhm commented Oct 25, 2018

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(),
  ]

});

@ryancwalsh
Copy link

I have a related question here: https://stackoverflow.com/questions/74186705/how-to-preserve-default-syntax-highlighting-colors-in-javascript-console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants