moved hookio appender to log4js-hookio
This commit is contained in:
parent
c3d3a8c363
commit
b12a39ac79
@ -1,76 +0,0 @@
|
||||
"use strict";
|
||||
var log4js = require('../log4js')
|
||||
, layouts = require('../layouts')
|
||||
, Hook = require('hook.io').Hook
|
||||
, 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 initHook(hookioOptions) {
|
||||
var loggerHook;
|
||||
if (hookioOptions.mode === 'master') {
|
||||
// Start the master hook, handling the actual logging
|
||||
loggerHook = new Logger(hookioOptions);
|
||||
} else {
|
||||
// Start a worker, just emitting events for a master
|
||||
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;
|
||||
});
|
||||
|
||||
return function log(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) {
|
||||
var actualAppender;
|
||||
if (config.appender && config.mode === 'master') {
|
||||
log4js.loadAppender(config.appender.type);
|
||||
actualAppender = log4js.appenderMakers[config.appender.type](config.appender);
|
||||
config.actualAppender = actualAppender;
|
||||
}
|
||||
return createAppender(config);
|
||||
}
|
||||
|
||||
exports.appender = createAppender;
|
||||
exports.configure = configure;
|
@ -1,176 +0,0 @@
|
||||
"use strict";
|
||||
var vows = require('vows')
|
||||
, assert = require('assert')
|
||||
, sandbox = require('sandboxed-module');
|
||||
|
||||
function fancyResultingHookioAppender(hookNotReady) {
|
||||
var emitHook = !hookNotReady
|
||||
, result = { ons: {}, emissions: {}, logged: [], configs: [] };
|
||||
|
||||
var fakeLog4Js = {
|
||||
appenderMakers: {}
|
||||
};
|
||||
fakeLog4Js.loadAppender = function (appender) {
|
||||
fakeLog4Js.appenderMakers[appender] = function (config) {
|
||||
result.actualLoggerConfig = config;
|
||||
return function log(logEvent) {
|
||||
result.logged.push(logEvent);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
var fakeHookIo = { Hook: function(config) { result.configs.push(config); } };
|
||||
fakeHookIo.Hook.prototype.start = function () {
|
||||
result.startCalled = true;
|
||||
};
|
||||
fakeHookIo.Hook.prototype.on = function (eventName, functionToExec) {
|
||||
result.ons[eventName] = { functionToExec: functionToExec };
|
||||
if (emitHook && eventName === 'hook::ready') {
|
||||
functionToExec();
|
||||
}
|
||||
};
|
||||
fakeHookIo.Hook.prototype.emit = function (eventName, data) {
|
||||
result.emissions[eventName] = result.emissions[eventName] || [];
|
||||
result.emissions[eventName].push({data: data});
|
||||
var on = '*::' + eventName;
|
||||
if (eventName !== 'hook::ready' && result.ons[on]) {
|
||||
result.ons[on].callingCount =
|
||||
result.ons[on].callingCount ? result.ons[on].callingCount += 1 : 1;
|
||||
result.ons[on].functionToExec(data);
|
||||
}
|
||||
};
|
||||
|
||||
return { theResult: result,
|
||||
theModule: sandbox.require('../lib/appenders/hookio', {
|
||||
requires: {
|
||||
'../log4js': fakeLog4Js,
|
||||
'hook.io': fakeHookIo
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
vows.describe('log4js hookioAppender').addBatch({
|
||||
'master': {
|
||||
topic: function() {
|
||||
var fancy = fancyResultingHookioAppender();
|
||||
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;
|
||||
},
|
||||
|
||||
'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);
|
||||
},
|
||||
|
||||
'data written should be formatted correctly': function (result) {
|
||||
assert.equal(result.logged[0].level.toString(), 'INFO');
|
||||
assert.equal(result.logged[0].data, 'ALRIGHTY THEN');
|
||||
assert.isTrue(typeof(result.logged[0].startTime) === 'object');
|
||||
assert.equal(result.logged[1].level.toString(), 'DEBUG');
|
||||
assert.equal(result.logged[1].data, 'OH WOW');
|
||||
assert.isTrue(typeof(result.logged[1].startTime) === 'object');
|
||||
},
|
||||
|
||||
'the actual logger should get the right config': function (result) {
|
||||
assert.equal(result.actualLoggerConfig.type, 'file');
|
||||
}
|
||||
},
|
||||
'worker': {
|
||||
'should emit logging events to the master': {
|
||||
topic: function() {
|
||||
var fancy = fancyResultingHookioAppender();
|
||||
var logger = fancy.theModule.configure({
|
||||
name: 'ohno',
|
||||
mode: 'worker',
|
||||
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;
|
||||
},
|
||||
|
||||
'should not write to the actual appender': function (result) {
|
||||
assert.isTrue(result.startCalled);
|
||||
assert.equal(result.logged.length, 0);
|
||||
assert.equal(result.emissions['ohno::log'].length, 2);
|
||||
assert.isUndefined(result.ons['*::ohno::log']);
|
||||
}
|
||||
}
|
||||
},
|
||||
'when hook not ready': {
|
||||
topic: function() {
|
||||
var fancy = fancyResultingHookioAppender(true)
|
||||
, logger = fancy.theModule.configure({
|
||||
name: 'ohno',
|
||||
mode: 'worker'
|
||||
});
|
||||
|
||||
logger({
|
||||
level: { levelStr: 'INFO' },
|
||||
data: "something",
|
||||
startTime: '2011-10-27T03:45:12.031Z'
|
||||
});
|
||||
return fancy;
|
||||
},
|
||||
'should buffer the log events': function(fancy) {
|
||||
assert.isUndefined(fancy.theResult.emissions['ohno::log']);
|
||||
},
|
||||
},
|
||||
'when hook ready': {
|
||||
topic: function() {
|
||||
var fancy = fancyResultingHookioAppender(true)
|
||||
, logger = fancy.theModule.configure({
|
||||
name: 'ohno',
|
||||
mode: 'worker'
|
||||
});
|
||||
|
||||
logger({
|
||||
level: { levelStr: 'INFO' },
|
||||
data: "something",
|
||||
startTime: '2011-10-27T03:45:12.031Z'
|
||||
});
|
||||
|
||||
fancy.theResult.ons['hook::ready'].functionToExec();
|
||||
return fancy;
|
||||
},
|
||||
'should emit the buffered events': function(fancy) {
|
||||
assert.equal(fancy.theResult.emissions['ohno::log'].length, 1);
|
||||
}
|
||||
}
|
||||
|
||||
}).exportTo(module);
|
Loading…
Reference in New Issue
Block a user