捕获生产中的JavaScript错误的最佳方式? [英] Best way to capture JavaScript errors in production?

查看:113
本文介绍了捕获生产中的JavaScript错误的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个严重的JavaScript问题,很难在我们的任何dev / test / prod环境中重现。尽管如此,我们的客户一直报道。有时我们认为这是浏览器特定的 - 有时我们认为这是具体的动作 - 有时我们认为它是与cookie相关的。这是一个艰难的一个,我们被拉到太多不同的方向,而且都是短暂的。



我们认为这个问题发生在我们的一个主要的JavaScript文件 - 但这个文件是巨大的。我们以前在这个文件中指出了其他问题,并且成功地使用try / catch块来防范未来的问题,但是在这个时候,我们不确定这些更新的问题是在发生的。



我们通过我们的服务器日志进行搜索,我们发现的信息无效。



我想知道如果使用JavaScript日志记录框架可以帮助我们解决问题。将实施类似 log4javascript 的捕获/登录用户的活动,而不仅仅是我们?任何建议?有人在这种情况吗?您采用哪些策略来更好地了解错误?

解决方案

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

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

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

  window.onerror = function(errorMsg,url,lineNumber){
log.fatal(未知错误+ errorMsg +在+ url +,行+ lineNumber ;
};

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

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


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.

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.

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?

解决方案

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

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.onerror handler to catch errors not caught by try/catch blocks in your code:

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.

这篇关于捕获生产中的JavaScript错误的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆