客户端Javascript崩溃的服务器端日志
Server Side Logging Of Client Side Javascript Crashes
我有一个大型复杂的web应用程序,有数千行Javascript。有一小部分断断续续的Javascript bug是由用户报告的。
我认为这些都是竞争条件的附带现象-某些东西没有正确初始化,Javascript崩溃导致'下游' js不运行。
是否有办法让Javascript执行崩溃日志回服务器端?
我用窗口写了一个远程错误记录函数。@pimvdb
建议的OnerrorErr = {};
Err.Remoterr = {};
Err.Remoterr.onerror = function (msg, errorfileurl, lineno) {
var jsonstring, response, pageurl, cookies;
// Get some user input
response = prompt("There has been an error. " +
"It has been logged and will be investigated.",
"Put in comments (and e-mail or phone number for" +
" response.)");
// get some context of where and how the error occured
// to make debugging easier
pageurl = window.location.href;
cookies = document.cookie;
// Make the json message we are going to post
// Could use JSON.stringify() here if you are sure that
// JSON will have run when the error occurs
// http://www.JSON.org/js.html
jsonstring = "{'"set'": {'"jserr'": " +
"{'"msg'": '"" + msg + "'", " +
"'"errorfileurl'": '"" + errorfileurl + "'", " +
"'"pageurl'": '"" + pageurl + "'", " +
"'"cookies'": '"" + cookies + "'", " +
"'"lineno'": '"" + lineno + "'", " +
"'"response'": '"" + response + "'"}}}";
// Use the jquery cross-browser post
// http://api.jquery.com/jQuery.post/
// this assumes that no errors happen before jquery has initialised
$.post("?jserr", jsonstring, null, "json");
// I don't want the page to 'pretend' to work
// so I am going to return 'false' here
// Returning 'true' will clear the error in the browser
return false;
};
window.onerror = Err.Remoterr.onerror;
我将它部署在网页的头部和正文标记之间。
您将需要更改JSON和您发布它的URL,这取决于您将如何记录数据服务器端。
看看https://log4sure.com(披露:我创建了它)-但它真的很有用,看看它并为自己做决定。它允许您记录错误/事件,还允许您创建自定义日志表。它还允许您实时监视日志。最棒的是,它是免费的。
您也可以使用bower安装它,使用bower install log4sure
设置代码也很简单:
// setup
var _logServer;
(function() {
var ls = document.createElement('script');
ls.type = 'text/javascript';
ls.async = true;
ls.src = 'https://log4sure.com/ScriptsExt/log4sure.min.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ls, s);
ls.onload = function() {
// use your token here.
_logServer = new LogServer("use-your-token-here");
};
})();
// example for logging text
_logServer.logText("your log message goes here.")
//example for logging error
divide = function(numerator, divisor) {
try {
if (parseFloat(value) && parseFloat(divisor)) {
throw new TypeError("Invalid input", "myfile.js", 12, {
value: value,
divisor: divisor
});
} else {
if (divisor == 0) {
throw new RangeError("Divide by 0", "myfile.js", 15, {
value: value,
divisor: divisor
});
}
}
} catch (e) {
_logServer.logError(e.name, e.message, e.stack);
}
}
// another use of logError in window.onerror
// must be careful with window.onerror as you might be overwriting some one else's window.onerror functionality
// also someone else can overwrite window.onerror.
window.onerror = function(msg, url, line, column, err) {
// may want to check if url belongs to your javascript file
var data = {
url: url,
line: line,
column: column,
}
_logServer.logError(err.name, err.message, err.stack, data);
};
// example for custom logs
var foo = "some variable value";
var bar = "another variable value";
var flag = "false";
var temp = "yet another variable value";
_logServer.log(foo, bar, flag, temp);
相关文章:
- 如何使用skip参数使用angular ui引导进行服务器端分页
- Webpack开发服务器和React服务器端渲染
- 提示使用服务器端事件处理程序激活JavaScript
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- 将表单数据提交到服务器端
- 使用ajax的服务器端分页&jQuery
- 显示具有服务器端自动时间注销的同步倒计时计时器
- 使用FormData上传AJAX图像;t在服务器端显示图像
- React路由器服务器端渲染和ajax获取数据
- renderReact/Rect Router+Node/Express.js的服务器端/同构渲染中未定义renderP
- Meteor如何运行服务器端python脚本
- CORS-服务器端cookie没有保存在chrome浏览器上
- 加载服务器端渲染的React组件后执行脚本
- 在Javascript中复制C#服务器端验证
- AngularJS:客户端错误的服务器端日志记录
- 捕获服务器端日志文件中的用户名
- 方法时间的Javascript服务器端日志记录
- 客户端Javascript崩溃的服务器端日志