Skip to content

Commit

Permalink
[minor] Expose more functionality on the logger plugin publicly for r…
Browse files Browse the repository at this point in the history
…euse
  • Loading branch information
indexzero committed May 9, 2011
1 parent a4a2826 commit 36c425c
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions lib/haibu/plugins/logger.js
Expand Up @@ -50,21 +50,7 @@ logger.init = function (options) {
// Monkey punch `haibu.emit` to allow for lazy listening
// of logging events.
//
var _emit = haibu.emit;
haibu.emit = function (ev) {
if (ev !== 'newListener') {
if (!listening[ev]) {
listening[ev] = true;
haibu.on(ev, function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(ev);
logEvent.apply(null, args);
});
}

_emit.apply(haibu, arguments);
}
};
logger._logAll(haibu);

// Initialize a new winston logger for each input
inputs.forEach(function (input) {
Expand Down Expand Up @@ -114,7 +100,16 @@ logger.initInput = function (input, options) {
logger[input] = loggers[input];
};

function logEvent (ev, level, msg, meta) {
//
// ### function logEvent (ev, level, msg, meta)
// #### @ev {string} Name of the event to log
// #### @level {string} Level to log the event at
// #### @msg {string} Message to log at the specified level
// #### @meta {Object} Metadata for this event to log
// Logs the specified event `ev` with the `level`, `msg`
// and `meta` supplied.
//
logger.logEvent = function (ev, level, msg, meta) {
var name = ev.split(':')[0];
input = namespaces[name] || 'haibu',
log = loggers[input];
Expand All @@ -129,4 +124,40 @@ function logEvent (ev, level, msg, meta) {
}

log[level](ev, meta);
};

//
// ### private function _logAll (emitter)
// #### @emitter {events.EventEmitter} Emitter to log all events from
// Monkey punch `emitter.emit` to allow for lazy listening
// of logging events.
//
logger._logAll = function (emitter) {
//
// Store a reference to the original `emitter.emit` function
//
var _emit = emitter.emit;

//
// Overwrite `emitter.emit` to lazily add a listener to any
// event if the boolean flag is not set in `logger.listening`.
//
emitter.emit = function (ev) {
if (ev !== 'newListener') {
if (!listening[ev]) {
listening[ev] = true;
emitter.on(ev, function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(ev);
logger.logEvent.apply(null, args);
});
}

//
// Call the original `emitter.emit` function now that
// (potentially) a new listener has been added.
//
_emit.apply(emitter, arguments);
}
};
};

0 comments on commit 36c425c

Please sign in to comment.