mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
Format source code
This commit is contained in:
parent
a4331b9a96
commit
5db457beee
@ -1,120 +1,122 @@
|
||||
"use strict";
|
||||
var layouts = require("../layouts")
|
||||
, mailer = require("nodemailer")
|
||||
, os = require('os')
|
||||
, async = require('async')
|
||||
, unsentCount = 0
|
||||
, shutdownTimeout;
|
||||
, mailer = require("nodemailer")
|
||||
, os = require('os')
|
||||
, async = require('async')
|
||||
, unsentCount = 0
|
||||
, shutdownTimeout;
|
||||
|
||||
/**
|
||||
* 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).
|
||||
*/
|
||||
* 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) {
|
||||
layout = layout || layouts.basicLayout;
|
||||
var subjectLayout = layouts.messagePassThroughLayout;
|
||||
var sendInterval = config.sendInterval*1000 || 0;
|
||||
|
||||
var logEventBuffer = [];
|
||||
var sendTimer;
|
||||
|
||||
shutdownTimeout = ('shutdownTimeout' in config ? config.shutdownTimeout : 5) * 1000;
|
||||
|
||||
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";
|
||||
}
|
||||
layout = layout || layouts.basicLayout;
|
||||
var subjectLayout = layouts.messagePassThroughLayout;
|
||||
var sendInterval = config.sendInterval * 1000 || 0;
|
||||
|
||||
var msg = {
|
||||
to: config.recipients,
|
||||
subject: config.subject || subjectLayout(firstEvent),
|
||||
headers: { "Hostname": os.hostname() }
|
||||
};
|
||||
|
||||
if (!config.html) {
|
||||
msg.text = body;
|
||||
} else {
|
||||
msg.html = body;
|
||||
}
|
||||
|
||||
if (config.sender) {
|
||||
msg.from = config.sender;
|
||||
}
|
||||
transport.sendMail(msg, function(error, success) {
|
||||
if (error) {
|
||||
console.error("log4js.smtpAppender - Error happened", error);
|
||||
var logEventBuffer = [];
|
||||
var sendTimer;
|
||||
|
||||
shutdownTimeout = ('shutdownTimeout' in config ? config.shutdownTimeout : 5) * 1000;
|
||||
|
||||
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 (!config.html) {
|
||||
msg.text = body;
|
||||
} else {
|
||||
msg.html = body;
|
||||
}
|
||||
|
||||
if (config.sender) {
|
||||
msg.from = config.sender;
|
||||
}
|
||||
transport.sendMail(msg, function (error, success) {
|
||||
if (error) {
|
||||
console.error("log4js.smtpAppender - Error happened", error);
|
||||
}
|
||||
transport.close();
|
||||
unsentCount -= count;
|
||||
});
|
||||
}
|
||||
transport.close();
|
||||
unsentCount -= count;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleSend() {
|
||||
if (!sendTimer) {
|
||||
sendTimer = setTimeout(function() {
|
||||
sendTimer = null;
|
||||
sendBuffer();
|
||||
}, sendInterval);
|
||||
|
||||
function scheduleSend() {
|
||||
if (!sendTimer) {
|
||||
sendTimer = setTimeout(function () {
|
||||
sendTimer = null;
|
||||
sendBuffer();
|
||||
}, sendInterval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getTransportOptions(config) {
|
||||
|
||||
function getTransportOptions(config) {
|
||||
var transportOpts = null;
|
||||
if( config.SMTP ) {
|
||||
if (config.SMTP) {
|
||||
transportOpts = config.SMTP;
|
||||
} else if( config.transport ) {
|
||||
} else if (config.transport) {
|
||||
var plugin = config.transport.plugin || 'smtp';
|
||||
var transportModule = 'nodemailer-' + plugin + '-transport';
|
||||
var transporter = require( transportModule );
|
||||
transportOpts = transporter( config.transport.options );
|
||||
var transporter = require(transportModule);
|
||||
transportOpts = transporter(config.transport.options);
|
||||
}
|
||||
|
||||
return transportOpts;
|
||||
}
|
||||
|
||||
return function(loggingEvent) {
|
||||
unsentCount++;
|
||||
logEventBuffer.push(loggingEvent);
|
||||
if (sendInterval > 0) {
|
||||
scheduleSend();
|
||||
} else {
|
||||
sendBuffer();
|
||||
}
|
||||
};
|
||||
|
||||
return function (loggingEvent) {
|
||||
unsentCount++;
|
||||
logEventBuffer.push(loggingEvent);
|
||||
if (sendInterval > 0) {
|
||||
scheduleSend();
|
||||
} else {
|
||||
sendBuffer();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function configure(config) {
|
||||
var layout;
|
||||
if (config.layout) {
|
||||
layout = layouts.layout(config.layout.type, config.layout);
|
||||
}
|
||||
return smtpAppender(config, layout);
|
||||
var layout;
|
||||
if (config.layout) {
|
||||
layout = layouts.layout(config.layout.type, config.layout);
|
||||
}
|
||||
return smtpAppender(config, layout);
|
||||
}
|
||||
|
||||
function shutdown(cb) {
|
||||
if (shutdownTimeout > 0) {
|
||||
setTimeout(function() { unsentCount = 0; }, shutdownTimeout);
|
||||
}
|
||||
async.whilst(function() {
|
||||
return unsentCount > 0;
|
||||
}, function(done) {
|
||||
setTimeout(done, 100);
|
||||
}, cb);
|
||||
if (shutdownTimeout > 0) {
|
||||
setTimeout(function () {
|
||||
unsentCount = 0;
|
||||
}, shutdownTimeout);
|
||||
}
|
||||
async.whilst(function () {
|
||||
return unsentCount > 0;
|
||||
}, function (done) {
|
||||
setTimeout(done, 100);
|
||||
}, cb);
|
||||
}
|
||||
|
||||
exports.name = "smtp";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user