Merge pull request #89 from ixti/master

Fix possible memleak with `exit` event handlers
This commit is contained in:
Gareth Jones 2012-08-13 16:32:08 -07:00
commit c6a0e58409
2 changed files with 32 additions and 3 deletions

View File

@ -5,6 +5,9 @@ var layouts = require('../layouts')
, os = require('os')
, eol = os.EOL || '\n';
var openFiles = [];
var listenerAtttached = false;
/**
* File Appender writing the logs to a text file. Supports rolling of logs by size.
*
@ -42,10 +45,18 @@ function fileAppender (file, layout, logSize, numBackups) {
var logFile = openTheStream(file, logSize, numBackups);
// push file to the stack of open handlers
openFiles.push(logFile);
//close the file on process exit.
if (!listenerAtttached) {
listenerAtttached = true;
process.on('exit', function() {
logFile.end();
openFiles.forEach(function (file) {
file.end();
});
});
}
return function(loggingEvent) {
logFile.write(layout(loggingEvent) + eol, "utf8");

View File

@ -15,6 +15,24 @@ function remove(filename) {
}
vows.describe('log4js fileAppender').addBatch({
'adding multiple fileAppenders': {
topic: function () {
var listenersCount = process.listeners('exit').length
, logger = log4js.getLogger('default-settings')
, count = 5, logfile;
while (count--) {
logfile = path.join(__dirname, '/fa-default-test' + count + '.log');
log4js.addAppender(require('../lib/appenders/file').appender(logfile), 'default-settings');
}
return listenersCount;
},
'does not adds more than one `exit` listeners': function (initialCount) {
assert.ok(process.listeners('exit').length <= initialCount + 1);
}
},
'with default fileAppender settings': {
topic: function() {