Skip to content

Instantly share code, notes, and snippets.

@nevergiveup-j
Last active June 12, 2018 06:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nevergiveup-j/f8df2b1854492e0f80b6 to your computer and use it in GitHub Desktop.
Save nevergiveup-j/f8df2b1854492e0f80b6 to your computer and use it in GitHub Desktop.
前端代码异常日志收集与监控
/**
* @param {String} msg 错误信息
* @param {String} url 出错的文件
* @param {Long} line 出错代码的行号
* @param {Long} col 出错代码的列号
* @param {Object} error 错误的详细信息,Anything
*/
window.onerror = function(msg,url,line,col,error){
//没有URL不上报!上报也不知道错误
if (msg != "Script error." && !url){
return true;
}
//采用异步的方式
//我遇到过在window.onunload进行ajax的堵塞上报
//由于客户端强制关闭webview导致这次堵塞上报有Network Error
//我猜测这里window.onerror的执行流在关闭前是必然执行的
//而离开文章之后的上报对于业务来说是可丢失的
//所以我把这里的执行流放到异步事件去执行
//脚本的异常数降低了10倍
setTimeout(function(){
var data = {};
//不一定所有浏览器都支持col参数
col = col || (window.event && window.event.errorCharacter) || 0;
data.url = url;
data.line = line;
data.col = col;
if (!!error && !!error.stack){
//如果浏览器有堆栈信息
//直接使用
data.msg = error.stack.toString();
}else if (!!arguments.callee){
//尝试通过callee拿堆栈信息
var ext = [];
var f = arguments.callee.caller, c = 3;
//这里只拿三层堆栈信息
while (f && (--c>0)) {
ext.push(f.toString());
if (f === f.caller) {
break;//如果有环
}
f = f.caller;
}
ext = ext.join(",");
data.msg = error.stack.toString();
}
console.log(data);
//把data上报到后台!
},0);
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment