2013-06-04 06:17:36 +08:00
|
|
|
"use strict";
|
|
|
|
var log4js = require('../log4js')
|
|
|
|
, layouts = require('../layouts')
|
|
|
|
, Hook = require('hook.io').Hook
|
|
|
|
, util = require('util');
|
2011-10-27 09:38:13 +08:00
|
|
|
|
|
|
|
var Logger = function createLogger(options) {
|
|
|
|
var self = this;
|
|
|
|
var actualAppender = options.actualAppender;
|
|
|
|
Hook.call(self, options);
|
|
|
|
self.on('hook::ready', function hookReady() {
|
|
|
|
self.on('*::' + options.name + '::log', function log(loggingEvent) {
|
|
|
|
deserializeLoggingEvent(loggingEvent);
|
|
|
|
actualAppender(loggingEvent);
|
|
|
|
});
|
|
|
|
});
|
2013-06-04 06:17:36 +08:00
|
|
|
};
|
2011-10-27 09:38:13 +08:00
|
|
|
util.inherits(Logger, Hook);
|
|
|
|
|
|
|
|
function deserializeLoggingEvent(loggingEvent) {
|
|
|
|
loggingEvent.startTime = new Date(loggingEvent.startTime);
|
|
|
|
loggingEvent.level.toString = function levelToString() {
|
|
|
|
return loggingEvent.level.levelStr;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-10-28 07:50:28 +08:00
|
|
|
function initHook(hookioOptions) {
|
2011-10-27 09:38:13 +08:00
|
|
|
var loggerHook;
|
|
|
|
if (hookioOptions.mode === 'master') {
|
|
|
|
// Start the master hook, handling the actual logging
|
2011-10-28 07:07:48 +08:00
|
|
|
loggerHook = new Logger(hookioOptions);
|
2011-10-27 09:38:13 +08:00
|
|
|
} else {
|
|
|
|
// Start a worker, just emitting events for a master
|
2011-10-28 07:07:48 +08:00
|
|
|
loggerHook = new Hook(hookioOptions);
|
2011-10-27 09:38:13 +08:00
|
|
|
}
|
|
|
|
loggerHook.start();
|
2011-10-28 07:50:28 +08:00
|
|
|
return loggerHook;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBufferedHook(hook, eventName) {
|
|
|
|
var hookBuffer = [];
|
|
|
|
var hookReady = false;
|
|
|
|
hook.on('hook::ready', function emptyBuffer() {
|
|
|
|
hookBuffer.forEach(function logBufferItem(loggingEvent) {
|
|
|
|
hook.emit(eventName, loggingEvent);
|
2013-06-04 06:17:36 +08:00
|
|
|
});
|
2011-10-28 07:50:28 +08:00
|
|
|
hookReady = true;
|
|
|
|
});
|
2011-10-27 09:38:13 +08:00
|
|
|
|
|
|
|
return function log(loggingEvent) {
|
2011-10-28 07:50:28 +08:00
|
|
|
if (hookReady) {
|
|
|
|
hook.emit(eventName, loggingEvent);
|
|
|
|
} else {
|
|
|
|
hookBuffer.push(loggingEvent);
|
|
|
|
}
|
2013-06-04 06:17:36 +08:00
|
|
|
};
|
2011-10-28 07:50:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createAppender(hookioOptions) {
|
|
|
|
var loggerHook = initHook(hookioOptions);
|
|
|
|
var loggerEvent = hookioOptions.name + '::log';
|
|
|
|
return getBufferedHook(loggerHook, loggerEvent);
|
2011-10-27 09:38:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function configure(config) {
|
|
|
|
var actualAppender;
|
|
|
|
if (config.appender && config.mode === 'master') {
|
2011-10-27 13:25:38 +08:00
|
|
|
log4js.loadAppender(config.appender.type);
|
2011-10-27 09:38:13 +08:00
|
|
|
actualAppender = log4js.appenderMakers[config.appender.type](config.appender);
|
2011-10-28 07:07:48 +08:00
|
|
|
config.actualAppender = actualAppender;
|
2011-10-27 09:38:13 +08:00
|
|
|
}
|
2011-10-28 07:07:48 +08:00
|
|
|
return createAppender(config);
|
2011-10-27 09:38:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.appender = createAppender;
|
|
|
|
exports.configure = configure;
|