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:
parent
0aca64623e
commit
3b77a42706
55
lib/appenders/hookio.js
Normal file
55
lib/appenders/hookio.js
Normal 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;
|
@ -248,13 +248,13 @@ function getDefaultLogger () {
|
||||
return getLogger(DEFAULT_CATEGORY);
|
||||
}
|
||||
|
||||
function findConfiguration() {
|
||||
function findConfiguration(filename) {
|
||||
var path;
|
||||
try {
|
||||
path = require.resolve('log4js.json');
|
||||
path = require.resolve(filename || 'log4js.json');
|
||||
} catch (e) {
|
||||
//file not found. default to the one in the log4js module.
|
||||
path = __dirname + '/log4js.json';
|
||||
path = filename || __dirname + '/log4js.json';
|
||||
}
|
||||
|
||||
return path;
|
||||
@ -263,8 +263,8 @@ function findConfiguration() {
|
||||
var configState = {};
|
||||
|
||||
function loadConfigurationFile(filename) {
|
||||
filename = filename || findConfiguration();
|
||||
if (filename && (!configState.lastFilename || filename !== configState.lastFilename ||
|
||||
filename = findConfiguration(filename);
|
||||
if (filename && (!configState.lastFilename || filename !== configState.lastFilename ||
|
||||
!configState.lastMTime || fs.statSync(filename).mtime !== configState.lastMTime)) {
|
||||
configState.lastFilename = filename;
|
||||
configState.lastMTime = fs.statSync(filename).mtime;
|
||||
@ -285,7 +285,7 @@ function configureOnceOff(config) {
|
||||
}
|
||||
|
||||
function reloadConfiguration() {
|
||||
var filename = configState.filename || findConfiguration(),
|
||||
var filename = findConfiguration(configState.filename),
|
||||
mtime;
|
||||
if (!filename) {
|
||||
// can't find anything to reload
|
||||
@ -371,6 +371,7 @@ module.exports = {
|
||||
|
||||
layouts: layouts,
|
||||
appenders: {},
|
||||
appenderMakers: appenderMakers,
|
||||
connectLogger: require('./connect-logger').connectLogger
|
||||
};
|
||||
|
||||
|
11
package.json
11
package.json
@ -6,23 +6,26 @@
|
||||
"logging",
|
||||
"log",
|
||||
"log4j",
|
||||
"node"
|
||||
"node"
|
||||
],
|
||||
"main": "./lib/log4js",
|
||||
"author": "Gareth Jones <gareth.jones@sensis.com.au>",
|
||||
"bugs": {
|
||||
"web": "http://github.com/csausdev/log4js-node/issues"
|
||||
"web": "http://github.com/csausdev/log4js-node/issues"
|
||||
},
|
||||
"engines": [ "node >=0.4" ],
|
||||
"scripts": {
|
||||
"test": "vows test/*.js"
|
||||
"test": "vows test/*.js"
|
||||
},
|
||||
"directories": {
|
||||
"test": "test",
|
||||
"lib": "lib"
|
||||
},
|
||||
"dependencies": {
|
||||
"hook.io": "0.7.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"vows": ">=0.5.2",
|
||||
"sandboxed-module": ">= 0.1.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user