Instantly share code, notes, and snippets.

Embed
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){
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':
// 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.info.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));
};
@temitope

This comment has been minimized.

temitope commented Aug 24, 2014

thx!

@temitope

This comment has been minimized.

temitope commented Sep 17, 2014

helpful

@shaikatzir

This comment has been minimized.

shaikatzir commented Sep 23, 2014

line 44 - >
logger.info.apply(logger, formatArgs(arguments));
should be (?) ->
logger.log.apply(logger, formatArgs(arguments));

@verboze

This comment has been minimized.

verboze commented Nov 7, 2015

awesome, thanks!

@xruiz

This comment has been minimized.

xruiz commented Dec 13, 2015

The code is right. I've tried your suggestion and it doesn't work.

@sebringj

This comment has been minimized.

sebringj commented Sep 3, 2016

This is a very useful idea because it allows you to program as you normally would in debug mode yet override logging in production so that it isn't synchronous. Simple and good.

@fega

This comment has been minimized.

fega commented Aug 31, 2017

Simple ES6 syntax :)

    console.log = (...args) => logger.info.call(logger, ...args);
    console.info = (...args) => logger.info.call(logger, ...args);
    console.warn = (...args) => logger.warn.call(logger, ...args);
    console.error = (...args) => logger.error.call(logger, ...args);
    console.debug = (...args) => logger.debug.call(logger, ...args);
@nigilan

This comment has been minimized.

nigilan commented Sep 7, 2017

This is helpful! Thanks!

@jmjpro

This comment has been minimized.

jmjpro commented Sep 18, 2017

Thanks @fega!

@elyobo

This comment has been minimized.

elyobo commented Sep 26, 2017

@fega surely it could be more simple, the .call seems redundant.

  console.log = (...args) => logger.info(...args);
  // etc
@rob2d

This comment has been minimized.

rob2d commented Jan 8, 2018

@spmason thanks for the great simple reference here!

@fega much easier to read, but important to note that it's not semantically equivalent to normal syntax; binds this to current scope which could or might cause people unfamiliar to get confused or have debugging problems somewhere (if they want to override with any other logic as well). Not to say it's bad, just pointing out in case somebody is bashing their head on the keyboard somewhere one day :)

@sbolel

This comment has been minimized.

sbolel commented Jan 23, 2018

Thanks for the info everyone.

@spmason and @fega -- I've quoted your answers in this stackoverflow answer, and marked it as a community wiki since I did not write the answers myself. Cheers.

@carlosatta

This comment has been minimized.

carlosatta commented Apr 18, 2018

tnks

@Himanshu922

This comment has been minimized.

Himanshu922 commented Aug 29, 2018

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

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