使用 Winston 在 HAPI 中记录所有未处理的异常

Logging all unhandled exceptions in HAPI with Winston

我正在使用 Winston 记录我使用 log.info 或其他级别专门记录的文件/序列信息。但是我注意到,当发生未处理的异常时,它不会被记录...我对 Nodejs 和 HAPI 不是很熟悉(需要在我的同事休假时执行一些 activity)..但我是想知道是否有一种中间件可以附加并让 Winston 记录所有 HAPI 内容。

提前致谢

您可以监听您当前 Node.js 进程的 uncaughtException and/or unhandledRejection 来调用您的记录器(这里我只是调用 console.log):

process.on('uncaughtException', (err, origin) => {
  console.log('Caught exception:', err, 'Exception origin:', origin);
});

process.on('unhandledRejection', (reason, promise) => {
  console.log('Unhandled Rejection at:', promise, 'reason:', reason);
});

但是:

uncaughtException is a crude mechanism for exception handling intended to be used only as a last resort.

...

The correct use of uncaughtException is to perform synchronous cleanup of allocated resources (e.g. file descriptors, handles, etc) before shutting down the process. It is not safe to resume normal operation after uncaughtException.

另请阅读Catch all uncaughtException for Node js app