Buffer the logging until the hook is ready, will prevent lost logs

This commit is contained in:
Danny Brain 2011-10-28 10:50:28 +11:00
parent ced570413c
commit 8b376eb46e

View File

@ -23,7 +23,7 @@ function deserializeLoggingEvent(loggingEvent) {
}; };
} }
function createAppender(hookioOptions) { function initHook(hookioOptions) {
var loggerHook; var loggerHook;
if (hookioOptions.mode === 'master') { if (hookioOptions.mode === 'master') {
// Start the master hook, handling the actual logging // Start the master hook, handling the actual logging
@ -33,11 +33,32 @@ function createAppender(hookioOptions) {
loggerHook = new Hook(hookioOptions); loggerHook = new Hook(hookioOptions);
} }
loggerHook.start(); 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) { 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) { function configure(config) {