Merge pull request #277 from hasegawa-jun/shutdown-smtp-appender
Implemented shutdown function for SMTP appender
This commit is contained in:
commit
eb87ccb78d
@ -1,7 +1,9 @@
|
|||||||
"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
|
||||||
|
, shutdownTimeout;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SMTP Appender. Sends logging events using SMTP protocol.
|
* SMTP Appender. Sends logging events using SMTP protocol.
|
||||||
@ -11,6 +13,7 @@ var layouts = require("../layouts")
|
|||||||
* @param config appender configuration data
|
* @param config appender configuration data
|
||||||
* config.sendInterval time between log emails (in seconds), if 0
|
* config.sendInterval time between log emails (in seconds), if 0
|
||||||
* then every event sends an email
|
* then every event sends an email
|
||||||
|
* config.shutdownTimeout time to give up remaining emails (in seconds; defaults to 5).
|
||||||
* @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
|
* @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
|
||||||
*/
|
*/
|
||||||
function smtpAppender(config, layout) {
|
function smtpAppender(config, layout) {
|
||||||
@ -21,12 +24,15 @@ function smtpAppender(config, layout) {
|
|||||||
var logEventBuffer = [];
|
var logEventBuffer = [];
|
||||||
var sendTimer;
|
var sendTimer;
|
||||||
|
|
||||||
|
shutdownTimeout = ('shutdownTimeout' in config ? config.shutdownTimeout : 5) * 1000;
|
||||||
|
|
||||||
function sendBuffer() {
|
function sendBuffer() {
|
||||||
if (logEventBuffer.length > 0) {
|
if (logEventBuffer.length > 0) {
|
||||||
|
|
||||||
var transport = mailer.createTransport(config.SMTP);
|
var transport = mailer.createTransport(config.SMTP);
|
||||||
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(), config.timezoneOffset) + "\n";
|
body += layout(logEventBuffer.shift(), config.timezoneOffset) + "\n";
|
||||||
}
|
}
|
||||||
@ -51,6 +57,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 +72,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 +90,19 @@ function configure(config) {
|
|||||||
return smtpAppender(config, layout);
|
return smtpAppender(config, layout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function shutdown(cb) {
|
||||||
|
if (shutdownTimeout > 0) {
|
||||||
|
setTimeout(function() { unsentCount = 0; }, shutdownTimeout);
|
||||||
|
}
|
||||||
|
async.whilst(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;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user