Colourized timestamp
Opened this issue · 2 comments
I'm currently using a format like so:
const formatter = require("winston").format;
new winston.transports.Console({
format: formatter.combine(
formatter.timestamp(),
formatter.colorize(),
formatter.align(),
formatter.printf(
(info) => `[${info.timestamp}] ${info.level}: ${info.message}`
)
),
})
I wanted to set a colour to my timestamp, I don't see a way of doing that at this point. Adding this to the colors
object would be ideal. Something like:
formatter.colorize({
colors: {
timestamp: "red",
},
})
I have a small recommendation for the default color for timestamp too: #666666
, in case anyone's listening :)
This is already possible and I am using it in development.
You need to install chalk.
Then you can do:
const chalk = require('chalk');
const { combine, timestamp, colorize, align, printf } = winston.format;
new winston.transports.Console({
format: combine(
timestamp(),
colorize(),
align(),
printf(
({ timestamp, level, message }) => chalk`[{keyword('lightblue') ${timestamp}}] ${level}: ${message}`
)
)
});
See chalk's documentation on tagged template literals https://github.com/chalk/chalk#tagged-template-literal
In case you're curious, this is what i'm using currently:
if (process.env.NODE_ENV !== 'production') {
logger.add(new transports.Console({
format: combine(
format.splat(),
format.colorize(),
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
format.ms(),
printf(({ timestamp, level, message, ms }) => {
return chalk`[{keyword('lightblue') ${timestamp}}] [${level}]: ${message} {keyword('magenta') ${ms}}`
})
)
}));
}
This is already possible with the winston-timestamp-colorize module, as the name says it allows you to colorize the timestamp. Example:
const winston = require("winston");
const timestampColorize = require("winston-timestamp-colorize");
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.splat(),
winston.format.align(),
winston.format.timestamp(),
winston.format.colorize(),
timestampColorize({ color: "red" }),
winston.format.printf(
info => `${info.timestamp} [${info.level}]:${info.message}`
)
),
level: "debug",
transports: [new winston.transports.Console({})]
});
logger.debug("Hello world!");
Available colors are:
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
- gray
- grey