Create a gist now

Instantly share code, notes, and snippets.

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));



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

verboze commented Nov 7, 2015

awesome, thanks!

xruiz commented Dec 13, 2015

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

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 commented Aug 31, 2017 edited

Simple ES6 syntax :)

    console.log = (...args) =>, ...args); = (...args) =>, ...args);
    console.warn = (...args) =>, ...args);
    console.error = (...args) =>, ...args);
    console.debug = (...args) =>, ...args);

nigilan commented Sep 7, 2017 edited

This is helpful! Thanks!

jmjpro commented Sep 18, 2017

Thanks @fega!

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