3 var layouts = require("../layouts");
4 var mailer = require("nodemailer");
5 var os = require('os');
7 var logEventBuffer = [];
19 function sendBuffer() {
20 if (logEventBuffer.length > 0) {
22 var transportOpts = getTransportOptions(config);
23 var transport = mailer.createTransport(transportOpts);
24 var firstEvent = logEventBuffer[0];
26 var count = logEventBuffer.length;
27 while (logEventBuffer.length > 0) {
28 body += layout(logEventBuffer.shift(), config.timezoneOffset) + "\n";
32 to: config.recipients,
33 subject: config.subject || subjectLayout(firstEvent),
34 headers: {"Hostname": os.hostname()}
37 if (true === config.attachment.enable) {
38 msg[config.html ? "html" : "text"] = config.attachment.message;
41 filename: config.attachment.filename,
42 contentType: 'text/x-log',
47 msg[config.html ? "html" : "text"] = body;
51 msg.from = config.sender;
53 transport.sendMail(msg, function (error) {
55 console.error("log4js.smtpAppender - Error happened", error);
63 function getTransportOptions() {
64 var transportOpts = null;
66 transportOpts = config.SMTP;
67 } else if (config.transport) {
68 var plugin = config.transport.plugin || 'smtp';
69 var transportModule = 'nodemailer-' + plugin + '-transport';
70 var transporter = require(transportModule);
71 transportOpts = transporter(config.transport.options);
77 function scheduleSend() {
79 sendTimer = setTimeout(function () {
87 * SMTP Appender. Sends logging events using SMTP protocol.
88 * It can either send an email on each event or group several
89 * logging events gathered during specified interval.
91 * @param _config appender configuration data
92 * config.sendInterval time between log emails (in seconds), if 0
93 * then every event sends an email
94 * config.shutdownTimeout time to give up remaining emails (in seconds; defaults to 5).
95 * @param _layout a function that takes a logevent and returns a string (defaults to basicLayout).
97 function smtpAppender(_config, _layout) {
100 if (!config.attachment) {
101 config.attachment = {};
104 config.attachment.enable = !!config.attachment.enable;
105 config.attachment.message = config.attachment.message || "See logs as attachment";
106 config.attachment.filename = config.attachment.filename || "default.log";
107 layout = _layout || layouts.basicLayout;
108 subjectLayout = layouts.messagePassThroughLayout;
109 sendInterval = config.sendInterval * 1000 || 0;
111 shutdownTimeout = ('shutdownTimeout' in config ? config.shutdownTimeout : 5) * 1000;
113 return function (loggingEvent) {
115 logEventBuffer.push(loggingEvent);
116 if (sendInterval > 0) {
124 function configure(_config) {
126 if (_config.layout) {
127 layout = layouts.layout(_config.layout.type, _config.layout);
129 return smtpAppender(_config, layout);
132 function shutdown(cb) {
133 if (shutdownTimeout > 0) {
134 setTimeout(function () {
136 clearTimeout(sendTimer);
140 (function checkDone() {
141 if (unsentCount > 0) {
142 setTimeout(checkDone, 100);
149 exports.name = "smtp";
150 exports.appender = smtpAppender;
151 exports.configure = configure;
152 exports.shutdown = shutdown;