got log rolling working, need to fix all the tests
This commit is contained in:
parent
7aa076c278
commit
80474c6881
@ -286,52 +286,44 @@ function consoleAppender (layout) {
|
|||||||
*/
|
*/
|
||||||
function fileAppender (file, layout, logSize, numBackups, filePollInterval) {
|
function fileAppender (file, layout, logSize, numBackups, filePollInterval) {
|
||||||
layout = layout || layouts.basicLayout;
|
layout = layout || layouts.basicLayout;
|
||||||
var logFile = fs.createWriteStream(file, { flags: 'a', mode: 0644, encoding: 'utf8' });
|
numBackups = numBackups || 5;
|
||||||
|
filePollInterval = filePollInterval * 1000 || 30000;
|
||||||
|
|
||||||
if (logSize > 0) {
|
function setupLogRolling () {
|
||||||
setupLogRolling(logFile, file, logSize, numBackups || 5, (filePollInterval * 1000) || 30000);
|
|
||||||
}
|
|
||||||
|
|
||||||
//close the file on process exit.
|
|
||||||
process.on('exit', function() {
|
|
||||||
logFile.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
return function(loggingEvent) {
|
|
||||||
logFile.write(layout(loggingEvent)+'\n');
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function setupLogRolling (logFile, filename, logSize, numBackups, filePollInterval) {
|
|
||||||
fs.watchFile(
|
fs.watchFile(
|
||||||
filename,
|
file,
|
||||||
{
|
{
|
||||||
persistent: false,
|
persistent: false,
|
||||||
interval: filePollInterval
|
interval: filePollInterval
|
||||||
},
|
},
|
||||||
function (curr, prev) {
|
function (curr, prev) {
|
||||||
if (curr.size >= logSize) {
|
if (curr.size >= logSize) {
|
||||||
rollThatLog(logFile, filename, numBackups);
|
rollThatLog();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function rollThatLog (logFile, filename, numBackups) {
|
function rollThatLog () {
|
||||||
//first close the current one.
|
|
||||||
logFile.end();
|
|
||||||
//roll the backups (rename file.n-1 to file.n, where n <= numBackups)
|
//roll the backups (rename file.n-1 to file.n, where n <= numBackups)
|
||||||
for (var i=numBackups; i > 0; i--) {
|
for (var i=numBackups; i > 0; i--) {
|
||||||
if (i > 1) {
|
if (i > 1) {
|
||||||
if (fileExists(filename + '.' + (i-1))) {
|
if (fileExists(file + '.' + (i-1))) {
|
||||||
fs.renameSync(filename+'.'+(i-1), filename+'.'+i);
|
console.debug("Renaming " + file + '.' + (i-1) + ' -> ' + file + '.' + i);
|
||||||
|
fs.renameSync(file+'.'+(i-1), file+'.'+i);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fs.renameSync(filename, filename+'.1');
|
console.debug("Renaming " + file + " -> " + file + ".1");
|
||||||
|
fs.renameSync(file, file+'.1');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//open it up again
|
//let's make a new file
|
||||||
logFile = fs.createWriteStream(filename, { flags: 'a', mode: 0644, encoding: "utf8" });
|
var newLogFileFD = fs.openSync(file, 'a', 0644)
|
||||||
|
, oldLogFileFD = logFile.fd;
|
||||||
|
console.debug("made a new logFile ", newLogFileFD);
|
||||||
|
logFile.fd = newLogFileFD;
|
||||||
|
console.debug("closing old log file fd ", oldLogFileFD);
|
||||||
|
fs.close(oldLogFileFD);
|
||||||
}
|
}
|
||||||
|
|
||||||
function fileExists (filename) {
|
function fileExists (filename) {
|
||||||
@ -343,6 +335,59 @@ function fileExists (filename) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function openTheStream() {
|
||||||
|
console.debug("Creating the write Stream with file ", file);
|
||||||
|
var stream = fs.createWriteStream(file, { flags: 'a', mode: 0644, encoding: 'utf8' });
|
||||||
|
stream.on("open", function() {
|
||||||
|
canWrite = true;
|
||||||
|
console.debug("Stream is open, writing %d buffered events", logEventBuffer.length);
|
||||||
|
while (logEventBuffer.length > 0 && canWrite) {
|
||||||
|
canWrite = writeToLog(logEventBuffer.shift());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
stream.on("error", function (err) {
|
||||||
|
console.error("Error happened ", err);
|
||||||
|
});
|
||||||
|
stream.on("drain", function() {
|
||||||
|
canWrite = true;
|
||||||
|
while (logEventBuffer.length > 0 && canWrite) {
|
||||||
|
canWrite = writeToLog(logEventBuffer.shift());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var logEventBuffer = []
|
||||||
|
, canWrite = false
|
||||||
|
, logFile = openTheStream();
|
||||||
|
|
||||||
|
if (logSize > 0) {
|
||||||
|
setupLogRolling();
|
||||||
|
}
|
||||||
|
|
||||||
|
//close the file on process exit.
|
||||||
|
process.on('exit', function() {
|
||||||
|
console.debug("Buffer contains %d events", logEventBuffer.length);
|
||||||
|
logFile.end();
|
||||||
|
logFile.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
function writeToLog(loggingEvent) {
|
||||||
|
return logFile.write(layout(loggingEvent)+'\n', "utf8");
|
||||||
|
}
|
||||||
|
|
||||||
|
return function(loggingEvent) {
|
||||||
|
//because the log stream is opened asynchronously, we don't want to write
|
||||||
|
//until it is ready.
|
||||||
|
if (canWrite) {
|
||||||
|
canWrite = writeToLog(loggingEvent);
|
||||||
|
} else {
|
||||||
|
logEventBuffer.push(loggingEvent);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function configure (configurationFileOrObject) {
|
function configure (configurationFileOrObject) {
|
||||||
var config = configurationFileOrObject;
|
var config = configurationFileOrObject;
|
||||||
if (typeof(config) === "string") {
|
if (typeof(config) === "string") {
|
||||||
|
Loading…
Reference in New Issue
Block a user