UNPKG

@aligov/retcodelog

Version:

retcode log上报npm包,适用于 browser|nodejs|weex|E应用

370 lines (369 loc) 12.8 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var logBase_1 = require("./logBase"); var LogMpApi = /** @class */ (function (_super) { __extends(LogMpApi, _super); function LogMpApi() { return _super !== null && _super.apply(this, arguments) || this; } LogMpApi.prototype.setDebugMode = function (debugParam) { this.debugMode = debugParam.debug; if (this.debugMode) { this.logConsole("[LOG DEBUG MODE]: print log params in console without sending request"); } return this; }; /** * 在retcode系列中,参数nick在发送日志的时会转变为 userNick发送 * @param logConfig */ LogMpApi.prototype.setConfig = function (logConfig) { if (logConfig.spmId) { this.spmId = logConfig.spmId; } if (logConfig.imgUrl) { this.imgUrl = logConfig.imgUrl; } if (logConfig.request) { this.request = logConfig.request; } if (logConfig.appName) { this.appName = logConfig.appName; } if (logConfig.nick) { this.setLogBaseSendValuePairs({ userNick: logConfig.nick, }); } if (logConfig.tag) { this.setLogBaseSendValuePairs({ tag: logConfig.tag, }); } if (logConfig.uid) { this.setLogBaseSendValuePairs({ uid: logConfig.uid, }); } if (logConfig.retCode) { this.setRetcodeApiSamplePairs(logConfig.retCode); } /** * 采样率配置,默认为1,全量采样 */ if (logConfig.sample) { this.sampling = logConfig.sample; } if (logConfig.errMsgFilter) { this.errMsgFilter = logConfig.errMsgFilter; } return this; }; LogMpApi.prototype.setRetcodeApiSamplePairs = function (samplePairs) { this.retCodeApiSampleMap = __assign({}, this.retCodeApiSampleMap, samplePairs); }; LogMpApi.prototype.setLogBaseSendValuePairs = function (keyValuePairs) { this.logBaseArgs = __assign({}, this.logBaseArgs, keyValuePairs); return this; }; /** * @param param 其他附属query,比如可以指定{page: 'page1/page2'}来统计不同页面的pv * @param fullLoadTime 性能时间,模拟dom环境下的onload时间 */ LogMpApi.prototype.performance = function (param, fullLoadTime) { var baseLogData = __assign({ type: 'per' }, (param || {})); if (fullLoadTime) { var params = { rrt: 0, dns: 0, cnt: 0, ntw: 0, dct: 0, flt: fullLoadTime, }; this.sendRequest(__assign({}, baseLogData, params)); } else { this.sendRequest(baseLogData); } }; LogMpApi.prototype.error = function (category, msg, file, line, col, stack) { var errParam = { type: 'jserror', }; if (arguments.length === 1) { // 只传error msg的情况 msg = category; category = undefined; } // 有错误信息才上报 if (!msg) { return; } if (typeof category === 'object' || typeof category === 'undefined') { errParam.category = 'sys'; } else { errParam.category = category; } if (typeof msg === 'object' && msg.message) { // event处理https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent var msgEvent = msg; try { msg = msgEvent.message; file = file || msgEvent.filename; line = line || msgEvent.lineno; col = col || msgEvent.colno; stack = stack || msgEvent.stack; } catch (e) { msg = ''; } } else { if (typeof msg === 'object') { try { msg = JSON.stringify(msg); } catch (e) { msg = ''; } } } try { if (msg) { // 限制1000个字符 msg = msg.substring(0, 1e3); } if (stack) { stack = stack.substring(0, 1e3); } } catch (e) { msg = ''; stack = ''; } errParam.msg = msg; if (file) { errParam.file = file; } if (line) { errParam.line = line; } if (col) { errParam.col = col; } if (stack) { errParam.stack = stack; } // 若有设置errMsgFilter if (this.filterErrMsg(msg)) { return; } else { // 跟随retcode源码,采样率为1 this.sendRequest(errParam, 1); } }; LogMpApi.prototype.retCode = function (api, issuccess, delay, code, msg) { var retApiParam = { type: 'retcode', }; var customApiSample; if (typeof api === 'object') { // 新接口 var apiLog = api; try { apiLog.msg = apiLog.msg ? apiLog.msg.substring(0, 1e3) : ''; } catch (_a) { apiLog.msg = ''; } retApiParam.api = apiLog.api; retApiParam.issucess = apiLog.issuccess; retApiParam.delay = typeof apiLog.delay === 'number' ? apiLog.delay : (new Date().getTime() - this.perfStartTime); retApiParam.msg = apiLog.msg || (apiLog.issuccess ? 'success' : 'fail'); retApiParam.detail = apiLog.detail || ''; retApiParam.traceId = apiLog.traceId || ''; customApiSample = this.retCodeApiSampleMap[apiLog.api]; } else { // 兼容老的接口 try { msg = msg ? msg.substring(0, 1e3) : ''; } catch (e) { msg = ''; } retApiParam.msg = code || (issuccess ? 'success' : 'fail'); retApiParam.api = api; retApiParam.issucess = !!issuccess; retApiParam.delay = typeof delay === 'number' ? delay : (new Date().getTime() - this.perfStartTime); retApiParam.detail = msg || ''; customApiSample = this.retCodeApiSampleMap[api]; } if (retApiParam.issucess) { // customApiSample为undefined时会用config中的默认值 this.sendRequest(retApiParam, customApiSample); } else { this.sendRequest(retApiParam, 1); } }; LogMpApi.prototype.speed = function (pos, delay, immediately) { var _this = this; var sArr; sArr = this.speedPoints || new Array(11); var posVal = typeof delay === 'number' ? (delay > 86400000 ? delay - this.perfStartTime : delay) : (new Date().getTime() - this.perfStartTime); if (posVal < 0) { posVal = new Date().getTime() - this.perfStartTime; } sArr[pos] = posVal; this.speedPoints = sArr; clearTimeout(this.speedTimer); if (!immediately) { this.speedTimer = setTimeout(function () { return _this.sendSpeed(); }, 3000); } else { this.sendSpeed(); } }; LogMpApi.prototype.log = function (msg, customSampling) { var logParam = { type: 'log', msg: msg, }; this.sendRequest(logParam, customSampling); }; /** * 兼容老的retcode custom打点使用方式 */ LogMpApi.prototype.custom = function (category, key, value) { var customParam = { type: 'custom', }; var arr = ['time', 'count']; customParam.category = arr[category] || category; customParam.key = key; if (customParam.category === 'time' && value) { customParam['value'] = value; } this.sendRequest(customParam); }; /** * 页面启动结束打点,统计 logPageMount打点时间 - performance打点的时间差 * 发送日志query: {type: 'page_usable', timeType: string} * 用户如果不想用sdk的计算方式可以自行制定param={timeType: XXX}来覆盖 * sdk内部time计算方式: logPageMount打点时间 - performance打点的时间 * 如果有多页需要进行打点pv操作,调用performance的时候,都会重置一次 performance打点的时间 */ LogMpApi.prototype.logPageMount = function (param) { var _a; var baseLogData = { type: 'page_usable', }; if (this.perfStartTime) { var timeCost = new Date().getTime() - this.perfStartTime; var timeLevel1 = 1000; var timeLevel2 = 3000; var timeLevel3 = 5000; var timeLevel4 = 10000; var timeTypeLargerThan10 = 'load_timeLargerThan10'; var perfLogMap = (_a = {}, _a[timeLevel1] = 'load_1', _a[timeLevel2] = 'load_3', _a[timeLevel3] = 'load_5', _a[timeLevel4] = 'load_10', _a); if (timeCost < timeLevel1) { baseLogData.timeType = perfLogMap[timeLevel1]; } else if (timeCost < timeLevel2) { baseLogData.timeType = perfLogMap[timeLevel2]; } else if (timeCost < timeLevel3) { baseLogData.timeType = perfLogMap[timeLevel3]; } else if (timeCost < timeLevel4) { baseLogData.timeType = perfLogMap[timeLevel4]; } else { baseLogData.timeType = timeTypeLargerThan10; } } var queryLogData = __assign({}, baseLogData, (param || {})); this.sendRequest(queryLogData); }; LogMpApi.prototype.logQueryPart = function (queryPart) { var logQueryPartParm = __assign({ type: 'log' }, queryPart); this.sendRequest(logQueryPartParm); }; LogMpApi.prototype.filterErrMsg = function (msg) { if (!this.errMsgFilter) { return false; } else { var filterPattern = this.errMsgFilter; try { var type = Object.prototype.toString.call(filterPattern).substring(8).replace(']', ''); if (type === 'Function') { var funcFilterPattern = filterPattern; return !!funcFilterPattern(msg); } else if (filterPattern instanceof RegExp) { return filterPattern.test(msg); } else if (typeof filterPattern === 'string') { return msg.indexOf(filterPattern) >= 0; } } catch (err) { this.logConsole("[LOG CONFIG ERROR], retcode log errMsgFitler error:" + JSON.stringify(err)); } return false; } }; LogMpApi.prototype.sendSpeed = function () { if (!this.speedPoints) { return; } var speedParam = { type: 'speed', }; var pointKey; for (var i = 0, len = this.speedPoints.length; i < len; i++) { pointKey = this.speedPoints[i]; if (pointKey) { speedParam["s" + i] = pointKey; this.speedPoints[i] = null; } } this.sendRequest(speedParam); }; return LogMpApi; }(logBase_1.LogBase)); exports.__WPO_MP = new LogMpApi();