如何使用nodejs和express来记录应用程序崩溃和系统崩溃?

时间:2017-12-07 20:22:48

标签: node.js morgan

我很高兴使用npm模块morgan进行日志记录,但不确定如何在节点应用程序中记录系统崩溃或系统关闭。可能吗?请指导。

由于

1 个答案:

答案 0 :(得分:1)

您可以使用Process API来监视未捕获的异常。

process
  .on('unhandledRejection', (reason, p) => {
    // Use your own logger here
    console.error(reason, 'Unhandled Rejection at Promise', p);
  })
  .on('uncaughtException', err => {
    // Use your own logger here
    console.error(err, 'Uncaught Exception thrown');

    // Optional: Ensure process will stop after this
    process.exit(1);
  });

有关详细说明,请在Stack Overflow上查看此answer to a similar question。还有一篇很棒的博文:https://shapeshed.com/uncaught-exceptions-in-node/

作为额外内容,请检查另一个,以发送包含崩溃信息的电子邮件:https://coderwall.com/p/4yis4w/node-js-uncaught-exceptions