【问题标题】:Node.js winston logging using mail transport to send only uncaughtException as an emailNode.js winston 使用邮件传输进行日志记录以仅将 uncaughtException 作为电子邮件发送
【发布时间】:2016-05-02 08:23:12
【问题描述】:

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

显然配置不支持

handleException: true 

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

似乎存在一个未解决的问题,即只能通过电子邮件发送例外情况,但没有人对此做出回应。我做了+1这个问题,希望能有所收获。

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

【问题讨论】:

    标签: javascript node.js express logging winston


    【解决方案1】:

    奇怪的是,我通过更改配置记录器的方式让它工作。我在exceptionHandlers 中添加了邮件传输,并省略了handleException: true 位。配置如下:

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

    出于测试目的,我通过一个 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 通过文件传输记录并通过邮件传输发送电子邮件。

    当它不工作时,我配置了不同的传输然后我发现我可以使用exceptionHandlers。我不知道使用exceptionHanlders 是否使它工作或其他东西,但不管它是否工作。

    【讨论】:

    • ffd 在示例中不要使用 gmail,这就像送人去赌场谋生一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 2016-11-12
    相关资源
    最近更新 更多