diff --git a/lib/appenders/file.js b/lib/appenders/file.js index f2c194b4..f14a716e 100644 --- a/lib/appenders/file.js +++ b/lib/appenders/file.js @@ -5,6 +5,14 @@ const os = require('os'); const eol = os.EOL; +let mainSighupListenerStarted = false; +const sighupListeners = new Set(); +function mainSighupHandler() { + sighupListeners.forEach((app) => { + app.sighupHandler(); + }); +} + function openTheStream(file, fileSize, numFiles, options) { const stream = new streams.RollingFileStream( file, @@ -76,14 +84,22 @@ function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset }; app.shutdown = function (complete) { - process.removeListener('SIGHUP', app.sighupHandler); + sighupListeners.delete(app); + if (sighupListeners.size === 0 && mainSighupListenerStarted) { + process.removeListener('SIGHUP', mainSighupHandler); + mainSighupListenerStarted = false; + } writer.end('', 'utf-8', complete); }; // 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', app.sighupHandler); + sighupListeners.add(app); + if (!mainSighupListenerStarted) { + process.on('SIGHUP', mainSighupHandler); + mainSighupListenerStarted = true; + } return app; }