Fixing issue #137
This commit is contained in:
parent
fdc9d253c9
commit
8492519e3b
43
examples/smtp-appender.js
Normal file
43
examples/smtp-appender.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
//Note that smtp appender needs nodemailer to work.
|
||||||
|
//If you haven't got nodemailer installed, you'll get cryptic
|
||||||
|
//"cannot find module" errors when using the smtp appender
|
||||||
|
var log4js = require('../lib/log4js')
|
||||||
|
, log
|
||||||
|
, logmailer
|
||||||
|
, i = 0;
|
||||||
|
log4js.configure({
|
||||||
|
"appenders": [
|
||||||
|
{
|
||||||
|
type: "console",
|
||||||
|
category: "test"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "smtp",
|
||||||
|
"recipients": "logfilerecipient@logging.com",
|
||||||
|
"sendInterval": 5,
|
||||||
|
"transport": "SMTP",
|
||||||
|
"SMTP": {
|
||||||
|
"host": "smtp.gmail.com",
|
||||||
|
"secureConnection": true,
|
||||||
|
"port": 465,
|
||||||
|
"auth": {
|
||||||
|
"user": "someone@gmail",
|
||||||
|
"pass": "********************"
|
||||||
|
},
|
||||||
|
"debug": true
|
||||||
|
},
|
||||||
|
"category": "mailer"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
log = log4js.getLogger("test");
|
||||||
|
logmailer = log4js.getLogger("mailer");
|
||||||
|
|
||||||
|
function doTheLogging(x) {
|
||||||
|
log.info("Logging something %d", x);
|
||||||
|
logmailer.info("Logging something %d", x);
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( ; i < 500; i++) {
|
||||||
|
doTheLogging(i);
|
||||||
|
}
|
@ -17,12 +17,13 @@ function smtpAppender(config, layout) {
|
|||||||
|
|
||||||
var logEventBuffer = [];
|
var logEventBuffer = [];
|
||||||
var sendTimer;
|
var sendTimer;
|
||||||
var transport = mailer.createTransport(config.transport, config[config.transport]);
|
|
||||||
|
|
||||||
function sendBuffer() {
|
function sendBuffer() {
|
||||||
if (logEventBuffer.length == 0)
|
if (logEventBuffer.length == 0) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var transport = mailer.createTransport(config.transport, config[config.transport]);
|
||||||
var firstEvent = logEventBuffer[0];
|
var firstEvent = logEventBuffer[0];
|
||||||
var body = "";
|
var body = "";
|
||||||
while (logEventBuffer.length > 0) {
|
while (logEventBuffer.length > 0) {
|
||||||
@ -35,29 +36,33 @@ function smtpAppender(config, layout) {
|
|||||||
text: body,
|
text: body,
|
||||||
headers: {"Hostname": os.hostname()}
|
headers: {"Hostname": os.hostname()}
|
||||||
};
|
};
|
||||||
if (config.sender)
|
if (config.sender) {
|
||||||
msg.from = config.sender;
|
msg.from = config.sender;
|
||||||
|
}
|
||||||
transport.sendMail(msg, function(error, success) {
|
transport.sendMail(msg, function(error, success) {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error("log4js.smtpAppender - Error happened ", error);
|
console.error("log4js.smtpAppender - Error happened ", error);
|
||||||
}
|
}
|
||||||
|
transport.close();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function scheduleSend() {
|
function scheduleSend() {
|
||||||
if (!sendTimer)
|
if (!sendTimer) {
|
||||||
sendTimer = setTimeout(function() {
|
sendTimer = setTimeout(function() {
|
||||||
sendTimer = null;
|
sendTimer = null;
|
||||||
sendBuffer();
|
sendBuffer();
|
||||||
}, sendInterval);
|
}, sendInterval);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return function(loggingEvent) {
|
return function(loggingEvent) {
|
||||||
logEventBuffer.push(loggingEvent);
|
logEventBuffer.push(loggingEvent);
|
||||||
if (sendInterval > 0)
|
if (sendInterval > 0) {
|
||||||
scheduleSend();
|
scheduleSend();
|
||||||
else
|
} else {
|
||||||
sendBuffer();
|
sendBuffer();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user