I'm using winston and winston-elasticsearch to log on Elastic Search. I'm having no problem to setup the transport and everything is correctly working.
Anyway if the ES server became unreachable, the logs sent during the down-time seems to be lost forever.
Is there a way to tell Winston to log on file when (and only when) the Elastic-Search transport fails?
I was also trying to handle this scenario (i.e. when a Transport fails to log a message (in my case a file logger, it can fail if some one has mentioned wrong file path in config) ) by Subscribing to "error" event of Logger. But I can not get it working.
What I tried was instantiate 2 different Loggers One with File Transport and another with Mail Transport then subscribed to "error" event of First logger and in the error event Use the Logger with Mail transport to send mails, but the error event is not fired if I specify wrong file path and the Node process crashes.
var config = require('config');
var smtpConfig = config.get('serving.smtp');
var adClickLogConfig = config.get('adClick.logging');
var adClickMailConfig = config.get('adClick.mail');
var winston = require('winston');
require('winston-mail').Mail;
function AdClickLoggingService (staticFunctions){
this.staticFunctions = staticFunctions;
var fileName = adClickLogConfig.filePath + "/" + adClickLogConfig.fileName;
this.fileLogger = new (winston.Logger)({
transports: [
new (winston.transports.File)({
filename: fileName,
level: adClickLogConfig.level,
timestamp : adClickLogConfig.timeStamp,
maxsize : adClickLogConfig.maxSize,
maxFiles : adClickLogConfig.maxFiles,
json : adClickLogConfig.json
})
]
});
this.fileLogger.on('error', function (err) {
this.emailLogger.info(err);
});
this.fileLogger.on('logging', function (transport, level, msg, meta) {
console.log('Message logged' + msg);
// [msg] and [meta] have now been logged at [level] to [transport]
});
this.emailLogger = new (winston.Logger)({
transports: [
new (winston.transports.Mail)({
host: smtpConfig.host,
tls: smtpConfig.tls,
to : adClickMailConfig.toAddress.join(),
from : adClickMailConfig.fromAddress,
subject : adClickMailConfig.subject
})
]
});
this.fileLogger.on('error', function (err) {
console.log(err);
});
};
AdClickLoggingService.prototype = {
logAdClick:function(id, msg){
msg.adClickId = id;
msg = this.staticFunctions.convertToString(msg);
return this.fileLogger.info(msg);
}
};