Added a hookio appender, this allows you to run a 'master' log4js instance and 'worker' so only one process writes to file

This commit is contained in:
Danny Brain 2011-10-27 12:38:13 +11:00
parent 0aca64623e
commit 3b77a42706
3 changed files with 69 additions and 10 deletions

55
lib/appenders/hookio.js Normal file
View File

@ -0,0 +1,55 @@
var log4js = require('../log4js');
var layouts = require('../layouts');
var Hook = require('hook.io').Hook;
var util = require('util');
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);
});
});
}
util.inherits(Logger, Hook);
function deserializeLoggingEvent(loggingEvent) {
loggingEvent.startTime = new Date(loggingEvent.startTime);
loggingEvent.level.toString = function levelToString() {
return loggingEvent.level.levelStr;
};
}
function createAppender(hookioOptions, actualAppender) {
var loggerHook;
if (hookioOptions.mode === 'master') {
// Start the master hook, handling the actual logging
loggerHook = new Logger({ name: hookioOptions.name, debug: hookioOptions.debug, actualAppender: actualAppender });
} else {
// Start a worker, just emitting events for a master
loggerHook = new Hook({ name: hookioOptions.name, debug: hookioOptions.debug });
}
loggerHook.start();
var loggerEvent = hookioOptions.name + '::log';
return function log(loggingEvent) {
loggerHook.emit(loggerEvent, loggingEvent);
};
}
function configure(config) {
var actualAppender;
if (config.appender && config.mode === 'master') {
actualAppender = log4js.appenderMakers[config.appender.type](config.appender);
}
delete config.appender;
delete config.type;
return createAppender(config, actualAppender);
}
exports.name = 'hookio';
exports.appender = createAppender;
exports.configure = configure;

View File

@ -248,13 +248,13 @@ function getDefaultLogger () {
return getLogger(DEFAULT_CATEGORY); return getLogger(DEFAULT_CATEGORY);
} }
function findConfiguration() { function findConfiguration(filename) {
var path; var path;
try { try {
path = require.resolve('log4js.json'); path = require.resolve(filename || 'log4js.json');
} catch (e) { } catch (e) {
//file not found. default to the one in the log4js module. //file not found. default to the one in the log4js module.
path = __dirname + '/log4js.json'; path = filename || __dirname + '/log4js.json';
} }
return path; return path;
@ -263,7 +263,7 @@ function findConfiguration() {
var configState = {}; var configState = {};
function loadConfigurationFile(filename) { function loadConfigurationFile(filename) {
filename = filename || findConfiguration(); filename = findConfiguration(filename);
if (filename && (!configState.lastFilename || filename !== configState.lastFilename || if (filename && (!configState.lastFilename || filename !== configState.lastFilename ||
!configState.lastMTime || fs.statSync(filename).mtime !== configState.lastMTime)) { !configState.lastMTime || fs.statSync(filename).mtime !== configState.lastMTime)) {
configState.lastFilename = filename; configState.lastFilename = filename;
@ -285,7 +285,7 @@ function configureOnceOff(config) {
} }
function reloadConfiguration() { function reloadConfiguration() {
var filename = configState.filename || findConfiguration(), var filename = findConfiguration(configState.filename),
mtime; mtime;
if (!filename) { if (!filename) {
// can't find anything to reload // can't find anything to reload
@ -371,6 +371,7 @@ module.exports = {
layouts: layouts, layouts: layouts,
appenders: {}, appenders: {},
appenderMakers: appenderMakers,
connectLogger: require('./connect-logger').connectLogger connectLogger: require('./connect-logger').connectLogger
}; };

View File

@ -21,6 +21,9 @@
"test": "test", "test": "test",
"lib": "lib" "lib": "lib"
}, },
"dependencies": {
"hook.io": "0.7.7"
},
"devDependencies": { "devDependencies": {
"vows": ">=0.5.2", "vows": ">=0.5.2",
"sandboxed-module": ">= 0.1.1" "sandboxed-module": ">= 0.1.1"