在生产中捕获 JavaScript 错误的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3701225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:59:55  来源:igfitidea点击:

Best way to capture JavaScript errors in production?

javascriptdebuggingfrontend

提问by rsturim

I've got a serious JavaScript problem that is hard to reproduce in any of our dev/test/prod environments. Nonetheless, it is being reported consistently by our customers. Sometimes we think it's browser specific -- sometimes we think it's action specific -- sometimes we think it's cookie related. It's a tough one and we're getting pulled in too many different directions and they are all coming up short.

我有一个严重的 JavaScript 问题,很难在我们的任何开发/测试/生产环境中重现。尽管如此,我们的客户一直在报告它。有时我们认为它是特定于浏览器的——有时我们认为它是特定于操作的——有时我们认为它与 cookie 相关。这是一个艰难的过程,我们被拉向了太多不同的方向,而且它们都不够用。

We believe the problem occurs in one of our main JavaScript files -- but that file is enormous. We've pin-pointed other problems in the past in this file -- and guarded against future problems using try/catch blocks successfully -- but, at this time we're very unsure where these newer problems are occurring.

我们认为问题出在我们的一个主要 JavaScript 文件中——但该文件非常庞大。我们已经在这个文件中指出了过去的其他问题——并成功地使用 try/catch 块防止了未来的问题——但是,此时我们非常不确定这些新问题是在哪里发生的。

We've searched through our server logs and the information we are finding there is not useful.

我们已经搜索了我们的服务器日志,我们发现那里的信息没有用。

I am wondering if utilizing a JavaScript logging framework would help our problems. Will implementing something like "log4javascript" capture/log the activity of our users, not just us? Any advice? Anyone else been in this situation? What strategies did you employ to better understand your errors?

我想知道使用 JavaScript 日志框架是否会帮助我们解决问题。实施诸如“ log4javascript”之类的东西会捕获/记录我们用户的活动,而不仅仅是我们吗?有什么建议吗?还有其他人遇到过这种情况吗?你采用了什么策略来更好地理解你的错误?

回答by Tim Down

log4javascript's AjaxAppendercan be used to send log messages to the server.

log4javascript 的AjaxAppender可用于向服务器发送日志消息。

var log = log4javascript.getLogger("serverlog");
var ajaxAppender = new log4javascript.AjaxAppender("http://example.com/clientlogger");
log.addAppender(ajaxAppender);

You could put informational logging calls in your code and add a window.onerrorhandler to catch errors not caught by try/catchblocks in your code:

您可以在代码中放置信息日志调用,并添加一个window.onerror处理程序来捕获try/catch代码中块未捕获的错误:

window.onerror = function(errorMsg, url, lineNumber) {
    log.fatal("Uncaught error " + errorMsg + " in " + url + ", line " + lineNumber);
};

You will also need to create something on the server to process the logging requests from the browser.

您还需要在服务器上创建一些东西来处理来自浏览器的日志记录请求。

Full disclosure: I am the author of log4javascript.

完全披露:我是 log4javascript 的作者。

回答by detj

  1. Take a look at errorception
  1. 看一下errorception

http://errorception.com/

http://errorception.com/

  1. Also try Atatus- It is a new JavaScript Error Tracking Service along with Real User Monitoring (RUM) for modern web apps.
  1. 还可以尝试Atatus- 它是一种新的 JavaScript 错误跟踪服务以及用于现代 Web 应用程序的真实用户监控 (RUM)。

https://www.atatus.com/

https://www.atatus.com/

  1. Also BugSense released an html5 version of their bug tracking
  1. BugSense 还发布了一个 html5 版本的错误跟踪

https://www.bugsense.com/docs/html5

https://www.bugsense.com/docs/html5

So, you get a pretty interface to track them

所以,你会得到一个漂亮的界面来跟踪它们