简体   繁体   English

如何记录node.js程序中的每个“抛出”?

[英]How to log every “throw” in node.js program?

I have a quite large js codebase that is run as node.js script. 我有一个相当大的js代码库,可以作为node.js脚本运行。 I'd like to log every exception even if it is caught inside the production code. 我想记录每个异常,即使它在生产代码中被捕获也是如此。

Is it possible to achieve? 有可能实现吗? I thought about injecting some code inside Error constructor by redefining it, but some parts of code throw custom errors. 我曾考虑过通过重新定义在Error构造函数中注入一些代码,但是代码的某些部分会抛出自定义错误。 Redefining all error types from the production code is not feasible, neither is adding logging logic next to every throw statement and/or try-catch block. 从生产代码中重新定义所有错误类型是不可行的,在每个throw语句和/或try-catch块旁边添加日志记录逻辑也不可行。

The best solution would be to somehow redefine throw keyword - is that possible? 最好的解决方案是以某种方式重新定义throw关键字-可能吗?

There is an event for unhandled exceptions : 有一个未处理的异常事件

process.on('uncaughtException', function (err) {
  console.log('Caught exception: ' + err);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM