简体   繁体   English

在Node.js + Express中记录错误的任何实际稳定方法?

[英]Any actual stable way to log errors in Node.js + Express?

So, I've been looking at this for quiet a while now. 所以,我一直在寻找安静的一段时间。 As of Node.js 0.8.15, Event: 'uncaughtException' at http://nodejs.org/api/process.html#process_event_uncaughtexception says: 从Node.js 0.8.15开始,在http://nodejs.org/api/process.html#process_event_uncaughtexception上的 Event: 'uncaughtException'说:

Don't use it, use domains instead. If you do use it, restart your application after every unhandled exception!

And domains at http://nodejs.org/api/domain.html say: http://nodejs.org/api/domain.html上的域名说:

Stability: 1 - Experimental

(also they refuse to work for me - my app still crashes) (他们也拒绝为我工作 - 我的应用程序仍然崩溃)

All I am trying to do is not let my Node.js + Express app crash. 我所要做的就是让我的Node.js + Express应用程序崩溃。

Any actual way anybody has been successful in logging ALL errors to a file (expected & unexpected like syntax errors) and not let Node.js crash? 任何人都成功地将所有错误记录到文件中(预期和意外的语法错误)并且不让Node.js崩溃的任何实际方式?

I've heard plenty of theoretical answers, any actual code that works? 我听过很多理论答案,任何有效的实际代码?

I have run into similar circumstances and have been using uncaught exception. 我遇到过类似的情况,一直在使用未捕获的异常。 It may not be pretty, but with some additional tooling, I think you can end up with a nice outcome. 它可能不是很漂亮,但通过一些额外的工具,我认为你可以得到一个很好的结果。

Use Forever and Winston 使用Forever和Winston

Let it crash 让它崩溃

If you just want to keep your application up, maybe consider using forever from NodeJitsu . 如果您只想保持自己的应用程序,可以考虑永远使用NodeJitsu You can set a threshold for the number of times it should restart your app in the event you've got a nasty bug and it's just not going to stay up. 你可以设置一个阈值,它应该重新启动你的应用程序的次数,如果你有一个讨厌的错误,它只是不会熬夜。 Not allowing your app to cycle in the event of an uncaught exception could put the application in an invalid state and lead to more exceptions. 不允许您的应用在未捕获异常的情况下循环可能会使应用程序处于无效状态并导致更多异常。 Why not just let it crash and have forever restart it for you. 为什么不让它崩溃并永远为你重新启动它。

Logging before exit 退出前记录

To catch and log the errors, try using winston to catch and log the uncaught exception and then exit. 要捕获并记录错误,请尝试使用winston捕获并记录未捕获的异常,然后退出。 See the section on Handling Uncaught Exceptions With Winston of their readme that explains how to handle this exact scenario. 请参阅有关使用Winston处理未捕获的异常的部分,其中介绍了如何处理此确切方案。 If winston is logging to a file, you can reference it and see how often your app is falling over and why. 如果winston正在登录文件,您可以参考它并查看您的应用程序频繁崩溃的频率以及原因。

Outcome 结果

Combining the two, you should be able to setup an uncaught exception log file that you can refer to, keep your app up and running under most circumstances and avoid ignoring exceptions that could potentially put your application in an invalid state. 结合这两者,您应该能够设置一个可以引用的未捕获的异常日志文件,在大多数情况下保持您的应用程序运行并避免忽略可能使您的应用程序处于无效状态的异常。

Alex 亚历克斯

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

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