8000 Merge pull request #336 from gchauvet/master · chewax/log4js-node@0fc65d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0fc65d3

Browse files
committed
Merge pull request log4js-node#336 from gchauvet/master
Send logs as email attachment
2 parents 1164cc7 + 0936052 commit 0fc65d3

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

lib/appenders/smtp.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@ function sendBuffer() {
3535
headers: {"Hostname": os.hostname()}
3636
};
3737

38-
if (!config.html) {
39-
msg.text = body;
38+
if (true === config.attachment.enable) {
39+
msg[config.html ? "html" : "text"] = config.attachment.message;
40+
msg.attachments = [
41+
{
42+
filename: config.attachment.filename,
43+
contentType: 'text/x-log',
44+
content: body
45+
}
46+
];
4047
} else {
41-
msg.html = body;
48+
msg[config.html ? "html" : "text"] = body;
4249
}
4350

4451
if (config.sender) {
@@ -82,20 +89,28 @@ function scheduleSend() {
8289
* It can either send an email on each event or group several
8390
* logging events gathered during specified interval.
8491
*
85-
* @param config appender configuration data
92+
* @param _config appender configuration data
8693
* config.sendInterval time between log emails (in seconds), if 0
8794
* then every event sends an email
8895
* config.shutdownTimeout time to give up remaining emails (in seconds; defaults to 5).
8996
* @param _layout a function that takes a logevent and returns a string (defaults to basicLayout).
9097
*/
9198
function smtpAppender(_config, _layout) {
9299
config = _config;
100+
101+
if (!config.attachment) {
102+
config.attachment = {};
103+
}
104+
105+
config.attachment.enable = !!config.attachment.enable;
106+
config.attachment.message = config.attachment.message || "See logs as attachment";
107+
config.attachment.filename = config.attachment.filename || "default.log";
93108
layout = _layout || layouts.basicLayout;
94109
subjectLayout = layouts.messagePassThroughLayout;
95110
sendInterval = config.sendInterval * 1000 || 0;
96-
111+
97112
shutdownTimeout = ('shutdownTimeout' in config ? config.shutdownTimeout : 5) * 1000;
98-
113+
99114
return function (loggingEvent) {
100115
unsentCount++;
101116
logEventBuffer.push(loggingEvent);
@@ -118,7 +133,8 @@ function configure(_config) {
118133
function shutdown(cb) {
119134
if (shutdownTimeout > 0) {
120135
setTimeout(function () {
121-
if(sendTimer) clearTimeout(sendTimer);
136+
if (sendTimer)
137+
clearTimeout(sendTimer);
122138
sendBuffer();
123139
}, shutdownTimeout);
124140
}

test/smtpAppender-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,5 +284,32 @@ vows.describe('log4js smtpAppender').addBatch({
284284
'message should contain proper data': function (result) {
285285
checkMessages(result);
286286
}
287+
},
288+
'attachment config': {
289+
topic: function () {
290+
var setup = setupLogging('attachment config', {
291+
recipients: 'recipient@domain.com',
292+
attachment: {
293+
enable: true
294+
},
295+
SMTP: {
296+
port: 25,
297+
auth: {
298+
user: 'user@domain.com'
299+
}
300+
}
301+
});
302+
setup.logger.info('Log event #1');
303+
return setup;
304+
},
305+
'message should contain proper data': function (result) {
306+
assert.equal(result.results.length, 1);
307+
assert.equal(result.results[0].attachments.length, 1);
308+
var attachment = result.results[0].attachments[0];
309+
assert.equal(result.results[0].text, "See logs as attachment");
310+
assert.equal(attachment.filename, "default.log");
311+
assert.equal(attachment.contentType, "text/x-log");
312+
assert.ok(new RegExp('.+Log event #' + 1 + '\n$').test(attachment.content));
313+
}
287314
}
288315
}).export(module);

0 commit comments

Comments
 (0)
0