winston: Console logging is not colorized.
I’m trying to colorize the console output and it’s not working. The output is all the same (white) color. I’m on 3.0.0-rc1.
const winston = require('winston');
const logLevels = {
levels: {
error: 0,
warn: 1,
info: 2,
http: 3,
sql: 4,
debug: 5
},
colors: {
error: "red",
warn: "darkred",
info: "black",
http: "green",
sql: "blue",
debug: "gray"
}
};
winston.addColors(logLevels);
const logger = winston.createLogger({...});
logger.add(new winston.transports.Console({colorize: true}));
I also tried the suggestion here but that’s not working either:
logger.add(new winston.transports.Console({format: winston.format.combine(formatter, winston.format.colorize())}));
Thanks, Alvaro
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 3
- Comments: 24 (1 by maintainers)
Commits related to this issue
- Document how to colorize levels * closes #1135 — committed to morenoh149/winston by morenoh149 9 years ago
- Document how to colorize levels (#1258) * closes #1135 — committed to winstonjs/winston by morenoh149 9 years ago
- Document how to colorize levels (#1258) * closes #1135 — committed to Mizumaki/winston by morenoh149 9 years ago
I was able to get it partially working using the documentation for logform.
Not sure how to handle dumping the JSON arguments.
Update here is what I worked out:
started winston and also got console without colors… i was using next transport defining code:
after reading this thread i did like this:
and got no difference. so i changed position of colorize format to 1st and got colors working
In reading the code you can also send
allto thecolorizeas an option to colorize the entire log.@tommuhm I see the defect now. There isn’t a built-in format option for
colorizeif one does indeed want to colorize the entireprintf. Does this make sense to you?The tricky bit is having the
colorizerknown when aninfohas been “serialized” (i.e. wheninfo[MESSAGE]has been set to a string). Follow-up PR tologformshortly – thanks for digging through it!format.colorize({all:true}does not really work if timestamp is included, here is a working example to colorize the whole line:Thx @Xeoncross. Take a look at: https://github.com/winstonjs/logform/blob/master/timestamp.js. You can format timestamp with
formatopt param. But be careful with timezone@Xeoncross we can also achieve the same in a much simpler way
reference: https://www.npmjs.com/package/winston#using-custom-logging-levels
check my full featured logger
Reading this in 2023?
The solution here didn’t work for me. This is how I could get it to work.
@markhealey I already removed winston dependency from my package.json and then I read your message. Thank you 👍
I love the new flexibility, but I hope it gets better documentation. I just spent too long getting this to work. A plug-n-play example for those of us who were using
winston.cli()would go a long way, something similar to @Xeoncross’s example.I created a gist that shows how to use winston & morgan logging in a simple expressjs app.
I have a very customized logger and placing the colorized statement at the end of the format was what worked for me. Thank u.
That did the trick for me in iTerm on MacOS
I’m having the same issue as well. Did you manage to fix it?
i am using winston for logging in cloudwatch and trying to add color but instead of color symbols appear… I try all solutions mention above. below is my code
and in cloudwatch console output is
this [39m apear instead of color. Any suggestion?
Thanks @Xeoncross! Adding winston.format.colorize() to my format worked. Interesting that this isn’t in the docs. Or at least I couldn’t find it in the docs.