From 9ac61e37f4fd35ce2ae8b0d32062d33470438360 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 25 Sep 2012 07:43:37 +1000 Subject: [PATCH] Refactored where the exit handler gets added --- lib/appenders/file.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/lib/appenders/file.js b/lib/appenders/file.js index 31afa73..38ddc46 100644 --- a/lib/appenders/file.js +++ b/lib/appenders/file.js @@ -1,12 +1,17 @@ var layouts = require('../layouts') -, path = require('path') -, fs = require('fs') -, streams = require('../streams') -, os = require('os') -, eol = os.EOL || '\n'; + , path = require('path') + , fs = require('fs') + , streams = require('../streams') + , os = require('os') + , eol = os.EOL || '\n' + , openFiles = []; -var openFiles = []; -var listenerAtttached = false; +//close open files on process exit. +process.on('exit', function() { + openFiles.forEach(function (file) { + file.end(); + }); +}); /** * File Appender writing the logs to a text file. Supports rolling of logs by size. @@ -48,16 +53,6 @@ function fileAppender (file, layout, logSize, numBackups) { // push file to the stack of open handlers openFiles.push(logFile); - //close the file on process exit. - if (!listenerAtttached) { - listenerAtttached = true; - process.on('exit', function() { - openFiles.forEach(function (file) { - file.end(); - }); - }); - } - return function(loggingEvent) { logFile.write(layout(loggingEvent) + eol, "utf8"); };