Merge pull request #336 from gchauvet/master

Send logs as email attachment
This commit is contained in:
Gareth Jones 2016-01-07 08:09:53 +11:00
commit 0fc65d38eb
2 changed files with 50 additions and 7 deletions

View File

@ -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);
}

View File

@ -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);