Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using node's process to emit events to log stuff from anywhere
// 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()));
} );
@constantology

This comment has been minimized.

Show comment Hide comment
@constantology

constantology Jul 24, 2014

this was jeffwad's original idea

Owner

constantology commented Jul 24, 2014

this was jeffwad's original idea

@iancrowther

This comment has been minimized.

Show comment Hide comment
@iancrowther

iancrowther Jul 24, 2014

nice

nice

@RangerMauve

This comment has been minimized.

Show comment Hide comment
@RangerMauve

RangerMauve Jul 24, 2014

This should be on NPM.

This should be on NPM.

@mscdex

This comment has been minimized.

Show comment Hide comment
@mscdex

mscdex Jul 24, 2014

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.

mscdex commented Jul 24, 2014

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.

@dougwilson

This comment has been minimized.

Show comment Hide comment
@dougwilson

dougwilson Jul 24, 2014

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.

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.

@ketansp

This comment has been minimized.

Show comment Hide comment
@ketansp

ketansp Mar 22, 2017

Can you please specify which versions of node are supported? I cant get it working in 6.6.0

ketansp commented Mar 22, 2017

Can you please specify which versions of node are supported? I cant get it working in 6.6.0

@aequasi

This comment has been minimized.

Show comment Hide comment
@aequasi

aequasi Nov 15, 2017

WHy

aequasi commented Nov 15, 2017

WHy

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