logger.js | |
---|---|
/*
* index.js: Top level module include for log module.
*
* (C) 2010, Nodejitsu Inc.
*
*/
var winston = require('winston'),
haibu = require('../../haibu'); | |
Define the set of loggers and inputs used by this module | var logger = exports,
loggers = exports.loggers = {},
listening = exports.listening = {}
inputs = exports.inputs = ['haibu'];
|
Setup mapping of special namespaces to inputs | exports.name = 'logger';
exports.namespaces = {};
var defaultConfig = {
console: {
level: 'silly',
colorize: false
}
}; |
Define a value indicating if this module is initialized | logger.initialized = false; |
function init (options, callback)@options {Object} Options to initialize this plugin with@callback {function} Continuation to respond to when completeInitalizes the | logger.init = function (options) {
if (logger.initialized) {
return;
}
options = options || {};
options.loggly = options.loggly || haibu.config.get('loggly');
options.loggly && (options.loggly.json = true);
|
Monkey punch | logger._logAll(haibu);
|
Initialize a new winston logger for each input | inputs.forEach(function (input) {
logger.initInput(input, options);
});
logger.initialized = true;
}; |
function initInput (input, options) Helper function for initializing individual inputs used by the logging module's methods e.g. drone, app, etc | logger.initInput = function (input, options) {
var transports = []; |
Create the transports for this input. Here we are defaulting to Console and Loggly. | transports.push(new winston.transports.Console(options.console || defaultConfig.console));
|
Only add the Loggly transport if the input token is in the options passed to 'init' | if (options.loggly && options.loggly.inputs[input]) {
transports.push(new winston.transports.Loggly({
subdomain: options.loggly.subdomain,
level: options.loggly.level || null,
inputToken: options.loggly.inputs[input]
}));
}
|
Create the winston logger for this input. Remark: forever will take care of this console output being saved to the appropriate file. | loggers[input] = new winston.Logger({ transports: transports });
|
Define the core logging functions for all relevant inputs ['drone', 'app'] <=> log.drone.info ... | logger[input] = loggers[input];
}; |
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 logLogs the specified event | logger.logEvent = function (ev, level, msg, meta) {
var name = ev.split(':')[0];
input = exports.namespaces[name] || 'haibu',
log = loggers[input];
if (!meta) {
meta = msg;
msg = null;
}
if (msg) {
meta.desc = msg;
}
log[level](ev, meta);
}; |
private function _logAll (emitter)@emitter {events.EventEmitter} Emitter to log all events fromMonkey punch | logger._logAll = function (emitter) { |
Store a reference to the original | var _emit = emitter.emit;
|
Overwrite | 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 | _emit.apply(emitter, arguments);
}
};
};
|