I happened to use bunyan to log the the data . I wanted the logs be printed with appropriate colors like errors in red , debug yellow .. etc; unfortunately I couldn't find anyways to do that . And now I would like to know if its possible with winston. Can I change the color of log data in winston ?
here is the code that I executed .
var logger = require("winston-color");
var winston = require('winston');
var util = require('util');
var logFilename = __dirname + '/logfile.log';
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({
filename: 'logfile.log',
timestamp:true
}),
new (winston.transports.File)({
name: 'error-log',
filename: 'error.log',
level: 'error'
}),
new (winston.transports.File)({
name: 'info-log',
filename: 'info.log',
level: 'info'
}),
]
});
logger.info('Hello Winston info!');
logger.debug('Hello Winston debug!');
logger.warn('Hello Winston warn!');
logger.info('Hello again distributed logs');
logger.error('error1');
logger.error('error2');
the output screen shot here
working code's output here here
If you look for a custom color schema you can write your own transporter like this
import winston from "winston";
import Transport from "winston-transport";
const Colors = {
info: "\x1b[36m",
error: "\x1b[31m",
warn: "\x1b[33m",
verbose: "\x1b[43m",
};
class SimpleConsoleTransport extends Transport {
constructor() {
super();
}
log = (info, callback) => {
const { level, message, stack } = info;
console.log(
`${Colors[level]}${level}\t${message}\x1b[0m`,
stack ? "\n" + stack : ""
)
if (callback) {
callback();
}
};
}
then config your winston instance this way:
winston.configure({
transports: [new SimpleConsoleTransport()],
});
these are the whole list of colors you can use:
Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"
FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"
BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"
Yes you can. You can use the following code that I am using in my project.
logger/WinstonPlugin.js
/* jslint node: true */
/* jshint esversion: 6 */
'use strict';
const Winston = require('winston');
const logLevel = 'debug';
var logger;
(function createLogger() {
logger = new(Winston.Logger)({
transports: [
new(Winston.transports.Console)({
level: logLevel,
colorize: true,
timestamp: function () {
return (new Date()).toLocaleTimeString();
},
prettyPrint: true
})
]
});
Winston.addColors({
error: 'red',
warn: 'yellow',
info: 'cyan',
debug: 'green'
});
})();
module.exports = logger;
And anytime you needed the Winston in any your code file. You can access like below:
const Winston = require('logger/WinstonPlugin');
Winston.info('This is a info statement');
Winston.debug('This is a debug statement');
Winston.warn('This is a warning statement');
Winston.error('This is a error statement');
and you will see the colored output in the console.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With