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.
I want the whole output of the messages from the winston logger to appear in color, not just the label and the message, but the timestamp as well. I know that, in Winston 2.x that was in some ways possible (don't know how though).
I have already tried different NPM Packages like winston color and winston-console-formatter, but they don't seem to work.
I have defined my logger as follows:
import winston from 'winston'
let alignColorsAndTime = winston.format.combine(
winston.format.colorize({
all:true
}),
winston.format.label({
label:'[LOGGER]'
}),
winston.format.timestamp({
format:"YY-MM-DD HH:MM:SS"
}),
winston.format.printf(
info => ` ${info.label} ${info.timestamp} ${info.level} : ${info.message}`
)
);
export const logger = winston.createLogger({
level: "debug",
transports: [
new (winston.transports.Console)({
format: alignColorsAndTime
})
],
});
Try with the code below. It will combine both formatting. It works for me.
export const logger = winston.createLogger({
level: "debug",
transports: [
new (winston.transports.Console)({
format: winston.format.combine(winston.format.colorize(), alignColorsAndTime)
})
],
});
Note the padding has to handle the colour. Ex:
const padding= info.level.length <= 7?7:17; //padding differently if it has colour.
${info.level.padEnd(padding,' ')}
Tested with:
"winston": "^3.1.0"