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

View File

@ -1,27 +1,23 @@
var layouts = require("../layouts"), 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. * 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 config appender configuration data
* @param sender sender of all emails (defaults to SMTP user)
* @param subject subject of all email messages (defaults to first event's message)
* @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).
* @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 * 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) { function smtpAppender(config, layout) {
sender = sender || smtpConfig.user;
layout = layout || layouts.basicLayout; layout = layout || layouts.basicLayout;
subjectLayout = layouts.messagePassThroughLayout; var subjectLayout = layouts.messagePassThroughLayout;
mailer.SMTP = smtpConfig; var sendInterval = config.sendInterval*1000 || 0;
sendInterval = sendInterval*1000 || 0;
var logEventBuffer = []; var logEventBuffer = [];
var sendTimer; var sendTimer;
var transport = mailer.createTransport(config.transport, config[config.transport]);
function sendBuffer() { function sendBuffer() {
if (logEventBuffer.length == 0) if (logEventBuffer.length == 0)
@ -34,12 +30,14 @@ function smtpAppender(recipients, sender, subject, layout, smtpConfig, sendInter
} }
var msg = { var msg = {
sender: sender, to: config.recipients,
to: recipients, subject: config.subject || subjectLayout(firstEvent),
subject: subject || subjectLayout(firstEvent), text: body,
body: 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) { if (error) {
console.error("log4js.smtpAppender - Error happened ", error); console.error("log4js.smtpAppender - Error happened ", error);
} }
@ -68,8 +66,10 @@ function configure(config) {
if (config.layout) { if (config.layout) {
layout = layouts.layout(config.layout.type, 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.appender = smtpAppender;
exports.configure = configure; exports.configure = configure;

View File

@ -7,11 +7,16 @@ function setupLogging(category, options) {
var msgs = []; var msgs = [];
var fakeMailer = { var fakeMailer = {
send_mail: function (msg, callback) { createTransport: function (name, options) {
return {
config: options,
sendMail: function (msg, callback) {
msgs.push(msg); msgs.push(msg);
callback(null, true); callback(null, true);
} }
}; };
}
};
var smtpModule = sandbox.require('../lib/appenders/smtp', { var smtpModule = sandbox.require('../lib/appenders/smtp', {
requires: { requires: {
@ -30,10 +35,10 @@ function setupLogging(category, options) {
function checkMessages (result, sender, subject) { function checkMessages (result, sender, subject) {
for (var i = 0; i < result.results.length; ++i) { for (var i = 0; i < result.results.length; ++i) {
assert.equal(result.results[i].sender, sender ? sender : result.mailer.SMTP.user); assert.equal(result.results[i].from, sender);
assert.equal(result.results[i].to, 'recipient@domain.com'); assert.equal(result.results[i].to, 'recipient@domain.com');
assert.equal(result.results[i].subject, subject ? subject : 'Log event #' + (i+1)); assert.equal(result.results[i].subject, subject ? subject : 'Log event #' + (i+1));
assert.ok(new RegExp('.+Log event #' + (i+1) + '\n$').test(result.results[i].body)); assert.ok(new RegExp('.+Log event #' + (i+1) + '\n$').test(result.results[i].text));
} }
} }
@ -43,19 +48,17 @@ vows.describe('log4js smtpAppender').addBatch({
topic: function() { topic: function() {
var setup = setupLogging('minimal config', { var setup = setupLogging('minimal config', {
recipients: 'recipient@domain.com', recipients: 'recipient@domain.com',
smtp: { transport: "SMTP",
SMTP: {
port: 25, port: 25,
auth: {
user: 'user@domain.com' user: 'user@domain.com'
} }
}
}); });
setup.logger.info('Log event #1'); setup.logger.info('Log event #1');
return setup; return setup;
}, },
'mailer should be configured properly': function (result) {
assert.ok(result.mailer.SMTP);
assert.equal(result.mailer.SMTP.port, 25);
assert.equal(result.mailer.SMTP.user, 'user@domain.com');
},
'there should be one message only': function (result) { 'there should be one message only': function (result) {
assert.equal(result.results.length, 1); assert.equal(result.results.length, 1);
}, },
@ -69,19 +72,17 @@ vows.describe('log4js smtpAppender').addBatch({
recipients: 'recipient@domain.com', recipients: 'recipient@domain.com',
sender: 'sender@domain.com', sender: 'sender@domain.com',
subject: 'This is subject', subject: 'This is subject',
smtp: { transport: "SMTP",
SMTP: {
port: 25, port: 25,
auth: {
user: 'user@domain.com' user: 'user@domain.com'
} }
}
}); });
setup.logger.info('Log event #1'); setup.logger.info('Log event #1');
return setup; return setup;
}, },
'mailer should be configured properly': function (result) {
assert.ok(result.mailer.SMTP);
assert.equal(result.mailer.SMTP.port, 25);
assert.equal(result.mailer.SMTP.user, 'user@domain.com');
},
'there should be one message only': function (result) { 'there should be one message only': function (result) {
assert.equal(result.results.length, 1); assert.equal(result.results.length, 1);
}, },
@ -94,10 +95,13 @@ vows.describe('log4js smtpAppender').addBatch({
var self = this; var self = this;
var setup = setupLogging('separate email for each event', { var setup = setupLogging('separate email for each event', {
recipients: 'recipient@domain.com', recipients: 'recipient@domain.com',
smtp: { transport: "SMTP",
SMTP: {
port: 25, port: 25,
auth: {
user: 'user@domain.com' user: 'user@domain.com'
} }
}
}); });
setTimeout(function () { setTimeout(function () {
setup.logger.info('Log event #1'); setup.logger.info('Log event #1');
@ -125,10 +129,13 @@ vows.describe('log4js smtpAppender').addBatch({
var setup = setupLogging('multiple events in one email', { var setup = setupLogging('multiple events in one email', {
recipients: 'recipient@domain.com', recipients: 'recipient@domain.com',
sendInterval: 1, sendInterval: 1,
smtp: { transport: "SMTP",
SMTP: {
port: 25, port: 25,
auth: {
user: 'user@domain.com' user: 'user@domain.com'
} }
}
}); });
setTimeout(function () { setTimeout(function () {
setup.logger.info('Log event #1'); setup.logger.info('Log event #1');
@ -147,16 +154,15 @@ vows.describe('log4js smtpAppender').addBatch({
assert.equal(result.results.length, 2); assert.equal(result.results.length, 2);
}, },
'messages should contain proper data': function (result) { 'messages should contain proper data': function (result) {
assert.equal(result.results[0].sender, result.mailer.SMTP.user);
assert.equal(result.results[0].to, 'recipient@domain.com'); assert.equal(result.results[0].to, 'recipient@domain.com');
assert.equal(result.results[0].subject, 'Log event #1'); assert.equal(result.results[0].subject, 'Log event #1');
assert.equal(result.results[0].body.match(new RegExp('.+Log event #[1-2]$', 'gm')).length, 2); assert.equal(result.results[0].text.match(new RegExp('.+Log event #[1-2]$', 'gm')).length, 2);
assert.equal(result.results[1].sender, result.mailer.SMTP.user);
assert.equal(result.results[1].to, 'recipient@domain.com'); assert.equal(result.results[1].to, 'recipient@domain.com');
assert.equal(result.results[1].subject, 'Log event #3'); assert.equal(result.results[1].subject, 'Log event #3');
assert.ok(new RegExp('.+Log event #3\n$').test(result.results[1].body)); assert.ok(new RegExp('.+Log event #3\n$').test(result.results[1].text));
} }
} }
}).export(module); }).export(module);