From b7cf2f1e92704d40645058148da4b066754791cc Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Sun, 28 Aug 2016 14:40:47 -0400 Subject: [PATCH] Add close-on-SIGHUP behavior for logrotate. --- lib/appenders/file.js | 75 ++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/lib/appenders/file.js b/lib/appenders/file.js index 6788d30..9c71edc 100644 --- a/lib/appenders/file.js +++ b/lib/appenders/file.js @@ -11,7 +11,16 @@ var layouts = require('../layouts') //close open files on process exit. process.on('exit', function() { openFiles.forEach(function (file) { - file.end(); + file.stream.end(); + }); +}); + +// On SIGHUP, close and reopen all files. This allows this appender to work with +// logrotate. Note that if you are using logrotate, you should not set +// `logSize`. +process.on('SIGHUP', function() { + openFiles.forEach(function(writer) { + writer.reopen(); }); }); @@ -34,38 +43,45 @@ function fileAppender (file, layout, logSize, numBackups) { //there has to be at least one backup if logSize has been specified numBackups = numBackups === 0 ? 1 : numBackups; - function openTheStream(file, fileSize, numFiles) { - var stream; - if (fileSize) { - stream = new streams.RollingFileStream( - file, - fileSize, - numFiles - ); - } else { - stream = fs.createWriteStream( - file, - { encoding: "utf8", - mode: parseInt('0644', 8), - flags: 'a' } - ); + var writer = { + stream: openTheStream(file, logSize, numBackups), + reopen: function() { + this.stream.end(); + this.stream = openTheStream(file, logSize, numBackups); } - stream.on("error", function (err) { - console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err); - }); - return stream; } - var logFile = openTheStream(file, logSize, numBackups); - // push file to the stack of open handlers - openFiles.push(logFile); - + openFiles.push(writer); + return function(loggingEvent) { - logFile.write(layout(loggingEvent) + eol, "utf8"); + writer.stream.write(layout(loggingEvent) + eol, "utf8"); }; } +function openTheStream(file, fileSize, numFiles) { + var stream; + if (fileSize) { + stream = new streams.RollingFileStream( + file, + fileSize, + numFiles + ); + } else { + stream = fs.createWriteStream( + file, + { encoding: "utf8", + mode: parseInt('0644', 8), + flags: 'a' } + ); + } + stream.on("error", function (err) { + console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err); + }); + return stream; +} + + function configure(config, options) { var layout; if (config.layout) { @@ -81,12 +97,13 @@ function configure(config, options) { function shutdown(cb) { async.each(openFiles, function(file, done) { - if (!file.write(eol, "utf-8")) { - file.once('drain', function() { - file.end(done); + var stream = file.stream; + if (!stream.write(eol, "utf-8")) { + stream.once('drain', function() { + stream.end(done); }); } else { - file.end(done); + stream.end(done); } }, cb); }