Created
September 3, 2016 09:35
-
-
Save michaelneu/867e31b47b4ec5810b5d26e6aacd5e2b to your computer and use it in GitHub Desktop.
Customized Winston logger with chalk-colored output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" "); | |
} | |
}) | |
] | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!