Skip to content

Instantly share code, notes, and snippets.

@flesch
Created November 20, 2014 17:44
Show Gist options
  • Save flesch/9eb7df501ea8f3016c08 to your computer and use it in GitHub Desktop.
Save flesch/9eb7df501ea8f3016c08 to your computer and use it in GitHub Desktop.
Deprecate `console.log` in favor of node-bunyan.
var deprecate = require('depd')('bunyan')
, bunyan = require('bunyan')
;
// Bunyan logger used by the app.
var logger = bunyan.createLogger({ name:'app', level:'debug' });
// Bunyan logger to catch only the console.
var depdconsole = bunyan.createLogger({ name:'console', level:'warn' });
// Overwrite `process.stderr.write` which is used by depd to log warnings.
// I'm not sure this is a good idea.
process.stderr.isTTY = false;
process.stderr.write = function(message){
depdconsole.warn(message.replace(/(.*)GMT\s(.*)\n/g, '$2'));
};
// Deprecate `console.log` and use the Bunyan instance.
console.log = deprecate.function(function(){
logger.info.apply(logger, arguments);
}, 'console.log');
// Legacy code not yet updated to Bunyan.
console.log('Hello %s!', 'World');
{"name":"console","hostname":"flesch.local","pid":3893,"level":40,"msg":"bunyan deprecated console.log at module.js:456:26","time":"2014-11-20T17:43:07.642Z","v":0}
{"name":"app","hostname":"flesch.local","pid":3893,"level":30,"msg":"Hello World!","time":"2014-11-20T17:43:07.645Z","v":0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment