Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

using node's process to emit events to log stuff from anywhere

View process.logger.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// 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()));
} );
Owner

this was jeffwad's original idea

nice

This should be on NPM.

mscdex commented

FWIW you should avoid Array.prototype.slice.call(arguments, index) style calls if you care about performance since that will make logConsole unoptimizable by v8. Creating a new array and appending to it in a for-loop will avoid this deoptimization.

The module depd will emit deprecation events on process if there is a listener, automatically suppressing output to STDERR for centralized logging in mind similar to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.