javascript - Node.js winston logging using mail transport to send only uncaughtException as an email - Stack Overflow

Is there any way to send uncaughtException to an email address using Winston module's Mail transpo

Is there any way to send uncaughtException to an email address using Winston module's Mail transport? I don't want to use any other approach to email 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 es 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. Obviously I will be using either forever or pm2 as supervisor which would restart the app anyway.

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.

Has anyone used Winston's Mail transport to send uncaughtException only. If yes, how did you get around this issue? Sharing would be appreciated.

Is there any way to send uncaughtException to an email address using Winston module's Mail transport? I don't want to use any other approach to email 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 es 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. Obviously I will be using either forever or pm2 as supervisor which would restart the app anyway.

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.

Has anyone used Winston's Mail transport to send uncaughtException only. If yes, how did you get around this issue? Sharing would be appreciated.

Share Improve this question asked Jan 25, 2016 at 21:18 RafRaf 7,6491 gold badge44 silver badges60 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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. 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.',
      username:'emailadd',
      password:'password',
      ssl: true
    })
  ]
});

For testing purpose, I through an uncaughtException and made sure that Express don't catch it as follow:

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.

When it was not working, I had configured the transport differently then I found out that I can use exceptionHandlers. I don't know if using exceptionHanlders made it work or something else but, regardless it is working.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745231171a4617684.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信