From ced570413cda78fc2666d1715f6a6be143fe52f1 Mon Sep 17 00:00:00 2001 From: Danny Brain Date: Fri, 28 Oct 2011 10:07:48 +1100 Subject: [PATCH 1/2] Pass in all appender parameters to the Hook constructor so a port can be specified --- README.md | 1 + lib/appenders/hookio.js | 11 +++++------ test/hookioAppender.js | 8 +++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f1926da..7ce5666 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,7 @@ This was mainly created for [cluster](https://github.com/LearnBoost/cluster), bu .use(cluster.pidfiles('run/pids')) .listen(3000); +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) diff --git a/lib/appenders/hookio.js b/lib/appenders/hookio.js index 3ce3301..5e3108d 100644 --- a/lib/appenders/hookio.js +++ b/lib/appenders/hookio.js @@ -23,14 +23,14 @@ function deserializeLoggingEvent(loggingEvent) { }; } -function createAppender(hookioOptions, actualAppender) { +function createAppender(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(); @@ -45,10 +45,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'; diff --git a/test/hookioAppender.js b/test/hookioAppender.js index 32c3d66..12e6650 100644 --- a/test/hookioAppender.js +++ b/test/hookioAppender.js @@ -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); From 8b376eb46ecca9b9fe4a6f503e970ba1344795f4 Mon Sep 17 00:00:00 2001 From: Danny Brain Date: Fri, 28 Oct 2011 10:50:28 +1100 Subject: [PATCH 2/2] Buffer the logging until the hook is ready, will prevent lost logs --- lib/appenders/hookio.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/appenders/hookio.js b/lib/appenders/hookio.js index 5e3108d..8574030 100644 --- a/lib/appenders/hookio.js +++ b/lib/appenders/hookio.js @@ -23,7 +23,7 @@ function deserializeLoggingEvent(loggingEvent) { }; } -function createAppender(hookioOptions) { +function initHook(hookioOptions) { var loggerHook; if (hookioOptions.mode === 'master') { // Start the master hook, handling the actual logging @@ -33,11 +33,32 @@ function createAppender(hookioOptions) { 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) {