-
-
Save rveitch/12534bd8e573843352a41010a818ac27 to your computer and use it in GitHub Desktop.
A simple node module that makes console.log/error/warn/debug statements log through winston (simply include at the beginning of your app)
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
'use strict'; | |
var util = require('util'), | |
winston = require('winston'), | |
logger = new winston.Logger(), | |
production = (process.env.NODE_ENV || '').toLowerCase() === 'production'; | |
module.exports = { | |
middleware: function(req, res, next){ | |
console.info(req.method, req.url, res.statusCode); | |
next(); | |
}, | |
production: production | |
}; | |
// Override the built-in console methods with winston hooks | |
switch((process.env.NODE_ENV || '').toLowerCase()){ | |
case 'production': | |
production = true; | |
logger.add(winston.transports.File, { | |
filename: __dirname + '/application.log', | |
handleExceptions: true, | |
exitOnError: false, | |
level: 'warn' | |
}); | |
break; | |
case 'test': | |
case 'development': | |
// Don't set up the logger overrides | |
return; | |
default: | |
logger.add(winston.transports.Console, { | |
colorize: true, | |
timestamp: true, | |
level: 'info' | |
}); | |
break; | |
} | |
function formatArgs(args){ | |
return [util.format.apply(util.format, Array.prototype.slice.call(args))]; | |
} | |
console.log = function(){ | |
logger.log.apply(logger, formatArgs(arguments)); | |
}; | |
console.info = function(){ | |
logger.info.apply(logger, formatArgs(arguments)); | |
}; | |
console.warn = function(){ | |
logger.warn.apply(logger, formatArgs(arguments)); | |
}; | |
console.error = function(){ | |
logger.error.apply(logger, formatArgs(arguments)); | |
}; | |
console.debug = function(){ | |
logger.debug.apply(logger, formatArgs(arguments)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment