trying out a shutdown function
This commit is contained in:
parent
1e17f88ded
commit
ce8b6b06b9
@ -1,5 +1,6 @@
|
||||
"use strict";
|
||||
var layouts = require('../layouts')
|
||||
, async = require('async')
|
||||
, path = require('path')
|
||||
, fs = require('fs')
|
||||
, streams = require('../streams')
|
||||
@ -61,7 +62,12 @@ function fileAppender (file, layout, logSize, numBackups) {
|
||||
openFiles.push(logFile);
|
||||
|
||||
return function(loggingEvent) {
|
||||
logFile.write(layout(loggingEvent) + eol, "utf8");
|
||||
logFile.write(layout(loggingEvent) + eol, "utf8",
|
||||
function() {
|
||||
// just here to keep the event loop
|
||||
// open - doesn't work.
|
||||
return;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@ -78,5 +84,16 @@ function configure(config, options) {
|
||||
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
|
||||
}
|
||||
|
||||
function shutdown(cb) {
|
||||
async.forEach(openFiles, function(file, done) {
|
||||
if (!file.write(eol, "utf-8")) {
|
||||
file.once('drain', function() {
|
||||
file.end(done);
|
||||
});
|
||||
}
|
||||
}, cb);
|
||||
}
|
||||
|
||||
exports.appender = fileAppender;
|
||||
exports.configure = configure;
|
||||
exports.shutdown = shutdown;
|
||||
|
@ -44,6 +44,7 @@
|
||||
* Website: http://log4js.berlios.de
|
||||
*/
|
||||
var events = require('events')
|
||||
, async = require('async')
|
||||
, fs = require('fs')
|
||||
, path = require('path')
|
||||
, util = require('util')
|
||||
@ -301,6 +302,24 @@ function loadAppender(appender) {
|
||||
appenderMakers[appender] = appenderModule.configure.bind(appenderModule);
|
||||
}
|
||||
|
||||
function shutdown(cb) {
|
||||
var allAppenders = Object.keys(appenders).reduce(
|
||||
function(accum, category) {
|
||||
return accum.concat(appenders[category]);
|
||||
}, []);
|
||||
async.forEach(
|
||||
allAppenders,
|
||||
function(appender, done) {
|
||||
if (appender.shutdown) {
|
||||
appender.shutdown(done);
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
},
|
||||
cb
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getLogger: getLogger,
|
||||
getDefaultLogger: getDefaultLogger,
|
||||
@ -309,6 +328,7 @@ module.exports = {
|
||||
loadAppender: loadAppender,
|
||||
clearAppenders: clearAppenders,
|
||||
configure: configure,
|
||||
shutdown: shutdown,
|
||||
|
||||
replaceConsole: replaceConsole,
|
||||
restoreConsole: restoreConsole,
|
||||
|
Loading…
Reference in New Issue
Block a user