Winston 3.0 colorize whole output on console
simply use addColors()
method
const { createLogger, format, transports, addColors } = require('winston');
const { combine, colorize, label, timestamp, json, prettyPrint, printf } = format;
require('winston-mongodb');
let myCustomFormat = format.combine(
colorize({ all: true }),
label({ label: '[LOGGER]' }),
timestamp({ format: 'YY-MM-DD HH:MM:SS' }),
printf(
(info) =>
` ${info.label} ${info.timestamp} ${info.level} : ${info.message}`
)
);
addColors({
info: 'bold blue', // fontStyle color
warn: 'italic yellow',
error: 'bold red',
debug: 'green',
});
const logger = createLogger({
level: 'info',
transports: [new transports.Console({ format: combine(myCustomFormat) })],
});
This worked for me for customizing colors and font style.
Possible options from the doc are below.
Font styles: bold, dim, italic, underline, inverse, hidden, strikethrough.
Font foreground colors: black, red, green, yellow, blue, magenta, cyan, white, gray, grey.
Background colors: blackBG, redBG, greenBG, yellowBG, blueBG magentaBG, cyanBG, whiteBG
Try with the code below. It will combine both formatting. It works for me.
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: 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"