2013-06-04 06:17:36 +08:00
|
|
|
"use strict";
|
|
|
|
var streams = require('../streams')
|
|
|
|
, layouts = require('../layouts')
|
2014-04-22 08:05:37 +08:00
|
|
|
, async = require('async')
|
2013-06-04 06:17:36 +08:00
|
|
|
, path = require('path')
|
|
|
|
, os = require('os')
|
|
|
|
, eol = os.EOL || '\n'
|
|
|
|
, openFiles = [];
|
2012-09-25 06:16:59 +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 06:16:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File appender that rolls files according to a date pattern.
|
|
|
|
* @filename base filename.
|
|
|
|
* @pattern the format that will be added to the end of filename when rolling,
|
|
|
|
* also used to check when to roll files - defaults to '.yyyy-MM-dd'
|
|
|
|
* @layout layout function for log messages - defaults to basicLayout
|
2015-03-20 10:51:23 +08:00
|
|
|
* @timezoneOffset optional timezone offset in minutes - defaults to system local
|
2012-09-25 06:16:59 +08:00
|
|
|
*/
|
2015-03-20 10:51:23 +08:00
|
|
|
function appender(filename, pattern, alwaysIncludePattern, layout, timezoneOffset) {
|
2013-06-04 06:17:36 +08:00
|
|
|
layout = layout || layouts.basicLayout;
|
|
|
|
|
|
|
|
var logFile = new streams.DateRollingFileStream(
|
2014-04-22 08:05:37 +08:00
|
|
|
filename,
|
|
|
|
pattern,
|
2013-06-04 06:17:36 +08:00
|
|
|
{ alwaysIncludePattern: alwaysIncludePattern }
|
|
|
|
);
|
|
|
|
openFiles.push(logFile);
|
2014-04-22 08:05:37 +08:00
|
|
|
|
2013-06-04 06:17:36 +08:00
|
|
|
return function(logEvent) {
|
2015-03-20 10:51:23 +08:00
|
|
|
logFile.write(layout(logEvent, timezoneOffset) + eol, "utf8");
|
2013-06-04 06:17:36 +08:00
|
|
|
};
|
2012-09-25 06:16:59 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function configure(config, options) {
|
2013-06-04 06:17:36 +08:00
|
|
|
var layout;
|
2014-04-22 08:05:37 +08:00
|
|
|
|
2013-06-04 06:17:36 +08:00
|
|
|
if (config.layout) {
|
|
|
|
layout = layouts.layout(config.layout.type, config.layout);
|
|
|
|
}
|
2014-04-22 08:05:37 +08:00
|
|
|
|
2013-06-04 06:17:36 +08:00
|
|
|
if (!config.alwaysIncludePattern) {
|
|
|
|
config.alwaysIncludePattern = false;
|
|
|
|
}
|
2014-04-22 08:05:37 +08:00
|
|
|
|
2013-06-04 06:17:36 +08:00
|
|
|
if (options && options.cwd && !config.absolute) {
|
|
|
|
config.filename = path.join(options.cwd, config.filename);
|
|
|
|
}
|
|
|
|
|
2015-03-20 10:51:23 +08:00
|
|
|
return appender(config.filename, config.pattern, config.alwaysIncludePattern, layout, config.timezoneOffset);
|
2012-09-25 06:16:59 +08:00
|
|
|
}
|
|
|
|
|
2014-04-22 08:05:37 +08:00
|
|
|
function shutdown(cb) {
|
2014-08-28 16:04:02 +08:00
|
|
|
async.each(openFiles, function(file, done) {
|
2014-04-22 08:05:37 +08:00
|
|
|
if (!file.write(eol, "utf-8")) {
|
|
|
|
file.once('drain', function() {
|
|
|
|
file.end(done);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
file.end(done);
|
|
|
|
}
|
|
|
|
}, cb);
|
|
|
|
}
|
|
|
|
|
2012-09-25 06:16:59 +08:00
|
|
|
exports.appender = appender;
|
|
|
|
exports.configure = configure;
|
2014-04-22 08:05:37 +08:00
|
|
|
exports.shutdown = shutdown;
|