2013-06-04 06:17:36 +08:00
|
|
|
"use strict";
|
2011-07-24 19:58:02 +08:00
|
|
|
var layouts = require('../layouts')
|
2013-08-05 05:21:12 +08:00
|
|
|
, async = require('async')
|
2013-06-04 06:17:36 +08:00
|
|
|
, path = require('path')
|
|
|
|
, fs = require('fs')
|
|
|
|
, streams = require('../streams')
|
|
|
|
, os = require('os')
|
|
|
|
, eol = os.EOL || '\n'
|
|
|
|
, openFiles = [];
|
2011-07-24 19:58:02 +08:00
|
|
|
|
2012-09-25 05:43:37 +08:00
|
|
|
//close open files on process exit.
|
|
|
|
process.on('exit', function() {
|
2013-06-04 06:17:36 +08:00
|
|
|
openFiles.forEach(function (file) {
|
|
|
|
file.end();
|
|
|
|
});
|
2012-09-25 05:43:37 +08:00
|
|
|
});
|
2012-08-09 21:21:30 +08:00
|
|
|
|
2011-07-24 19:58:02 +08:00
|
|
|
/**
|
|
|
|
* File Appender writing the logs to a text file. Supports rolling of logs by size.
|
|
|
|
*
|
|
|
|
* @param file file log messages will be written to
|
2013-06-04 06:17:36 +08:00
|
|
|
* @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-07-24 19:58:02 +08:00
|
|
|
*/
|
2011-11-21 12:03:51 +08:00
|
|
|
function fileAppender (file, layout, logSize, numBackups) {
|
2013-03-20 06:14:27 +08:00
|
|
|
var bytesWritten = 0;
|
|
|
|
file = path.normalize(file);
|
|
|
|
layout = layout || layouts.basicLayout;
|
|
|
|
numBackups = numBackups === undefined ? 5 : numBackups;
|
|
|
|
//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
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
function openTheStream(file, fileSize, numFiles) {
|
|
|
|
var stream;
|
|
|
|
if (fileSize) {
|
|
|
|
stream = new streams.RollingFileStream(
|
|
|
|
file,
|
|
|
|
fileSize,
|
|
|
|
numFiles
|
|
|
|
);
|
|
|
|
} else {
|
2013-06-04 06:17:36 +08:00
|
|
|
stream = fs.createWriteStream(
|
|
|
|
file,
|
|
|
|
{ encoding: "utf8",
|
|
|
|
mode: parseInt('0644', 8),
|
|
|
|
flags: 'a' }
|
|
|
|
);
|
2011-07-24 19:58:02 +08:00
|
|
|
}
|
2013-03-20 06:14:27 +08:00
|
|
|
stream.on("error", function (err) {
|
|
|
|
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
|
|
|
|
});
|
|
|
|
return stream;
|
|
|
|
}
|
2011-07-24 19:58:02 +08:00
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
var logFile = openTheStream(file, logSize, numBackups);
|
|
|
|
|
|
|
|
// push file to the stack of open handlers
|
|
|
|
openFiles.push(logFile);
|
|
|
|
|
|
|
|
return function(loggingEvent) {
|
2014-04-06 07:14:56 +08:00
|
|
|
logFile.write(layout(loggingEvent) + eol, "utf8");
|
2013-03-20 06:14:27 +08:00
|
|
|
};
|
2011-07-24 19:58:02 +08:00
|
|
|
}
|
|
|
|
|
2012-05-31 05:50:01 +08:00
|
|
|
function configure(config, options) {
|
2013-03-20 06:14:27 +08:00
|
|
|
var layout;
|
|
|
|
if (config.layout) {
|
|
|
|
layout = layouts.layout(config.layout.type, config.layout);
|
|
|
|
}
|
2012-05-31 05:50:01 +08:00
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
if (options && options.cwd && !config.absolute) {
|
|
|
|
config.filename = path.join(options.cwd, config.filename);
|
|
|
|
}
|
2012-05-31 05:50:01 +08:00
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
|
2011-07-24 19:58:02 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 05:21:12 +08:00
|
|
|
function shutdown(cb) {
|
|
|
|
async.forEach(openFiles, function(file, done) {
|
|
|
|
if (!file.write(eol, "utf-8")) {
|
|
|
|
file.once('drain', function() {
|
|
|
|
file.end(done);
|
|
|
|
});
|
2014-04-21 15:27:51 +08:00
|
|
|
} else {
|
|
|
|
file.end(done);
|
2013-08-05 05:21:12 +08:00
|
|
|
}
|
|
|
|
}, cb);
|
|
|
|
}
|
|
|
|
|
2011-07-24 19:58:02 +08:00
|
|
|
exports.appender = fileAppender;
|
|
|
|
exports.configure = configure;
|
2013-08-05 05:21:12 +08:00
|
|
|
exports.shutdown = shutdown;
|