From 8b376eb46ecca9b9fe4a6f503e970ba1344795f4 Mon Sep 17 00:00:00 2001 From: Danny Brain Date: Fri, 28 Oct 2011 10:50:28 +1100 Subject: [PATCH] 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) {