From d991f3bc60e4430bb89b06e0819daab97aec0e98 Mon Sep 17 00:00:00 2001 From: peteriman Date: Wed, 15 Dec 2021 01:25:07 +0800 Subject: [PATCH] Creates a 1 SIGHUP handler instead I think the issue is that each file appender instance adds a SIGHUP handler, when they could all use the same handler. I'll see if I can work on a fix. _Originally posted by @nomiddlename in https://github.com/log4js-node/log4js-node/issues/852#issuecomment-496316399_ --- lib/appenders/file.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/appenders/file.js b/lib/appenders/file.js index f2c194b4..7e7c03ed 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; }