Skip to content

Commit

Permalink
Creates a 1 SIGHUP handler instead
Browse files Browse the repository at this point in the history
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 #852 (comment)
  • Loading branch information
lamweili committed Dec 14, 2021
1 parent f8d46a9 commit d991f3b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/appenders/file.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit d991f3b

Please sign in to comment.