-
-
Save askdaddy/79adf45927d155cf06d34aff92846192 to your computer and use it in GitHub Desktop.
Custom Logger Library with winston
This file contains 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') | |
// set default log level. | |
var logLevel = 'info' | |
// Set up logger | |
var customColors = { | |
trace: 'white', | |
debug: 'green', | |
info: 'blue', | |
warn: 'yellow', | |
crit: 'red', | |
fatal: 'red' | |
} | |
var logger = new (winston.Logger)({ | |
colors: customColors, | |
level: logLevel, | |
levels: { | |
fatal: 0, | |
crit: 1, | |
warn: 2, | |
info: 3, | |
debug: 4, | |
trace: 5 | |
}, | |
transports: [ | |
new (winston.transports.Console)({ | |
colorize: true, | |
timestamp: true | |
}), | |
new (winston.transports.File)({ filename: 'somefile.log' }) | |
] | |
}) | |
winston.addColors(customColors) | |
// Extend logger object to properly log 'Error' types | |
var origLog = logger.log | |
logger.log = function (level, msg) { | |
if (msg instanceof Error) { | |
var args = Array.prototype.slice.call(arguments) | |
args[1] = msg.stack | |
origLog.apply(logger, args) | |
} else { | |
origLog.apply(logger, arguments) | |
} | |
} | |
/* LOGGER EXAMPLES | |
var log = require('./log.js') | |
log.trace('testing') | |
log.debug('testing') | |
log.info('testing') | |
log.warn('testing') | |
log.crit('testing') | |
log.fatal('testing') | |
*/ | |
module.exports = logger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment