'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)); | |
}; |
This comment has been minimized.
This comment has been minimized.
helpful |
This comment has been minimized.
This comment has been minimized.
line 44 - > |
This comment has been minimized.
This comment has been minimized.
awesome, thanks! |
This comment has been minimized.
This comment has been minimized.
The code is right. I've tried your suggestion and it doesn't work. |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
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); |
This comment has been minimized.
This comment has been minimized.
This is helpful! Thanks! |
This comment has been minimized.
This comment has been minimized.
Thanks @fega! |
This comment has been minimized.
This comment has been minimized.
@fega surely it could be more simple, the
|
This comment has been minimized.
This comment has been minimized.
@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 comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
tnks |
This comment has been minimized.
This comment has been minimized.
Can i override logger level default methods, actually i want to save all logger.error msgs in mongo |
This comment has been minimized.
This comment has been minimized.
Took a stab at an updated version with color support: https://gist.github.com/BinaryShrub/790d97e34247d409f85b7c9434d0ddd5 |
This comment has been minimized.
thx!