2011-07-24 19:58:02 +08:00
|
|
|
var layouts = require('../layouts')
|
2011-11-21 12:03:51 +08:00
|
|
|
, path = require('path')
|
2011-07-24 19:58:02 +08:00
|
|
|
, fs = require('fs');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File Appender writing the logs to a text file. Supports rolling of logs by size.
|
|
|
|
*
|
|
|
|
* @param file file log messages will be written to
|
|
|
|
* @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
|
|
|
|
* @param logSize - the maximum size (in bytes) for a log file, if not provided then logs won't be rotated.
|
|
|
|
* @param numBackups - the number of log files to keep after logSize has been reached (default 5)
|
|
|
|
*/
|
2011-11-21 12:03:51 +08:00
|
|
|
function fileAppender (file, layout, logSize, numBackups) {
|
|
|
|
var bytesWritten = 0;
|
|
|
|
file = path.normalize(file);
|
2011-07-24 19:58:02 +08:00
|
|
|
layout = layout || layouts.basicLayout;
|
|
|
|
numBackups = numBackups === undefined ? 5 : numBackups;
|
2011-07-26 16:40:41 +08:00
|
|
|
//there has to be at least one backup if logSize has been specified
|
|
|
|
numBackups = numBackups === 0 ? 1 : numBackups;
|
2011-07-24 19:58:02 +08:00
|
|
|
|
|
|
|
function setupLogRolling () {
|
2011-11-21 12:03:51 +08:00
|
|
|
try {
|
|
|
|
var stat = fs.statSync(file);
|
|
|
|
bytesWritten = stat.size;
|
|
|
|
if (bytesWritten >= logSize) {
|
|
|
|
rollThatLog();
|
2011-07-24 19:58:02 +08:00
|
|
|
}
|
2011-11-21 12:03:51 +08:00
|
|
|
} catch (e) {
|
|
|
|
//file does not exist
|
|
|
|
bytesWritten = 0;
|
|
|
|
}
|
2011-07-24 19:58:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function rollThatLog () {
|
2011-11-21 12:03:51 +08:00
|
|
|
function index(filename) {
|
|
|
|
return parseInt(filename.substring((path.basename(file) + '.').length), 10) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var nameMatcher = new RegExp('^' + path.basename(file));
|
|
|
|
function justTheLogFiles (item) {
|
|
|
|
return nameMatcher.test(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
function byIndex(a, b) {
|
|
|
|
if (index(a) > index(b)) {
|
|
|
|
return 1;
|
|
|
|
} else if (index(a) < index(b) ) {
|
|
|
|
return -1;
|
2011-07-26 16:40:41 +08:00
|
|
|
} else {
|
2011-11-21 12:03:51 +08:00
|
|
|
return 0;
|
2011-07-24 19:58:02 +08:00
|
|
|
}
|
|
|
|
}
|
2011-11-21 12:03:51 +08:00
|
|
|
|
|
|
|
function increaseFileIndex (fileToRename) {
|
|
|
|
var idx = index(fileToRename);
|
|
|
|
if (idx < numBackups) {
|
2011-11-24 05:37:05 +08:00
|
|
|
//on windows, you can get a EEXIST error if you rename a file to an existing file
|
|
|
|
//so, we'll try to delete the file we're renaming to first
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(file + '.' + (idx+1));
|
|
|
|
} catch (e) {
|
|
|
|
//couldn't delete, but that could be because it doesn't exist
|
|
|
|
//try renaming anyway
|
|
|
|
}
|
2011-11-21 12:03:51 +08:00
|
|
|
fs.renameSync(path.join(path.dirname(file), fileToRename), file + '.' + (idx + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//roll the backups (rename file.n to file.n+1, where n <= numBackups)
|
|
|
|
fs.readdirSync(path.dirname(file))
|
|
|
|
.filter(justTheLogFiles)
|
|
|
|
.sort(byIndex)
|
|
|
|
.reverse()
|
|
|
|
.forEach(increaseFileIndex);
|
|
|
|
|
2011-07-26 16:40:41 +08:00
|
|
|
//let's make a new file
|
|
|
|
var newLogFileFD = fs.openSync(file, 'a', 0644)
|
|
|
|
, oldLogFileFD = logFile.fd;
|
|
|
|
logFile.fd = newLogFileFD;
|
|
|
|
fs.close(oldLogFileFD);
|
2011-11-21 12:03:51 +08:00
|
|
|
//reset the counter
|
|
|
|
bytesWritten = 0;
|
2011-07-24 19:58:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function fileExists (filename) {
|
|
|
|
try {
|
|
|
|
fs.statSync(filename);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function openTheStream() {
|
|
|
|
var stream = fs.createWriteStream(file, { flags: 'a', mode: 0644, encoding: 'utf8' });
|
|
|
|
stream.on("open", function() {
|
2011-11-24 05:20:33 +08:00
|
|
|
canWrite = true;
|
|
|
|
flushBuffer();
|
2011-07-24 19:58:02 +08:00
|
|
|
});
|
|
|
|
stream.on("error", function (err) {
|
2011-11-21 12:03:51 +08:00
|
|
|
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
|
2011-07-24 19:58:02 +08:00
|
|
|
});
|
|
|
|
stream.on("drain", function() {
|
2011-11-24 05:20:33 +08:00
|
|
|
canWrite = true;
|
|
|
|
flushBuffer();
|
2011-11-21 12:03:51 +08:00
|
|
|
if (logEventBuffer.length > 0) {
|
|
|
|
writeToLog(logEventBuffer.shift());
|
2011-07-24 19:58:02 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2011-11-24 05:20:33 +08:00
|
|
|
function flushBuffer() {
|
|
|
|
while (logEventBuffer.length > 0 && canWrite) {
|
|
|
|
writeToLog(logEventBuffer.shift());
|
|
|
|
}
|
|
|
|
}
|
2011-07-24 19:58:02 +08:00
|
|
|
|
|
|
|
var logEventBuffer = []
|
2011-11-24 05:20:33 +08:00
|
|
|
, canWrite = false
|
|
|
|
, logFile = openTheStream();
|
2011-07-24 19:58:02 +08:00
|
|
|
|
|
|
|
if (logSize > 0) {
|
|
|
|
setupLogRolling();
|
|
|
|
}
|
|
|
|
|
|
|
|
//close the file on process exit.
|
|
|
|
process.on('exit', function() {
|
2011-11-24 05:20:33 +08:00
|
|
|
flushBuffer();
|
2011-07-24 19:58:02 +08:00
|
|
|
logFile.end();
|
|
|
|
logFile.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
function writeToLog(loggingEvent) {
|
2011-11-21 12:03:51 +08:00
|
|
|
var logMessage = layout(loggingEvent)+'\n';
|
|
|
|
//not entirely accurate, but it'll do.
|
|
|
|
bytesWritten += logMessage.length;
|
2011-11-24 05:20:33 +08:00
|
|
|
canWrite = logFile.write(logMessage, "utf8");
|
2011-11-21 12:03:51 +08:00
|
|
|
if (bytesWritten >= logSize) {
|
|
|
|
rollThatLog();
|
|
|
|
}
|
2011-07-24 19:58:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return function(loggingEvent) {
|
2011-11-21 12:03:51 +08:00
|
|
|
logEventBuffer.push(loggingEvent);
|
2011-11-24 05:20:33 +08:00
|
|
|
flushBuffer();
|
2011-07-24 19:58:02 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function configure(config) {
|
|
|
|
var layout;
|
|
|
|
if (config.layout) {
|
|
|
|
layout = layouts.layout(config.layout.type, config.layout);
|
|
|
|
}
|
2011-11-21 12:03:51 +08:00
|
|
|
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
|
2011-07-24 19:58:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.name = "file";
|
|
|
|
exports.appender = fileAppender;
|
|
|
|
exports.configure = configure;
|