Skip to content

Instantly share code, notes, and snippets.

@michaelneu
Created September 3, 2016 09:35
Show Gist options
  • Save michaelneu/867e31b47b4ec5810b5d26e6aacd5e2b to your computer and use it in GitHub Desktop.
Save michaelneu/867e31b47b4ec5810b5d26e6aacd5e2b to your computer and use it in GitHub Desktop.
Customized Winston logger with chalk-colored output
var winston = require("winston"),
dateformat = require("dateformat"),
chalk = require("chalk");
module.exports = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
timestamp: function () {
return dateformat(Date.now(), "yyyy-mm-dd HH:MM:ss.l");
},
formatter: function (options) {
var message = "";
if (options.message !== undefined) {
message = options.message;
}
var meta = "";
if (options.meta && Object.keys(options.meta).length) {
meta = "\n\t" + JSON.stringify(options.meta);
}
var level = options.level.toUpperCase();
switch (level) {
case "INFO":
level = chalk.cyan(level);
break;
case "WARN":
level = chalk.yellow(level);
break;
case "ERROR":
level = chalk.red(level);
break;
default:
break;
}
var output = [
"[" + options.timestamp() + "][" + level + "]",
message,
meta
];
return output.join(" ");
}
})
]
});
@platzhirsch
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment