From a669f7c2f4f64670b0d078795955c78a79613bed Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Tue, 2 Dec 2014 12:16:15 +0100 Subject: [PATCH] Allow shutting down of appenders without disabling future log writes --- lib/appenders/file.js | 4 +++- lib/log4js.js | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/appenders/file.js b/lib/appenders/file.js index 6788d30..44bedf7 100644 --- a/lib/appenders/file.js +++ b/lib/appenders/file.js @@ -80,7 +80,9 @@ function configure(config, options) { } function shutdown(cb) { - async.each(openFiles, function(file, done) { + var filesToClose = openFiles; + openFiles = []; + async.each(filesToClose, function(file, done) { if (!file.write(eol, "utf-8")) { file.once('drain', function() { file.end(done); diff --git a/lib/log4js.js b/lib/log4js.js index de4f974..33a5d05 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -176,6 +176,11 @@ function clearAppenders () { } } +function clearAndShutdownAppenders(cb) { + clearAppenders(); + shutdownAppenders(cb); +} + function configureAppenders(appenderList, options) { clearAppenders(); if (appenderList) { @@ -380,7 +385,10 @@ function shutdown(cb) { // First, disable all writing to appenders. This prevents appenders from // not being able to be drained because of run-away log writes. loggerModule.disableAllLogWrites(); + shutdownAppenders(cb); +} +function shutdownAppenders(cb) { // Next, get all the shutdown functions for appenders as an array. var shutdownFunctions = Object.keys(appenderShutdowns).reduce( function(accum, category) { @@ -406,6 +414,7 @@ module.exports = { addAppender: addAppender, loadAppender: loadAppender, clearAppenders: clearAppenders, + clearAndShutdownAppenders: clearAndShutdownAppenders, configure: configure, shutdown: shutdown,