winston version?_winston@2winston@3 node -v outputs:_ v8.11.1When I add colorize(), my .log file contains [32m and [39m.
Why is it happening?
This is my code:
'use strict';
const { format, createLogger, transports } = require('winston');
const logger = createLogger({
format: format.combine(
format.colorize({ all: true }),
format.simple()
),
transports: [
new transports.File({ filename: 'combined.log' }),
new transports.Console({ handleExceptions: true })
]
});
logger.info('Hello there. How are you?');
logger.info('Hello again distributed logs', {
pippo: {
pluto: 'cio',
ooo: 2.4,
sasas: [1, 2, 3],
ole: {
prova: true
}
}
});
logger.warn('some foobar level-ed message');
logger.error('some baz level-ed message');
logger.silly('some bar level-ed message');
This is the result log:
[32minfo[39m: [32mHello there. How are you?[39m
[32minfo[39m: [32mHello again distributed logs[39m {"pippo":{"pluto":"cio","ooo":2.4,"sasas":[1,2,3],"ole":{"prova":true}}}
[33mwarn[39m: [33msome foobar level-ed message[39m
[31merror[39m: [31msome baz level-ed message[39m
Removing this line from my code: format.colorize({ all: true })
My log file become correct:
info: Hello there. How are you?
info: Hello again distributed logs {"pippo":{"pluto":"cio","ooo":2.4,"sasas":[1,2,3],"ole":{"prova":true}}}
warn: some foobar level-ed message
error: some baz level-ed message
Log file shouldn't contains [32m and [39m
Strange! I think those chars are supposed to be what set the colors, not quotes. Do you know if you've done anything special regarding your terminal environment (is this stock os x terminal, some IDE, etc.), any custom settings? Could be an issue with colorize, not totally sure at first glance.
I think those chars are supposed to be what set the colors, not quotes.
Yes, it's possible.
Nothing special about my terminal. I'm using iterm2, but the same issue happens with the stock terminal of macos.
My terminal configuration is created with this repo, but I don't see anything strange: https://github.com/Ks89/Dotfiles_macOS
Fixed using colorize ONLY inside transports.Console, instead of globally. I don't know why but it's working.
...
if (process.env.NODE_ENV !== 'production') {
logger.add(new transports.Console({
...
format: format.combine(
format.colorize({
all: true
}),
...
),
}));
}
Hello @Ks89 ,
I don't know where colorize is so that I can remove it, here is my code:
new winston.transports.File({
format: winston.format.combine(
winston.format.colorize({ // I added this but it's still not helping
all: false,
message: false,
level: false,
}),
winston.format.label({ label: 'API' }),
winston.format.timestamp(),
winston.format.printf(({ level, message, label, timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`;
}),
),
filename: environment.logDirectory,
level: 'http',
maxsize: 1024 * 1024 * 10,
}),
In main.ts, I have
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
In AppModule.ts, I have the following:
import { WinstonModule } from 'nest-winston';
...
WinstonModule.forRoot({
transports,
}),
I can't find anywhere else that uses colorize() and I don't know how can I disable it.
Most helpful comment
Fixed using colorize ONLY inside transports.Console, instead of globally. I don't know why but it's working.