From 614127bb1035fe1c446d4cc1a61aea7516f8c4c7 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 22 Apr 2014 10:05:37 +1000 Subject: [PATCH] added shutdown to datefile --- lib/appenders/dateFile.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/appenders/dateFile.js b/lib/appenders/dateFile.js index 6317c68..e1c5cea 100644 --- a/lib/appenders/dateFile.js +++ b/lib/appenders/dateFile.js @@ -1,6 +1,7 @@ "use strict"; var streams = require('../streams') , layouts = require('../layouts') +, async = require('async') , path = require('path') , os = require('os') , eol = os.EOL || '\n' @@ -24,12 +25,12 @@ function appender(filename, pattern, alwaysIncludePattern, layout) { layout = layout || layouts.basicLayout; var logFile = new streams.DateRollingFileStream( - filename, - pattern, + filename, + pattern, { alwaysIncludePattern: alwaysIncludePattern } ); openFiles.push(logFile); - + return function(logEvent) { logFile.write(layout(logEvent) + eol, "utf8"); }; @@ -38,15 +39,15 @@ function appender(filename, pattern, alwaysIncludePattern, layout) { function configure(config, options) { var layout; - + if (config.layout) { layout = layouts.layout(config.layout.type, config.layout); } - + if (!config.alwaysIncludePattern) { config.alwaysIncludePattern = false; } - + if (options && options.cwd && !config.absolute) { config.filename = path.join(options.cwd, config.filename); } @@ -54,5 +55,18 @@ function configure(config, options) { return appender(config.filename, config.pattern, config.alwaysIncludePattern, layout); } +function shutdown(cb) { + async.forEach(openFiles, function(file, done) { + if (!file.write(eol, "utf-8")) { + file.once('drain', function() { + file.end(done); + }); + } else { + file.end(done); + } + }, cb); +} + exports.appender = appender; exports.configure = configure; +exports.shutdown = shutdown;