使用邮件传输的Node.js winston日志记录仅发送uncaughtException作为电子邮件

时间:2016-01-25 21:18:04

标签: javascript node.js express logging winston

有没有办法使用uncaughtException模块的Winston传输将Mail发送到电子邮件地址?我不想使用任何其他方式发送电子邮件uncaughtExceptions。我知道有很多方法可以解决这个问题,但我更热衷于让温斯顿的邮件运输工作。

显然配置不支持

handleException: true 
如果可以的话,会很酷。我使用其他传输来记录所有异常但是,当涉及uncaughtException异常时,我不仅要记录它,而且还要在抛出它们时给自己发送电子邮件,这样我就可以进入系统并解决问题。显然,我将使用foreverpm2作为主管,无论如何都会重新启动应用程序。

似乎有一个公开的问题,即只能通过电子邮件发送例外情况,但是没有人对此作出回应。我做了+1这个问题,希望得到一些东西。

是否有人使用Winston的邮件传输仅发送uncaughtException。如果是的话,你是如何解决这个问题的?分享将不胜感激。

1 个答案:

答案 0 :(得分:3)

奇怪的是,我通过改变配置记录器的方式让它发挥作用。我在handleException: true中添加了Mail传输,并省略了var winston = require('winston'); var Mail = require('winston-mail').Mail; var logger = new (winston.Logger)({ transports: [ new (winston.transports.File)({ name: 'vehicle-log', filename: './log/vehicle.log', level: 'info', timestamp: true, colorize: true, handleExceptions: true, humanReadableUnhandledException: true, prettyPrint: true, json: true, maxsize: 5242880 }) ], exceptionHandlers: [ new winston.transports.Mail({ to:'toAddress', from:'fromAddress', subject: 'uncaughtException Report', host:'smtp.relaxitsjustanexample.com', username:'emailadd', password:'password', ssl: true }) ] }); 位。配置如下:

router.get('/api/burn', function(req, res) {
    logger.info('ROUTE GET /api/burn');

    process.nextTick(function() {
      throw new Error('Catch me if you can.');
    });
});

出于测试目的,我通过一个未被捕获的异常并确保Express没有抓住它如下:

uncaughtException

exceptionHandlers通过文件传输记录,也可以通过Mail传输进行电子邮件记录。

当它无法正常工作时,我以不同的方式配置了传输,然后我发现我可以使用exceptionHanlders。我不知道使用{{1}}是否使其工作或其他什么,但无论它是否有效。