trying out a shutdown function

This commit is contained in:
Gareth Jones 2013-08-05 07:21:12 +10:00
parent 1e17f88ded
commit ce8b6b06b9
2 changed files with 38 additions and 1 deletions

View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
var layouts = require('../layouts') var layouts = require('../layouts')
, async = require('async')
, path = require('path') , path = require('path')
, fs = require('fs') , fs = require('fs')
, streams = require('../streams') , streams = require('../streams')
@ -61,7 +62,12 @@ function fileAppender (file, layout, logSize, numBackups) {
openFiles.push(logFile); openFiles.push(logFile);
return function(loggingEvent) { 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); 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.appender = fileAppender;
exports.configure = configure; exports.configure = configure;
exports.shutdown = shutdown;

View File

@ -44,6 +44,7 @@
* Website: http://log4js.berlios.de * Website: http://log4js.berlios.de
*/ */
var events = require('events') var events = require('events')
, async = require('async')
, fs = require('fs') , fs = require('fs')
, path = require('path') , path = require('path')
, util = require('util') , util = require('util')
@ -301,6 +302,24 @@ function loadAppender(appender) {
appenderMakers[appender] = appenderModule.configure.bind(appenderModule); 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 = { module.exports = {
getLogger: getLogger, getLogger: getLogger,
getDefaultLogger: getDefaultLogger, getDefaultLogger: getDefaultLogger,
@ -309,6 +328,7 @@ module.exports = {
loadAppender: loadAppender, loadAppender: loadAppender,
clearAppenders: clearAppenders, clearAppenders: clearAppenders,
configure: configure, configure: configure,
shutdown: shutdown,
replaceConsole: replaceConsole, replaceConsole: replaceConsole,
restoreConsole: restoreConsole, restoreConsole: restoreConsole,