se-report
Version:
report js error to your log-server
157 lines (128 loc) • 3.76 kB
JavaScript
/**
* @desc 错误处理上报模块
* @author kangxufeng <kangxufeng@duiba.com.cn>
* @date 2018-01-15
*/
var T = require('../tools/util');
var info = require('../tools/info');
var getExt = require('./getExt');
var _log_list = [];
var _config = {};
var orgError = window.onerror;
// rewrite window.oerror
window.onerror = function(msg, url, line, col, error) {
var newMsg = msg;
if (error && error.stack) {
newMsg = T.processStackMsg(error);
}
if (T.isOBJByType(newMsg, "Event")) {
newMsg += newMsg.type ?
("--" + newMsg.type + "--" + (newMsg.target ?
(newMsg.target.tagName + "::" + newMsg.target.src) : "")) : "";
}
bad.push({
msg: newMsg,
target: url,
rowNum: line,
colNum: col,
_orgMsg: msg
});
_process_log();
orgError && orgError.apply(window, arguments);
};
var submit_log_list = [];
var comboTimeout = 0;
// 上报信息
var _submit_log = function() {
clearTimeout(comboTimeout);
if (!submit_log_list.length) {
return;
}
var url = _config._reportUrl + submit_log_list.join("&") + "&count=" + submit_log_list.length + "&_t=" + (+new Date);
if (_config.submit) {
_config.submit(url);
} else {
var _img = new Image();
_img.src = url;
}
comboTimeout = 0;
submit_log_list = [];
};
// 信息处理、过滤
var _process_log = function(isReportNow) {
if (!_config._reportUrl) return;
var randomIgnore = Math.random() >= _config.random;
while (_log_list.length) {
var isIgnore = false;
var report_log = _log_list.shift();
//有效保证字符不要过长
report_log.msg = (report_log.msg + "" || "").substr(0, 500);
// 重复上报
if (T.isRepeat(report_log, _config.repeat)) continue;
var log_str = T._report_log_tostring(report_log, submit_log_list.length);
if (T.isOBJByType(_config.ignore, "Array")) {
for (var i = 0, l = _config.ignore.length; i < l; i++) {
var rule = _config.ignore[i];
if ((T.isOBJByType(rule, "RegExp") && rule.test(log_str[1])) ||
(T.isOBJByType(rule, "Function") && rule(report_log, log_str[1]))) {
isIgnore = true;
break;
}
}
}
if (!isIgnore) {
if (!randomIgnore && report_log.level != 20) {
submit_log_list.push(log_str[0]);
}
}
}
if (isReportNow) {
_submit_log(); // 立即上报
} else if (!comboTimeout) {
comboTimeout = setTimeout(_submit_log, _config.delay); // 延迟上报
}
};
var bad = {
push: function(msg) {
var data = T.isOBJ(msg) ? T.processError(msg) : {
msg: msg
};
// ext 有默认值, 且上报不包含 ext, 使用默认 ext
if (_config.ext && !data.ext) {
data.ext = getExt(_config.ext);
}
// 在错误发生时获取页面链接
if (!data.from) {
data.from = location.href;
}
if (data._orgMsg) {
var _orgMsg = data._orgMsg;
delete data._orgMsg;
data.level = 4;
}
T.extend(data, info);
_log_list.push(data);
_process_log();
return bad;
},
report: function(msg) {
console.log('push', msg);
},
init: function(config) { // 初始化
_config = config;
// 没有设置pid将不上报
var pid = _config.pid.toString();
if (pid) {
// set default report url and uin
_config._reportUrl = (_config.url || "/api/log") +
"?pid=" + pid +
// "&from=" + encodeURIComponent(location.href) +
"&";
}
// if had error in cache , report now
if (_log_list.length) {
_process_log();
}
}
}
module.exports = bad;