简体   繁体   English

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

[英]Node.js winston logging using mail transport to send only uncaughtException as an email

Is there any way to send uncaughtException to an email address using Winston module's Mail transport? 有没有办法使用Winston模块的Mail传输将uncaughtException发送到电子邮件地址? I don't want to use any other approach to email uncaughtExceptions . 我不想使用任何其他方法向uncaughtExceptions发送电子邮件。 I kow that there are ways round this but, I am more keen to getting Winston's Mail transport working. 我知道有很多方法可以解决这个问题,但我更热衷于让温斯顿的邮件运输工作。

Apparently the configuration does not support 显然配置不支持

handleException: true 

Would be cool though if it could. 如果它可以的话会很酷。 I use other transports to log all exceptions but, when it comes to uncaughtException exception I not only want to log it but also email myself when they are thrown so I can ssh into the system and resolve the issue. 我使用其他传输来记录所有异常但是,当涉及到uncaughtException异常时,我不仅要记录它,还要在抛出它们时给自己发送电子邮件,这样我就可以进入系统并解决问题。 Obviously I will be using either forever or pm2 as supervisor which would restart the app anyway. 显然我将forever使用或pm2作为主管,无论如何都会重新启动应用程序。

There seem to be an open issue about being able to email only exceptions but, nobody has responded to that. 似乎有一个公开的问题,即只能通过电子邮件发送例外情况,但是没有人对此作出回应。 I did +1 the issue in the hope of getting something. 为了获得某些东西,我确实给了这个问题+1

Has anyone used Winston's Mail transport to send uncaughtException only. 有没有人使用Winston的Mail传输只发送uncaughtException If yes, how did you get around this issue? 如果是的话,你是如何解决这个问题的? Sharing would be appreciated. 分享将不胜感激。

Strangely enough, I got it to work by changing how I configured the logger. 奇怪的是,我通过改变配置记录器的方式来实现它。 I added the Mail transport inside the exceptionHandlers and omitted the handleException: true bit. 我在exceptionHandlers添加了Mail传输,并省略了handleException: true位。 The configuration is as follow: 配置如下:

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
    })
  ]
});

For testing purpose, I through an uncaughtException and made sure that Express don't catch it as follow: 出于测试目的,我通过uncaughtException并确保Express没有捕获它如下:

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

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

uncaughtException gets logged by File transport as well as emailed by Mail transport. uncaughtException由文件传输记录,也由Mail传输通过电子邮件发送。

When it was not working, I had configured the transport differently then I found out that I can use exceptionHandlers . 当它不工作时,我已经配置了不同的传输,然后我发现我可以使用exceptionHandlers I don't know if using exceptionHanlders made it work or something else but, regardless it is working. 我不知道是否使用exceptionHanlders使其工作或其他东西,但无论它是否正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM