question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[3.0.0-rc5]: Cannot colorize JSON logs

See original GitHub issue

Please tell us about your environment:

  • winston version?
    • winston@3
  • node -v outputs: v8.10.0
  • Operating System? macOS
  • Language? all

What is the problem?

I am trying to write colorized JSON logs using Winston 3.x. Following the documentation, I initialized the logger like this:

module.exports = winston.createLogger({
  format: winston.format.combine(
    winston.format.colorize(),
    winston.format.json()
  ),
  transports: [new winston.transports.Console()]
})

However, when using this logger, I get encoded color codes instead of colors:

{
  "message": "An example message",
  "level": "\u001b[32minfo\u001b[39m"
}

What do you expect to happen instead?

I would have expected the log output to actually be colored.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

29reactions
pdiniz13commented, Aug 16, 2018

Something should probably be added to the docs explaining that this is the intended behavior, given that the example given in the readme is:

winston.format.combine(
  winston.format.colorize(),
  winston.format.json()
);
7reactions
indexzerocommented, Jun 5, 2018

@denisw I can see how this might be confusing, but it’s quite logical when one examines how the different formats & specifications are fighting against each other.

  1. The behavior of JSON.stringify is well documented – strings must be enclosed in quotes and are escaped. Even using a custom replacer parameter I was unable to work around the string escaping for \\u chars.
  2. ANSI color codes when escaped look weird – but we don’t see it because the escaping is unwrapped when written to the console – e.g. \u001b[32m instead of \\\u001b[32m.

There may be a way to get this working with a custom replacer passed to JSON.stringify, but I couldn’t get it wired up.

Instead, if we think about the desired output as what I believe it is – simply not JSON. It is JSON-like, but it’s not JSON since the strings are unescaped to allow colors to pass through. There are a lot of ways to shave the JSON-like yak, but I quickly hacked this one together to illustrate it’s possible:

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

const logger = createLogger({
  level: 'silly',
  format: format.combine(
    format.colorize(),
    format.printf(info => {
      return Object.keys(info).reverse().reduce((acc, key, i) => {
        if (typeof key === 'string') {
          if (i > 0) acc += ", "
          acc += `"${key}": "${info[key]}"`
        }

        return acc;
      }, '{ ') + ' }';
    })
  ),
  transports: [new transports.Console()]
});

logger.info('hey colors!');
logger.error('oh and this one is red');
logger.silly('magenta maybe?');

Which when executed outputs:

screen shot 2018-06-04 at 10 49 50 pm
Read more comments on GitHub >

github_iconTop Results From Across the Web

Winston 3.0 colorize whole output on console - Stack Overflow
I am developing a Node.js application, using babel-cli as an ES6 transpiler and I am using Winston 3.0 as my logging service. Question:...
Read more >
Can't update to 3.1.x - Craft CMS Stack Exchange
In my case, the package itself doesn't appear in my composer.json . It does appear in my composer.lock file thought.
Read more >
npm fails to install - Google Groups
The log file generated is as follows: 0 info it worked if it ends with ok. 1 verbose cli [ 'node', '/usr/local/bin/npm', 'install',...
Read more >
logform.colorize JavaScript and Node.js code examples
Best JavaScript code snippets using logform.colorize(Showing top 15 results out of 315) · test/tools/logger.js/winston. · configs/logger.js/createLogger · helpers/ ...
Read more >
@pinojs/json-colorizer - npm
Latest version: 3.0.0, last published: 10 months ago. ... const colorize = require('json-colorizer') console.log(colorize({ foo: 'bar' })).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found