Winston: Colorize with uppercase level creates lots of blank lines before log message

Created on 31 May 2018  路  2Comments  路  Source: winstonjs/winston

Please tell us about your environment:

  • winston version?_
  • node -v outputs: v10.1.0
  • Operating System: Linux (Official Docker image)
  • Language: TypeScript 2.6.2

What is the problem?

Using a custom format that uppercases the level with colorize will cause the log line output to have lots of empty space before it (clears my console so probably 50+ empty lines).

What do you expect to happen instead?

Ideally the upper cased level would be colorized. Worst case is it would not be colorized.

Other information

Here is the code:

const custom = format.printf((info) => {
  return `${info.level.toUpperCase()}: ${info.message}`;
});

const log = createLogger({
  format: format.combine(
    format.colorize(),
    custom
  ),
  transports: [new transports.Console()]
});

It works fine if I do .toLowerCase()

feature request

Most helpful comment

@kingjerod this should be fixed by https://github.com/winstonjs/logform/pull/34. Once that lands you can solve this by:

const custom = format.printf((info) => {
  return `${info.level}: ${info.message}`;
});

const log = createLogger({
  format: format.combine(
    format(info => {
      info.level = info.level.toUpperCase()
      return info;
    })(),
    format.colorize(),
    custom
  ),
  transports: [new transports.Console()]
});

All 2 comments

info.level is mutable. It is changed from 'error' or 'info' to a string that includes ANSI color codes. e.g.:

// level = '\u001b[32minfo\u001b[39m'
info: wut
// level = '\u001b[32minfo\u001b[39m'
info: ok
// level = '\u001b[31merror\u001b[39m'
error: sure

... not 100% the right approach to add this feature. The underlying colorize code is in logform

@kingjerod this should be fixed by https://github.com/winstonjs/logform/pull/34. Once that lands you can solve this by:

const custom = format.printf((info) => {
  return `${info.level}: ${info.message}`;
});

const log = createLogger({
  format: format.combine(
    format(info => {
      info.level = info.level.toUpperCase()
      return info;
    })(),
    format.colorize(),
    custom
  ),
  transports: [new transports.Console()]
});
Was this page helpful?
0 / 5 - 0 ratings