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')
, path = require('path')
, fs = require('fs')
, streams = require('../streams')
, os = require('os')
, eol = os.EOL || '\n';
, path = require('path')
, fs = require('fs')
, streams = require('../streams')
, os = require('os')
, eol = os.EOL || '\n'
, openFiles = [];
var openFiles = [];
var listenerAtttached = false;
//close open files on process exit.
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.
@ -48,16 +53,6 @@ function fileAppender (file, layout, 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() {
openFiles.forEach(function (file) {
file.end();
});
});
}
return function(loggingEvent) {
logFile.write(layout(loggingEvent) + eol, "utf8");
};