Created
July 24, 2014 09:39
-
-
Save constantology/5f04d5782c1cc019722f to your computer and use it in GitHub Desktop.
using node's process to emit events to log stuff from anywhere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use like this: | |
// process.emit( 'app:log', module, arg1, arg2, ..., argN ); | |
var Module = require('module'); | |
function logConsole(method, module) { | |
var args = [(new Date()).toJSON(), method]; | |
var index = 1; | |
if (module instanceof Module) { | |
args.push(module.id); | |
index = 2; | |
} | |
args.push.apply(args, Array.prototype.slice.call(arguments, index)); | |
console.log.apply(console, args); | |
} | |
['debug', 'error', 'info', 'log', 'warn'].forEach( function(consoleMethod) { | |
process.on('app:' + consoleMethod, logConsole.bind(null, consoleMethod.toUpperCase())); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @nokome,
My friend @jeffwad — whom I commented above as, having the original idea for this — and I had completely forgotten about it when I got an email notification of your comment from github. 😅
I'm glad posting it gave you some value/inspiration, however small... 😊