|
1 | 1 | "use strict";
|
2 |
| -var layouts = require("../layouts") |
3 |
| -, mailer = require("nodemailer") |
4 |
| -, os = require('os') |
5 |
| -, async = require('async') |
6 |
| -, unsentCount = 0 |
7 |
| -, shutdownTimeout; |
8 | 2 |
|
9 |
| -/** |
10 |
| -* SMTP Appender. Sends logging events using SMTP protocol. |
11 |
| -* It can either send an email on each event or group several |
12 |
| -* logging events gathered during specified interval. |
13 |
| -* |
14 |
| -* @param config appender configuration data |
15 |
| -* config.sendInterval time between log emails (in seconds), if 0 |
16 |
| -* then every event sends an email |
17 |
| -* config.shutdownTimeout time to give up remaining emails (in seconds; defaults to 5). |
18 |
| -* @param layout a function that takes a logevent and returns a string (defaults to basicLayout). |
19 |
| -*/ |
20 |
| -function smtpAppender(config, layout) { |
21 |
| - layout = layout || layouts.basicLayout; |
22 |
| - var subjectLayout = layouts.messagePassThroughLayout; |
23 |
| - var sendInterval = config.sendInterval*1000 || 0; |
24 |
| - |
25 |
| - var logEventBuffer = []; |
26 |
| - var sendTimer; |
27 |
| - |
28 |
| - shutdownTimeout = ('shutdownTimeout' in config ? config.shutdownTimeout : 5) * 1000; |
29 |
| - |
30 |
| - function sendBuffer() { |
| 3 | +var layouts = require("../layouts"); |
| 4 | +var mailer = require("nodemailer"); |
| 5 | +var os = require('os'); |
| 6 | +var async = require('async'); |
| 7 | + |
| 8 | +var logEventBuffer = []; |
| 9 | +var subjectLayout; |
| 10 | +var layout; |
| 11 | + |
| 12 | +var unsentCount = 0; |
| 13 | +var shutdownTimeout; |
| 14 | + |
| 15 | +var sendInterval; |
| 16 | +var sendTimer; |
| 17 | + |
| 18 | +var config; |
| 19 | + |
| 20 | +function sendBuffer() { |
31 | 21 | if (logEventBuffer.length > 0) {
|
32 |
| - |
33 |
| - var transportOpts = getTransportOptions(config); |
34 |
| - var transport = mailer.createTransport(transportOpts); |
35 |
| - var firstEvent = logEventBuffer[0]; |
36 |
| - var body = ""; |
37 |
| - var count = logEventBuffer.length; |
38 |
| - while (logEventBuffer.length > 0) { |
39 |
| - body += layout(logEventBuffer.shift(), config.timezoneOffset) + "\n"; |
40 |
| - } |
41 |
| - |
42 |
| - var msg = { |
43 |
| - to: config.recipients, |
44 |
| - subject: config.subject || subjectLayout(firstEvent), |
45 |
| - headers: { "Hostname": os.hostname() } |
46 |
| - }; |
47 |
| - |
48 |
| - if (!config.html) { |
49 |
| - msg.text = body; |
50 |
| - } else { |
51 |
| - msg.html = body; |
52 |
| - } |
53 |
| - |
54 |
| - if (config.sender) { |
55 |
| - msg.from = config.sender; |
56 |
| - } |
57 |
| - transport.sendMail(msg, function(error, success) { |
58 |
| - if (error) { |
59 |
| - console.error("log4js.smtpAppender - Error happened", error); |
| 22 | + |
| 23 | + var transportOpts = getTransportOptions(config); |
| 24 | + var transport = mailer.createTransport(transportOpts); |
| 25 | + var firstEvent = logEventBuffer[0]; |
| 26 | + var body = ""; |
| 27 | + var count = logEventBuffer.length; |
| 28 | + while (logEventBuffer.length > 0) { |
| 29 | + body += layout(logEventBuffer.shift(), config.timezoneOffset) + "\n"; |
| 30 | + } |
| 31 | + |
| 32 | + var msg = { |
| 33 | + to: config.recipients, |
| 34 | + subject: config.subject || subjectLayout(firstEvent), |
| 35 | + headers: {"Hostname": os.hostname()} |
| 36 | + }; |
| 37 | + |
| 38 | + if (!config.html) { |
| 39 | + msg.text = body; |
| 40 | + } else { |
| 41 | + msg.html = body; |
| 42 | + } |
| 43 | + |
| 44 | + if (config.sender) { |
| 45 | + msg.from = config.sender; |
60 | 46 | }
|
61 |
| - transport.close(); |
62 |
| - unsentCount -= count; |
63 |
| - }); |
| 47 | + transport.sendMail(msg, function (error) { |
| 48 | + if (error) { |
| 49 | + console.error("log4js.smtpAppender - Error happened", error); |
| 50 | + } |
| 51 | + transport.close(); |
| 52 | + unsentCount -= count; |
| 53 | + }); |
64 | 54 | }
|
65 |
| - } |
66 |
| - |
67 |
| - function scheduleSend() { |
68 |
| - if (!sendTimer) { |
69 |
| - sendTimer = setTimeout(function() { |
70 |
| - sendTimer = null; |
71 |
| - sendBuffer(); |
72 |
| - }, sendInterval); |
| 55 | +} |
| 56 | + |
| 57 | +function getTransportOptions() { |
| 58 | + var transportOpts = null; |
| 59 | + if (config.SMTP) { |
| 60 | + transportOpts = config.SMTP; |
| 61 | + } else if (config.transport) { |
| 62 | + var plugin = config.transport.plugin || 'smtp'; |
| 63 | + var transportModule = 'nodemailer-' + plugin + '-transport'; |
| 64 | + var transporter = require(transportModule); |
| 65 | + transportOpts = transporter(config.transport.options); |
73 | 66 | }
|
74 |
| - } |
75 |
| - |
76 |
| - function getTransportOptions(config) { |
77 |
| - var transportOpts = null; |
78 |
| - if( config.SMTP ) { |
79 |
| - transportOpts = config.SMTP; |
80 |
| - } else if( config.transport ) { |
81 |
| - var plugin = config.transport.plugin || 'smtp'; |
82 |
| - var transportModule = 'nodemailer-' + plugin + '-transport'; |
83 |
| - var transporter = require( transportModule ); |
84 |
| - transportOpts = transporter( config.transport.options ); |
85 |
| - } |
86 | 67 |
|
87 |
| - return transportOpts; |
88 |
| - } |
89 |
| - |
90 |
| - return function(loggingEvent) { |
91 |
| - unsentCount++; |
92 |
| - logEventBuffer.push(loggingEvent); |
93 |
| - if (sendInterval > 0) { |
94 |
| - scheduleSend(); |
95 |
| - } else { |
96 |
| - sendBuffer(); |
| 68 | + return transportOpts; |
| 69 | +} |
| 70 | + |
| 71 | +function scheduleSend() { |
| 72 | + if (!sendTimer) { |
| 73 | + sendTimer = setTimeout(function () { |
| 74 | + sendTimer = null; |
| 75 | + sendBuffer(); |
| 76 | + }, sendInterval); |
97 | 77 | }
|
98 |
| - }; |
99 | 78 | }
|
100 | 79 |
|
101 |
| -function configure(config) { |
102 |
| - var layout; |
103 |
| - if (config.layout) { |
104 |
| - layout = layouts.layout(config.layout.type, config.layout); |
105 |
| - } |
106 |
| - return smtpAppender(config, layout); |
| 80 | +/** |
| 81 | + * SMTP Appender. Sends logging events using SMTP protocol. |
| 82 | + * It can either send an email on each event or group several |
| 83 | + * logging events gathered during specified interval. |
| 84 | + * |
| 85 | + * @param config appender configuration data |
| 86 | + * config.sendInterval time between log emails (in seconds), if 0 |
| 87 | + * then every event sends an email |
| 88 | + * config.shutdownTimeout time to give up remaining emails (in seconds; defaults to 5). |
| 89 | + * @param _layout a function that takes a logevent and returns a string (defaults to basicLayout). |
| 90 | + */ |
| 91 | +function smtpAppender(_config, _layout) { |
| 92 | + config = _config; |
| 93 | + layout = _layout || layouts.basicLayout; |
| 94 | + subjectLayout = layouts.messagePassThroughLayout; |
| 95 | + sendInterval = config.sendInterval * 1000 || 0; |
| 96 | + |
| 97 | + shutdownTimeout = ('shutdownTimeout' in config ? config.shutdownTimeout : 5) * 1000; |
| 98 | + |
| 99 | + return function (loggingEvent) { |
| 100 | + unsentCount++; |
| 101 | + logEventBuffer.push(loggingEvent); |
| 102 | + if (sendInterval > 0) { |
| 103 | + scheduleSend(); |
| 104 | + } else { |
| 105 | + sendBuffer(); |
| 106 | + } |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +function configure(_config) { |
| 111 | + config = _config; |
| 112 | + if (_config.layout) { |
| 113 | + layout = layouts.layout(_config.layout.type, _config.layout); |
| 114 | + } |
| 115 | + return smtpAppender(_config, layout); |
107 | 116 | }
|
108 | 117 |
|
109 | 118 | function shutdown(cb) {
|
110 |
| - if (shutdownTimeout > 0) { |
111 |
| - setTimeout(function() { unsentCount = 0; }, shutdownTimeout); |
112 |
| - } |
113 |
| - async.whilst(function() { |
114 |
| - return unsentCount > 0; |
115 |
| - }, function(done) { |
116 |
| - setTimeout(done, 100); |
117 |
| - }, cb); |
| 119 | + if (shutdownTimeout > 0) { |
| 120 | + setTimeout(function () { |
| 121 | + if(sendTimer) clearTimeout(sendTimer); |
| 122 | + sendBuffer(); |
| 123 | + }, shutdownTimeout); |
| 124 | + } |
| 125 | + async.whilst(function () { |
| 126 | + return unsentCount > 0; |
| 127 | + }, function (done) { |
| 128 | + setTimeout(done, 100); |
| 129 | + }, cb); |
118 | 130 | }
|
119 | 131 |
|
120 | 132 | exports.name = "smtp";
|
|
0 commit comments