Refactored where the exit handler gets added

This commit is contained in:
Gareth Jones 2012-09-25 07:43:37 +10:00
parent 185f343e68
commit 9ac61e37f4

View File

@ -1,12 +1,17 @@
var layouts = require('../layouts') var layouts = require('../layouts')
, path = require('path') , path = require('path')
, fs = require('fs') , fs = require('fs')
, streams = require('../streams') , streams = require('../streams')
, os = require('os') , os = require('os')
, eol = os.EOL || '\n'; , eol = os.EOL || '\n'
, openFiles = [];
var openFiles = []; //close open files on process exit.
var listenerAtttached = false; process.on('exit', function() {
openFiles.forEach(function (file) {
file.end();
});
});
/** /**
* File Appender writing the logs to a text file. Supports rolling of logs by size. * File Appender writing the logs to a text file. Supports rolling of logs by size.
@ -48,16 +53,6 @@ function fileAppender (file, layout, logSize, numBackups) {
// push file to the stack of open handlers // push file to the stack of open handlers
openFiles.push(logFile); openFiles.push(logFile);
//close the file on process exit.
if (!listenerAtttached) {
listenerAtttached = true;
process.on('exit', function() {
openFiles.forEach(function (file) {
file.end();
});
});
}
return function(loggingEvent) { return function(loggingEvent) {
logFile.write(layout(loggingEvent) + eol, "utf8"); logFile.write(layout(loggingEvent) + eol, "utf8");
}; };