|
1 | 1 | "use strict";
|
2 | 2 | var layouts = require("../layouts")
|
3 |
| -, mailer = require("nodemailer") |
4 |
| -, os = require('os') |
5 |
| -, async = require('async') |
6 |
| -, unsentCount = 0 |
7 |
| -, shutdownTimeout; |
| 3 | + , mailer = require("nodemailer") |
| 4 | + , os = require('os') |
| 5 | + , async = require('async') |
| 6 | + , unsentCount = 0 |
| 7 | + , shutdownTimeout; |
8 | 8 |
|
9 | 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 |
| -*/ |
| 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 | 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() { |
31 |
| - 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); |
| 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() { |
| 31 | + 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); |
| 60 | + } |
| 61 | + transport.close(); |
| 62 | + unsentCount -= count; |
| 63 | + }); |
60 | 64 | }
|
61 |
| - transport.close(); |
62 |
| - unsentCount -= count; |
63 |
| - }); |
64 | 65 | }
|
65 |
| - } |
66 |
| - |
67 |
| - function scheduleSend() { |
68 |
| - if (!sendTimer) { |
69 |
| - sendTimer = setTimeout(function() { |
70 |
| - sendTimer = null; |
71 |
| - sendBuffer(); |
72 |
| - }, sendInterval); |
| 66 | + |
| 67 | + function scheduleSend() { |
| 68 | + if (!sendTimer) { |
| 69 | + sendTimer = setTimeout(function () { |
| 70 | + sendTimer = null; |
| 71 | + sendBuffer(); |
| 72 | + }, sendInterval); |
| 73 | + } |
73 | 74 | }
|
74 |
| - } |
75 |
| - |
76 |
| - function getTransportOptions(config) { |
| 75 | + |
| 76 | + function getTransportOptions(config) { |
77 | 77 | var transportOpts = null;
|
78 |
| - if( config.SMTP ) { |
| 78 | + if (config.SMTP) { |
79 | 79 | transportOpts = config.SMTP;
|
80 |
| - } else if( config.transport ) { |
| 80 | + } else if (config.transport) { |
81 | 81 | var plugin = config.transport.plugin || 'smtp';
|
82 | 82 | var transportModule = 'nodemailer-' + plugin + '-transport';
|
83 |
| - var transporter = require( transportModule ); |
84 |
| - transportOpts = transporter( config.transport.options ); |
| 83 | + var transporter = require(transportModule); |
| 84 | + transportOpts = transporter(config.transport.options); |
85 | 85 | }
|
86 | 86 |
|
87 | 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(); |
97 | 88 | }
|
98 |
| - }; |
| 89 | + |
| 90 | + return function (loggingEvent) { |
| 91 | + unsentCount++; |
| 92 | + logEventBuffer.push(loggingEvent); |
| 93 | + if (sendInterval > 0) { |
| 94 | + scheduleSend(); |
| 95 | + } else { |
| 96 | + sendBuffer(); |
| 97 | + } |
| 98 | + }; |
99 | 99 | }
|
100 | 100 |
|
101 | 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); |
| 102 | + var layout; |
| 103 | + if (config.layout) { |
| 104 | + layout = layouts.layout(config.layout.type, config.layout); |
| 105 | + } |
| 106 | + return smtpAppender(config, layout); |
107 | 107 | }
|
108 | 108 |
|
109 | 109 | 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); |
| 110 | + if (shutdownTimeout > 0) { |
| 111 | + setTimeout(function () { |
| 112 | + unsentCount = 0; |
| 113 | + }, shutdownTimeout); |
| 114 | + } |
| 115 | + async.whilst(function () { |
| 116 | + return unsentCount > 0; |
| 117 | + }, function (done) { |
| 118 | + setTimeout(done, 100); |
| 119 | + }, cb); |
118 | 120 | }
|
119 | 121 |
|
120 | 122 | exports.name = "smtp";
|
|
0 commit comments