smtp.js
4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"use strict";
var layouts = require("../layouts");
var mailer = require("nodemailer");
var os = require('os');
var logEventBuffer = [];
var subjectLayout;
var layout;
var unsentCount = 0;
var shutdownTimeout;
var sendInterval;
var sendTimer;
var config;
function sendBuffer() {
if (logEventBuffer.length > 0) {
var transportOpts = getTransportOptions(config);
var transport = mailer.createTransport(transportOpts);
var firstEvent = logEventBuffer[0];
var body = "";
var count = logEventBuffer.length;
while (logEventBuffer.length > 0) {
body += layout(logEventBuffer.shift(), config.timezoneOffset) + "\n";
}
var msg = {
to: config.recipients,
subject: config.subject || subjectLayout(firstEvent),
headers: {"Hostname": os.hostname()}
};
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[config.html ? "html" : "text"] = body;
}
if (config.sender) {
msg.from = config.sender;
}
transport.sendMail(msg, function (error) {
if (error) {
console.error("log4js.smtpAppender - Error happened", error);
}
transport.close();
unsentCount -= count;
});
}
}
function getTransportOptions() {
var transportOpts = null;
if (config.SMTP) {
transportOpts = config.SMTP;
} else if (config.transport) {
var plugin = config.transport.plugin || 'smtp';
var transportModule = 'nodemailer-' + plugin + '-transport';
var transporter = require(transportModule);
transportOpts = transporter(config.transport.options);
}
return transportOpts;
}
function scheduleSend() {
if (!sendTimer) {
sendTimer = setTimeout(function () {
sendTimer = null;
sendBuffer();
}, sendInterval);
}
}
/**
* 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 _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).
* @param _layout a function that takes a logevent and returns a string (defaults to basicLayout).
*/
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);
if (sendInterval > 0) {
scheduleSend();
} else {
sendBuffer();
}
};
}
function configure(_config) {
config = _config;
if (_config.layout) {
layout = layouts.layout(_config.layout.type, _config.layout);
}
return smtpAppender(_config, layout);
}
function shutdown(cb) {
if (shutdownTimeout > 0) {
setTimeout(function () {
if (sendTimer)
clearTimeout(sendTimer);
sendBuffer();
}, shutdownTimeout);
}
(function checkDone() {
if (unsentCount > 0) {
setTimeout(checkDone, 100);
} else {
cb();
}
})();
}
exports.name = "smtp";
exports.appender = smtpAppender;
exports.configure = configure;
exports.shutdown = shutdown;