Merge pull request #32 from dbrain/master
hook.io appender should accept all configuration
This commit is contained in:
commit
0a0119300b
@ -157,6 +157,7 @@ This was mainly created for [cluster](https://github.com/LearnBoost/cluster), bu
|
||||
.use(cluster.pidfiles('run/pids'))
|
||||
.listen(3000);
|
||||
</pre>
|
||||
log4js-master/worker.json hookio appender parameters will be passed into the Hook constructor directly, so you can specify hook-port, hook-host etc.
|
||||
*NOTE* hook.io appender will currently (and probably indefinitely) explode if you enable hook.io debug because of the way log4js overrides console.log
|
||||
|
||||
## author (of this node version)
|
||||
|
@ -23,21 +23,42 @@ function deserializeLoggingEvent(loggingEvent) {
|
||||
};
|
||||
}
|
||||
|
||||
function createAppender(hookioOptions, actualAppender) {
|
||||
function initHook(hookioOptions) {
|
||||
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 });
|
||||
loggerHook = new Logger(hookioOptions);
|
||||
} else {
|
||||
// Start a worker, just emitting events for a master
|
||||
loggerHook = new Hook({ name: hookioOptions.name, debug: hookioOptions.debug });
|
||||
loggerHook = new Hook(hookioOptions);
|
||||
}
|
||||
loggerHook.start();
|
||||
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);
|
||||
})
|
||||
hookReady = true;
|
||||
});
|
||||
|
||||
var loggerEvent = hookioOptions.name + '::log';
|
||||
return function log(loggingEvent) {
|
||||
loggerHook.emit(loggerEvent, loggingEvent);
|
||||
};
|
||||
if (hookReady) {
|
||||
hook.emit(eventName, loggingEvent);
|
||||
} else {
|
||||
hookBuffer.push(loggingEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createAppender(hookioOptions) {
|
||||
var loggerHook = initHook(hookioOptions);
|
||||
var loggerEvent = hookioOptions.name + '::log';
|
||||
return getBufferedHook(loggerHook, loggerEvent);
|
||||
}
|
||||
|
||||
function configure(config) {
|
||||
@ -45,10 +66,9 @@ function configure(config) {
|
||||
if (config.appender && config.mode === 'master') {
|
||||
log4js.loadAppender(config.appender.type);
|
||||
actualAppender = log4js.appenderMakers[config.appender.type](config.appender);
|
||||
config.actualAppender = actualAppender;
|
||||
}
|
||||
delete config.appender;
|
||||
delete config.type;
|
||||
return createAppender(config, actualAppender);
|
||||
return createAppender(config);
|
||||
}
|
||||
|
||||
exports.name = 'hookio';
|
||||
|
@ -3,7 +3,7 @@ var assert = require('assert');
|
||||
var sandbox = require('sandboxed-module');
|
||||
|
||||
function fancyResultingHookioAppender(opts) {
|
||||
var result = { ons: {}, emissions: {}, logged: [] };
|
||||
var result = { ons: {}, emissions: {}, logged: [], configs: [] };
|
||||
|
||||
var fakeLog4Js = {
|
||||
appenderMakers: {}
|
||||
@ -17,7 +17,7 @@ function fancyResultingHookioAppender(opts) {
|
||||
};
|
||||
};
|
||||
|
||||
var fakeHookIo = { Hook: function() { } };
|
||||
var fakeHookIo = { Hook: function(config) { result.configs.push(config); } };
|
||||
fakeHookIo.Hook.prototype.start = function () {
|
||||
result.startCalled = true;
|
||||
};
|
||||
@ -52,7 +52,7 @@ vows.describe('log4js hookioAppender').addBatch({
|
||||
'master': {
|
||||
topic: function() {
|
||||
var fancy = fancyResultingHookioAppender();
|
||||
var logger = fancy.theModule.configure({ name: 'ohno', mode: 'master', appender: { type: 'file' } });
|
||||
var logger = fancy.theModule.configure({ name: 'ohno', mode: 'master', 'hook-port': 5001, appender: { type: 'file' } });
|
||||
logger({ level: { levelStr: 'INFO' }, data: "ALRIGHTY THEN", startTime: '2011-10-27T03:53:16.031Z' });
|
||||
logger({ level: { levelStr: 'DEBUG' }, data: "OH WOW", startTime: '2011-10-27T04:53:16.031Z'});
|
||||
return fancy.theResult;
|
||||
@ -60,6 +60,8 @@ vows.describe('log4js hookioAppender').addBatch({
|
||||
|
||||
'should write to the actual appender': function (result) {
|
||||
assert.isTrue(result.startCalled);
|
||||
assert.equal(result.configs.length, 1);
|
||||
assert.equal(result.configs[0]['hook-port'], 5001);
|
||||
assert.equal(result.logged.length, 2);
|
||||
assert.equal(result.emissions['ohno::log'].length, 2);
|
||||
assert.equal(result.ons['*::ohno::log'].callingCount, 2);
|
||||
|
Loading…
Reference in New Issue
Block a user