Implemented shutdown function for SMTP appender

This commit is contained in:
hasegawa-jun 2015-03-04 09:13:30 +09:00
parent 35067af550
commit adfad9ad20

View File

@ -1,7 +1,8 @@
"use strict"; "use strict";
var layouts = require("../layouts") var layouts = require("../layouts")
, mailer = require("nodemailer") , mailer = require("nodemailer")
, os = require('os'); , os = require('os')
, unsentCount = 0;
/** /**
* SMTP Appender. Sends logging events using SMTP protocol. * SMTP Appender. Sends logging events using SMTP protocol.
@ -27,6 +28,7 @@ function smtpAppender(config, layout) {
var transport = mailer.createTransport(config.transport, config[config.transport]); var transport = mailer.createTransport(config.transport, config[config.transport]);
var firstEvent = logEventBuffer[0]; var firstEvent = logEventBuffer[0];
var body = ""; var body = "";
var count = logEventBuffer.length;
while (logEventBuffer.length > 0) { while (logEventBuffer.length > 0) {
body += layout(logEventBuffer.shift()) + "\n"; body += layout(logEventBuffer.shift()) + "\n";
} }
@ -51,6 +53,7 @@ function smtpAppender(config, layout) {
console.error("log4js.smtpAppender - Error happened", error); console.error("log4js.smtpAppender - Error happened", error);
} }
transport.close(); transport.close();
unsentCount -= count;
}); });
} }
} }
@ -65,6 +68,7 @@ function smtpAppender(config, layout) {
} }
return function(loggingEvent) { return function(loggingEvent) {
unsentCount++;
logEventBuffer.push(loggingEvent); logEventBuffer.push(loggingEvent);
if (sendInterval > 0) { if (sendInterval > 0) {
scheduleSend(); scheduleSend();
@ -82,7 +86,16 @@ function configure(config) {
return smtpAppender(config, layout); return smtpAppender(config, layout);
} }
function shutdown(cb) {
async.until(function() {
return unsentCount === 0;
}, function(done) {
setTimeout(done, 100);
}, cb);
}
exports.name = "smtp"; exports.name = "smtp";
exports.appender = smtpAppender; exports.appender = smtpAppender;
exports.configure = configure; exports.configure = configure;
exports.shutdown = shutdown;