This commit is contained in:
Gareth Jones
2012-07-04 09:11:07 +10:00
2 changed files with 152 additions and 146 deletions

View File

@@ -1,59 +1,57 @@
var layouts = require("../layouts"),
mailer = require("nodemailer");
mailer = require("nodemailer"),
os = require('os');
/**
* SMTP Appender. Sends logging events using SMTP protocol.
* SMTP Appender. Sends logging events using SMTP protocol.
* It can either send an email on each event or group several logging events gathered during specified interval.
*
* @param recipients comma separated list of email recipients
* @param sender sender of all emails (defaults to SMTP user)
* @param subject subject of all email messages (defaults to first event's message)
* @param config appender configuration data
* @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
* @param smtpConfig SMTP configuration for 'nodemailer'
* @param sendInterval the time in seconds between sending attempts (defaults to 0);
* all events are buffered and sent in one email during this time; if 0 than every event sends an email
*/
function smtpAppender(recipients, sender, subject, layout, smtpConfig, sendInterval) {
sender = sender || smtpConfig.user;
function smtpAppender(config, layout) {
layout = layout || layouts.basicLayout;
subjectLayout = layouts.messagePassThroughLayout;
mailer.SMTP = smtpConfig;
sendInterval = sendInterval*1000 || 0;
var subjectLayout = layouts.messagePassThroughLayout;
var sendInterval = config.sendInterval*1000 || 0;
var logEventBuffer = [];
var sendTimer;
var transport = mailer.createTransport(config.transport, config[config.transport]);
function sendBuffer() {
if (logEventBuffer.length == 0)
return;
var firstEvent = logEventBuffer[0];
var body = "";
while (logEventBuffer.length > 0) {
body += layout(logEventBuffer.shift()) + "\n";
}
var msg = {
sender: sender,
to: recipients,
subject: subject || subjectLayout(firstEvent),
body: body
to: config.recipients,
subject: config.subject || subjectLayout(firstEvent),
text: body,
headers: {"Hostname": os.hostname()}
};
mailer.send_mail(msg, function(error, success) {
if (config.sender)
msg.from = config.sender;
transport.sendMail(msg, function(error, success) {
if (error) {
console.error("log4js.smtpAppender - Error happened ", error);
}
});
}
function scheduleSend() {
if (!sendTimer)
sendTimer = setTimeout(function() {
sendTimer = null;
sendTimer = null;
sendBuffer();
}, sendInterval);
}
return function(loggingEvent) {
logEventBuffer.push(loggingEvent);
if (sendInterval > 0)
@@ -68,8 +66,10 @@ function configure(config) {
if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout);
}
return smtpAppender(config.recipients, config.sender, config.subject, layout, config.smtp, config.sendInterval);
return smtpAppender(config, layout);
}
exports.name = "smtp";
exports.appender = smtpAppender;
exports.configure = configure;