我很高兴使用npm模块morgan进行日志记录,但不确定如何在节点应用程序中记录系统崩溃或系统关闭。可能吗?请指导。
由于
答案 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