Changes to handle drain events not fired on write in linux & windows - should fix issue #44

This commit is contained in:
Gareth Jones 2011-11-24 08:20:33 +11:00
parent a3a0c55322
commit cea3dc97d1

View File

@ -86,14 +86,15 @@ function fileAppender (file, layout, logSize, numBackups) {
function openTheStream() {
var stream = fs.createWriteStream(file, { flags: 'a', mode: 0644, encoding: 'utf8' });
stream.on("open", function() {
if (logEventBuffer.length > 0) {
writeToLog(logEventBuffer.shift());
}
canWrite = true;
flushBuffer();
});
stream.on("error", function (err) {
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
});
stream.on("drain", function() {
canWrite = true;
flushBuffer();
if (logEventBuffer.length > 0) {
writeToLog(logEventBuffer.shift());
}
@ -101,9 +102,15 @@ function fileAppender (file, layout, logSize, numBackups) {
return stream;
}
function flushBuffer() {
while (logEventBuffer.length > 0 && canWrite) {
writeToLog(logEventBuffer.shift());
}
}
var logEventBuffer = []
, logFile = openTheStream();
, canWrite = false
, logFile = openTheStream();
if (logSize > 0) {
setupLogRolling();
@ -111,6 +118,7 @@ function fileAppender (file, layout, logSize, numBackups) {
//close the file on process exit.
process.on('exit', function() {
flushBuffer();
logFile.end();
logFile.destroy();
});
@ -119,7 +127,7 @@ function fileAppender (file, layout, logSize, numBackups) {
var logMessage = layout(loggingEvent)+'\n';
//not entirely accurate, but it'll do.
bytesWritten += logMessage.length;
logFile.write(logMessage, "utf8");
canWrite = logFile.write(logMessage, "utf8");
if (bytesWritten >= logSize) {
rollThatLog();
}
@ -127,6 +135,7 @@ function fileAppender (file, layout, logSize, numBackups) {
return function(loggingEvent) {
logEventBuffer.push(loggingEvent);
flushBuffer();
};
}