diff --git a/lib/appenders/smtp.js b/lib/appenders/smtp.js index 557bc71..8299177 100644 --- a/lib/appenders/smtp.js +++ b/lib/appenders/smtp.js @@ -35,10 +35,17 @@ function sendBuffer() { headers: {"Hostname": os.hostname()} }; - if (!config.html) { - msg.text = body; + if (true === config.attachment.enable) { + msg[config.html ? "html" : "text"] = config.attachment.message; + msg.attachments = [ + { + filename: config.attachment.filename, + contentType: 'text/x-log', + content: body + } + ]; } else { - msg.html = body; + msg[config.html ? "html" : "text"] = body; } if (config.sender) { @@ -82,7 +89,7 @@ function scheduleSend() { * It can either send an email on each event or group several * logging events gathered during specified interval. * - * @param config appender configuration data + * @param _config appender configuration data * config.sendInterval time between log emails (in seconds), if 0 * then every event sends an email * config.shutdownTimeout time to give up remaining emails (in seconds; defaults to 5). @@ -90,12 +97,20 @@ function scheduleSend() { */ function smtpAppender(_config, _layout) { config = _config; + + if (!config.attachment) { + config.attachment = {}; + } + + config.attachment.enable = !!config.attachment.enable; + config.attachment.message = config.attachment.message || "See logs as attachment"; + config.attachment.filename = config.attachment.filename || "default.log"; layout = _layout || layouts.basicLayout; subjectLayout = layouts.messagePassThroughLayout; sendInterval = config.sendInterval * 1000 || 0; - + shutdownTimeout = ('shutdownTimeout' in config ? config.shutdownTimeout : 5) * 1000; - + return function (loggingEvent) { unsentCount++; logEventBuffer.push(loggingEvent); @@ -118,7 +133,8 @@ function configure(_config) { function shutdown(cb) { if (shutdownTimeout > 0) { setTimeout(function () { - if(sendTimer) clearTimeout(sendTimer); + if (sendTimer) + clearTimeout(sendTimer); sendBuffer(); }, shutdownTimeout); } diff --git a/test/smtpAppender-test.js b/test/smtpAppender-test.js index 1cc2d08..3fa1430 100644 --- a/test/smtpAppender-test.js +++ b/test/smtpAppender-test.js @@ -284,5 +284,32 @@ vows.describe('log4js smtpAppender').addBatch({ 'message should contain proper data': function (result) { checkMessages(result); } + }, + 'attachment config': { + topic: function () { + var setup = setupLogging('attachment config', { + recipients: 'recipient@domain.com', + attachment: { + enable: true + }, + SMTP: { + port: 25, + auth: { + user: 'user@domain.com' + } + } + }); + setup.logger.info('Log event #1'); + return setup; + }, + 'message should contain proper data': function (result) { + assert.equal(result.results.length, 1); + assert.equal(result.results[0].attachments.length, 1); + var attachment = result.results[0].attachments[0]; + assert.equal(result.results[0].text, "See logs as attachment"); + assert.equal(attachment.filename, "default.log"); + assert.equal(attachment.contentType, "text/x-log"); + assert.ok(new RegExp('.+Log event #' + 1 + '\n$').test(attachment.content)); + } } }).export(module);