Skip to content

Instantly share code, notes, and snippets.

Created January 24, 2012 13:35
What would you like to do?
A simple node module that makes console.log/error/warn/debug statements log through winston (simply include at the beginning of your app)
'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){, req.url, res.statusCode);
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'
case 'test':
// Don't set up the logger overrides
logger.add(winston.transports.Console, {
colorize: true,
timestamp: true,
level: 'info'
function formatArgs(args){
return [util.format.apply(util.format,];
console.log = function(){, formatArgs(arguments));
}; = function(){, 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));
Copy link

Can i override logger level default methods, actually i want to save all logger.error msgs in mongo

Copy link

Took a stab at an updated version with color support:

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