[3.0.0-rc5]: Cannot colorize JSON logs
See original GitHub issuePlease 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:
- Created 5 years ago
- Comments:12 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Something should probably be added to the docs explaining that this is the intended behavior, given that the example given in the readme is:
@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.
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.\u001b[32m
instead of\\\u001b[32m
.There may be a way to get this working with a custom
replacer
passed toJSON.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:
Which when executed outputs: