@pluve/logger-sdk 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.html +681 -0
- package/README.md +131 -153
- package/dist/cjs/capture/jsError.js +48 -0
- package/dist/cjs/capture/promiseError.js +49 -0
- package/dist/cjs/capture/resourceError.js +48 -0
- package/dist/cjs/capture/wechatError.js +95 -0
- package/dist/cjs/compress/compression.js +84 -0
- package/dist/cjs/config/index.js +55 -0
- package/dist/cjs/core/fingerprint.js +36 -0
- package/dist/cjs/core/httpClient.js +96 -0
- package/dist/cjs/core/loggerSDK.js +641 -0
- package/dist/cjs/core/queueManager.js +249 -0
- package/dist/cjs/core/retryManager.js +127 -0
- package/dist/cjs/index.js +29 -0
- package/dist/cjs/stack/stacktrace.js +48 -0
- package/dist/cjs/transport/beaconTransport.js +64 -0
- package/dist/cjs/transport/pixelImageTransport.js +100 -0
- package/dist/cjs/transport/transport.js +17 -0
- package/dist/cjs/transport/transportAdapter.js +56 -0
- package/dist/cjs/transport/wechatTransport.js +103 -0
- package/dist/cjs/types/api.js +17 -0
- package/dist/cjs/types/env.js +17 -0
- package/dist/cjs/types/external.d.ts +21 -0
- package/dist/cjs/types/logEvent.js +17 -0
- package/dist/cjs/types/logEventLevel.js +17 -0
- package/dist/cjs/types/sdkOptions.js +17 -0
- package/dist/cjs/types/trackOptions.js +17 -0
- package/dist/cjs/utils/environment.js +183 -0
- package/dist/cjs/utils/session.js +31 -0
- package/dist/cjs/utils/tools.js +82 -0
- package/dist/cjs/utils/uuid.js +35 -0
- package/dist/esm/capture/jsError.js +45 -0
- package/dist/esm/capture/promiseError.js +46 -0
- package/dist/esm/capture/resourceError.js +24 -0
- package/dist/esm/capture/wechatError.js +92 -0
- package/dist/esm/compress/compression.js +82 -0
- package/dist/esm/config/index.js +28 -0
- package/dist/esm/core/fingerprint.js +12 -0
- package/dist/esm/core/httpClient.js +95 -0
- package/dist/esm/core/loggerSDK.js +650 -0
- package/dist/esm/core/queueManager.js +269 -0
- package/dist/esm/core/retryManager.js +129 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/stack/stacktrace.js +37 -0
- package/dist/esm/transport/beaconTransport.js +81 -0
- package/dist/esm/transport/pixelImageTransport.js +99 -0
- package/dist/esm/transport/transport.js +0 -0
- package/dist/esm/transport/transportAdapter.js +32 -0
- package/dist/esm/transport/wechatTransport.js +120 -0
- package/dist/esm/types/api.js +0 -0
- package/dist/esm/types/env.js +0 -0
- package/dist/esm/types/external.d.ts +21 -0
- package/dist/esm/types/logEvent.js +0 -0
- package/dist/esm/types/logEventLevel.js +0 -0
- package/dist/esm/types/sdkOptions.js +0 -0
- package/dist/esm/types/trackOptions.js +0 -0
- package/dist/esm/utils/environment.js +154 -0
- package/dist/esm/utils/session.js +7 -0
- package/dist/esm/utils/tools.js +76 -0
- package/dist/esm/utils/uuid.js +11 -0
- package/dist/types/capture/jsError.d.ts +2 -0
- package/dist/types/capture/promiseError.d.ts +2 -0
- package/dist/types/capture/resourceError.d.ts +2 -0
- package/dist/types/capture/wechatError.d.ts +3 -0
- package/dist/types/compress/compression.d.ts +10 -0
- package/dist/types/config/index.d.ts +9 -0
- package/dist/types/core/fingerprint.d.ts +8 -0
- package/dist/types/core/httpClient.d.ts +11 -0
- package/dist/{loggerSDK.d.ts → types/core/loggerSDK.d.ts} +27 -2
- package/dist/{queueManager.d.ts → types/core/queueManager.d.ts} +9 -1
- package/dist/{retryManager.d.ts → types/core/retryManager.d.ts} +0 -4
- package/dist/types/index.d.ts +1 -0
- package/dist/types/stack/stacktrace.d.ts +2 -0
- package/dist/types/transport/beaconTransport.d.ts +11 -0
- package/dist/types/transport/pixelImageTransport.d.ts +11 -0
- package/dist/types/transport/transport.d.ts +14 -0
- package/dist/types/transport/transportAdapter.d.ts +10 -0
- package/dist/types/transport/wechatTransport.d.ts +11 -0
- package/dist/types/types/api.d.ts +12 -0
- package/dist/types/types/env.d.ts +14 -0
- package/dist/types/types/logEvent.d.ts +57 -0
- package/dist/types/types/logEventLevel.d.ts +2 -0
- package/dist/{types.d.ts → types/types/sdkOptions.d.ts} +23 -42
- package/dist/types/types/trackOptions.d.ts +7 -0
- package/dist/types/utils/environment.d.ts +21 -0
- package/dist/types/utils/session.d.ts +1 -0
- package/dist/types/utils/tools.d.ts +12 -0
- package/dist/types/utils/uuid.d.ts +7 -0
- package/dist/umd/logger-sdk.min.js +1 -0
- package/package.json +29 -5
- package/dist/index.d.ts +0 -10
- package/dist/index.js +0 -13
- package/dist/loggerSDK.js +0 -560
- package/dist/queueManager.js +0 -186
- package/dist/retryManager.js +0 -224
- package/dist/transportAdapter.d.ts +0 -51
- package/dist/transportAdapter.js +0 -315
- package/dist/types.js +0 -1
- package/dist/utils.d.ts +0 -52
- package/dist/utils.js +0 -348
package/dist/loggerSDK.js
DELETED
|
@@ -1,560 +0,0 @@
|
|
|
1
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
-
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
6
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
7
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
8
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
9
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
10
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
11
|
-
import { defaultTransport } from "./transportAdapter";
|
|
12
|
-
import { isBrowser, isWeChatMiniProgram, now, getSessionId, getCurrentUrl, collectEnvironmentTags, generateUUID, logDebug } from "./utils";
|
|
13
|
-
import { QueueManager } from "./queueManager";
|
|
14
|
-
import { RetryManager } from "./retryManager";
|
|
15
|
-
export var LoggerSDK = /*#__PURE__*/function () {
|
|
16
|
-
function LoggerSDK() {
|
|
17
|
-
_classCallCheck(this, LoggerSDK);
|
|
18
|
-
_defineProperty(this, "opts", void 0);
|
|
19
|
-
/** 事件序列编号,用于事件去重 */
|
|
20
|
-
_defineProperty(this, "seq", 0);
|
|
21
|
-
/** 是否已关闭 */
|
|
22
|
-
_defineProperty(this, "closed", false);
|
|
23
|
-
/** 预收集的环境信息 tags(仅用于初始化上报) */
|
|
24
|
-
_defineProperty(this, "envTags", {});
|
|
25
|
-
/** 是否已初始化 */
|
|
26
|
-
_defineProperty(this, "initialized", false);
|
|
27
|
-
/** 初始化时生成并锁定的会话标识 */
|
|
28
|
-
_defineProperty(this, "sessionId", void 0);
|
|
29
|
-
/** 队列管理器 */
|
|
30
|
-
_defineProperty(this, "queueManager", void 0);
|
|
31
|
-
/** 重试管理器 */
|
|
32
|
-
_defineProperty(this, "retryManager", void 0);
|
|
33
|
-
/** 批量上报定时器 */
|
|
34
|
-
_defineProperty(this, "batchTimer", void 0);
|
|
35
|
-
/** 是否正在上报 */
|
|
36
|
-
_defineProperty(this, "isSending", false);
|
|
37
|
-
// 初始化时生成并锁定会话标识
|
|
38
|
-
this.sessionId = getSessionId();
|
|
39
|
-
}
|
|
40
|
-
_createClass(LoggerSDK, [{
|
|
41
|
-
key: "init",
|
|
42
|
-
value:
|
|
43
|
-
/**
|
|
44
|
-
* 初始化:配置参数、收集环境信息与 UA,并生成 sessionId,完成初始上报
|
|
45
|
-
*/
|
|
46
|
-
function init(options) {
|
|
47
|
-
this.opts = {
|
|
48
|
-
endpoint: options.endpoint,
|
|
49
|
-
appId: options.appId || 'unknown',
|
|
50
|
-
env: options.env || 'dev',
|
|
51
|
-
debug: !!options.debug,
|
|
52
|
-
/** 是否启用 gzip 压缩,默认 true */
|
|
53
|
-
enableGzip: options.enableGzip !== false,
|
|
54
|
-
// 默认启用
|
|
55
|
-
/** 最大像素图 URL 长度,默认 1900 */
|
|
56
|
-
maxPixelUrlLen: options.maxPixelUrlLen || 4096,
|
|
57
|
-
// 批量上报配置(默认关闭,需显式开启)
|
|
58
|
-
enableBatch: options.enableBatch === true,
|
|
59
|
-
batchSize: options.batchSize || 10,
|
|
60
|
-
batchInterval: options.batchInterval || 5000,
|
|
61
|
-
maxQueueSize: options.maxQueueSize || 100,
|
|
62
|
-
// 持久化存储配置
|
|
63
|
-
enableStorage: options.enableStorage !== false,
|
|
64
|
-
// 默认启用
|
|
65
|
-
storagePrefix: options.storagePrefix || 'logger_sdk',
|
|
66
|
-
// 重试配置
|
|
67
|
-
enableRetry: options.enableRetry !== false,
|
|
68
|
-
// 默认启用
|
|
69
|
-
maxRetries: options.maxRetries || 3,
|
|
70
|
-
retryDelay: options.retryDelay || 1000,
|
|
71
|
-
retryBackoff: options.retryBackoff !== false // 默认启用
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
// 初始化队列管理器
|
|
75
|
-
if (this.opts.enableBatch) {
|
|
76
|
-
this.queueManager = new QueueManager({
|
|
77
|
-
maxSize: this.opts.maxQueueSize,
|
|
78
|
-
enableStorage: this.opts.enableStorage,
|
|
79
|
-
storagePrefix: this.opts.storagePrefix,
|
|
80
|
-
debug: this.opts.debug
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// 初始化重试管理器
|
|
85
|
-
if (this.opts.enableRetry) {
|
|
86
|
-
this.retryManager = new RetryManager({
|
|
87
|
-
maxRetries: this.opts.maxRetries,
|
|
88
|
-
baseDelay: this.opts.retryDelay,
|
|
89
|
-
useBackoff: this.opts.retryBackoff,
|
|
90
|
-
debug: this.opts.debug
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// 监听页面卸载事件
|
|
95
|
-
this.attachUnloadHandlers();
|
|
96
|
-
|
|
97
|
-
// 生成并缓存 sessionId(便于 UV 统计),并锁定到实例生命周期
|
|
98
|
-
// TODO 通过后端接口获取 sessionId
|
|
99
|
-
this.sessionId = getSessionId();
|
|
100
|
-
|
|
101
|
-
// 收集 UA / 环境信息并完成初始上报(便于后端统计 UA 和 UV)
|
|
102
|
-
this.envTags = collectEnvironmentTags(this.opts.debug);
|
|
103
|
-
|
|
104
|
-
// 调用接口初始化
|
|
105
|
-
|
|
106
|
-
// this.track('session_start', 'Session started', undefined, {
|
|
107
|
-
// level: 'info',
|
|
108
|
-
// tags: this.envTags,
|
|
109
|
-
// }).catch((err) => {
|
|
110
|
-
// logDebug(this.opts.debug, 'Failed to report session_start', err);
|
|
111
|
-
// });
|
|
112
|
-
|
|
113
|
-
// 启动批量上报定时器
|
|
114
|
-
if (this.opts.enableBatch) {
|
|
115
|
-
this.startBatchTimer();
|
|
116
|
-
}
|
|
117
|
-
this.initialized = true;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 设置用户信息
|
|
122
|
-
*/
|
|
123
|
-
}, {
|
|
124
|
-
key: "identify",
|
|
125
|
-
value: function identify(userId) {
|
|
126
|
-
logDebug(this.opts.debug, 'identify', userId);
|
|
127
|
-
if (this.opts) {
|
|
128
|
-
this.opts.userId = userId;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}, {
|
|
132
|
-
key: "setStoreCode",
|
|
133
|
-
value: function setStoreCode(storeCode) {
|
|
134
|
-
logDebug(this.opts.debug, 'setStoreCode', storeCode);
|
|
135
|
-
if (this.opts) {
|
|
136
|
-
this.opts.storeCode = storeCode;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 记录事件
|
|
142
|
-
*/
|
|
143
|
-
}, {
|
|
144
|
-
key: "track",
|
|
145
|
-
value: (function () {
|
|
146
|
-
var _track = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
147
|
-
var _error$stack;
|
|
148
|
-
var message, error, traceId, logLevel, logEvent;
|
|
149
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
150
|
-
while (1) switch (_context.prev = _context.next) {
|
|
151
|
-
case 0:
|
|
152
|
-
message = _ref.message, error = _ref.error, traceId = _ref.traceId, logLevel = _ref.logLevel;
|
|
153
|
-
if (!this.closed) {
|
|
154
|
-
_context.next = 3;
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
return _context.abrupt("return");
|
|
158
|
-
case 3:
|
|
159
|
-
if (this.initialized) {
|
|
160
|
-
_context.next = 6;
|
|
161
|
-
break;
|
|
162
|
-
}
|
|
163
|
-
logDebug(this.opts.debug, 'SDK not initialized, skip track');
|
|
164
|
-
return _context.abrupt("return");
|
|
165
|
-
case 6:
|
|
166
|
-
this.seq += 1;
|
|
167
|
-
|
|
168
|
-
// 构建标准化日志格式
|
|
169
|
-
logEvent = {
|
|
170
|
-
/** 日志 ID */
|
|
171
|
-
logId: "".concat(this.opts.appId).concat(generateUUID()).concat(now()),
|
|
172
|
-
// UUID
|
|
173
|
-
/** 应用标识 */
|
|
174
|
-
appId: this.opts.appId || 'unknown',
|
|
175
|
-
/** 环境标识 */
|
|
176
|
-
stage: this.opts.env || 'dev',
|
|
177
|
-
/** 日志级别:info/warn/error/fatal */
|
|
178
|
-
level: logLevel || 'info',
|
|
179
|
-
/** 可选:跟踪 ID(跨服务调用链跟踪) */
|
|
180
|
-
traceId: traceId,
|
|
181
|
-
/** 会话标识,同 sessionId */
|
|
182
|
-
frontendId: this.sessionId,
|
|
183
|
-
// 同 sessionId
|
|
184
|
-
/** 发生页面 URL */
|
|
185
|
-
url: getCurrentUrl(),
|
|
186
|
-
/** 异常发生位置,结构待定 */
|
|
187
|
-
location: (error === null || error === void 0 || (_error$stack = error.stack) === null || _error$stack === void 0 ? void 0 : _error$stack.split('\n')[2]) || '',
|
|
188
|
-
/** 异常信息 */
|
|
189
|
-
message: message,
|
|
190
|
-
/** 可选:堆栈信息(长字符串) */
|
|
191
|
-
throwable: error === null || error === void 0 ? void 0 : error.stack,
|
|
192
|
-
/** 可选:用户 ID(脱敏) */
|
|
193
|
-
userId: this.opts.userId,
|
|
194
|
-
/** 可选:店铺编码 */
|
|
195
|
-
storeCode: this.opts.storeCode,
|
|
196
|
-
/** 可选的结构化额外信息 */
|
|
197
|
-
tags: this.envTags
|
|
198
|
-
};
|
|
199
|
-
logDebug(this.opts.debug, 'track', logEvent);
|
|
200
|
-
|
|
201
|
-
// 如果启用批量上报,添加到队列
|
|
202
|
-
if (!(this.opts.enableBatch && this.queueManager)) {
|
|
203
|
-
_context.next = 17;
|
|
204
|
-
break;
|
|
205
|
-
}
|
|
206
|
-
this.queueManager.enqueue(logEvent);
|
|
207
|
-
// 如果队列已满,立即上报
|
|
208
|
-
if (!(this.queueManager.size() >= this.opts.batchSize)) {
|
|
209
|
-
_context.next = 15;
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
logDebug(this.opts.debug, 'Queue size reached batch size, flushing immediately');
|
|
213
|
-
_context.next = 15;
|
|
214
|
-
return this.flush();
|
|
215
|
-
case 15:
|
|
216
|
-
_context.next = 25;
|
|
217
|
-
break;
|
|
218
|
-
case 17:
|
|
219
|
-
_context.prev = 17;
|
|
220
|
-
_context.next = 20;
|
|
221
|
-
return this.sendEvent(logEvent);
|
|
222
|
-
case 20:
|
|
223
|
-
_context.next = 25;
|
|
224
|
-
break;
|
|
225
|
-
case 22:
|
|
226
|
-
_context.prev = 22;
|
|
227
|
-
_context.t0 = _context["catch"](17);
|
|
228
|
-
logDebug(this.opts.debug, 'track failed', _context.t0);
|
|
229
|
-
// 静默失败,不影响主流程
|
|
230
|
-
case 25:
|
|
231
|
-
case "end":
|
|
232
|
-
return _context.stop();
|
|
233
|
-
}
|
|
234
|
-
}, _callee, this, [[17, 22]]);
|
|
235
|
-
}));
|
|
236
|
-
function track(_x) {
|
|
237
|
-
return _track.apply(this, arguments);
|
|
238
|
-
}
|
|
239
|
-
return track;
|
|
240
|
-
}()
|
|
241
|
-
/**
|
|
242
|
-
* 手动刷新队列,立即上报所有待发送日志
|
|
243
|
-
*/
|
|
244
|
-
)
|
|
245
|
-
}, {
|
|
246
|
-
key: "flush",
|
|
247
|
-
value: (function () {
|
|
248
|
-
var _flush = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
249
|
-
var events;
|
|
250
|
-
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
251
|
-
while (1) switch (_context2.prev = _context2.next) {
|
|
252
|
-
case 0:
|
|
253
|
-
if (!(!this.queueManager || this.queueManager.size() === 0)) {
|
|
254
|
-
_context2.next = 2;
|
|
255
|
-
break;
|
|
256
|
-
}
|
|
257
|
-
return _context2.abrupt("return");
|
|
258
|
-
case 2:
|
|
259
|
-
if (!this.isSending) {
|
|
260
|
-
_context2.next = 5;
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
logDebug(this.opts.debug, 'Already sending, skip flush');
|
|
264
|
-
return _context2.abrupt("return");
|
|
265
|
-
case 5:
|
|
266
|
-
this.isSending = true;
|
|
267
|
-
logDebug(this.opts.debug, "Flushing ".concat(this.queueManager.size(), " events"));
|
|
268
|
-
_context2.prev = 7;
|
|
269
|
-
// 获取所有待发送的日志(不移除)
|
|
270
|
-
events = this.queueManager.peek(this.queueManager.size());
|
|
271
|
-
if (!(events.length === 0)) {
|
|
272
|
-
_context2.next = 11;
|
|
273
|
-
break;
|
|
274
|
-
}
|
|
275
|
-
return _context2.abrupt("return");
|
|
276
|
-
case 11:
|
|
277
|
-
_context2.next = 13;
|
|
278
|
-
return this.sendBatch(events);
|
|
279
|
-
case 13:
|
|
280
|
-
// 发送成功后,从队列中移除
|
|
281
|
-
this.queueManager.dequeue(events.length);
|
|
282
|
-
logDebug(this.opts.debug, "Flushed ".concat(events.length, " events successfully"));
|
|
283
|
-
_context2.next = 20;
|
|
284
|
-
break;
|
|
285
|
-
case 17:
|
|
286
|
-
_context2.prev = 17;
|
|
287
|
-
_context2.t0 = _context2["catch"](7);
|
|
288
|
-
logDebug(this.opts.debug, 'Flush failed', _context2.t0);
|
|
289
|
-
// 失败不移除队列,下次继续重试
|
|
290
|
-
case 20:
|
|
291
|
-
_context2.prev = 20;
|
|
292
|
-
this.isSending = false;
|
|
293
|
-
return _context2.finish(20);
|
|
294
|
-
case 23:
|
|
295
|
-
case "end":
|
|
296
|
-
return _context2.stop();
|
|
297
|
-
}
|
|
298
|
-
}, _callee2, this, [[7, 17, 20, 23]]);
|
|
299
|
-
}));
|
|
300
|
-
function flush() {
|
|
301
|
-
return _flush.apply(this, arguments);
|
|
302
|
-
}
|
|
303
|
-
return flush;
|
|
304
|
-
}()
|
|
305
|
-
/**
|
|
306
|
-
* 销毁实例
|
|
307
|
-
*/
|
|
308
|
-
)
|
|
309
|
-
}, {
|
|
310
|
-
key: "destroy",
|
|
311
|
-
value: (function () {
|
|
312
|
-
var _destroy = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
313
|
-
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
314
|
-
while (1) switch (_context3.prev = _context3.next) {
|
|
315
|
-
case 0:
|
|
316
|
-
this.closed = true;
|
|
317
|
-
|
|
318
|
-
// 停止批量上报定时器
|
|
319
|
-
if (this.batchTimer) {
|
|
320
|
-
clearInterval(this.batchTimer);
|
|
321
|
-
this.batchTimer = null;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// 刷新队列
|
|
325
|
-
_context3.next = 4;
|
|
326
|
-
return this.flush();
|
|
327
|
-
case 4:
|
|
328
|
-
// 清理资源
|
|
329
|
-
if (this.queueManager) {
|
|
330
|
-
this.queueManager.clear();
|
|
331
|
-
}
|
|
332
|
-
if (this.retryManager) {
|
|
333
|
-
this.retryManager.clear();
|
|
334
|
-
}
|
|
335
|
-
this.initialized = false;
|
|
336
|
-
this.sessionId = undefined;
|
|
337
|
-
LoggerSDK.instance = undefined;
|
|
338
|
-
case 9:
|
|
339
|
-
case "end":
|
|
340
|
-
return _context3.stop();
|
|
341
|
-
}
|
|
342
|
-
}, _callee3, this);
|
|
343
|
-
}));
|
|
344
|
-
function destroy() {
|
|
345
|
-
return _destroy.apply(this, arguments);
|
|
346
|
-
}
|
|
347
|
-
return destroy;
|
|
348
|
-
}() // ========== 内部方法 ===========
|
|
349
|
-
/**
|
|
350
|
-
* 发送单个事件(带重试)
|
|
351
|
-
*/
|
|
352
|
-
)
|
|
353
|
-
}, {
|
|
354
|
-
key: "sendEvent",
|
|
355
|
-
value: function () {
|
|
356
|
-
var _sendEvent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(event) {
|
|
357
|
-
var _this = this;
|
|
358
|
-
var sendFn;
|
|
359
|
-
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
360
|
-
while (1) switch (_context5.prev = _context5.next) {
|
|
361
|
-
case 0:
|
|
362
|
-
sendFn = /*#__PURE__*/function () {
|
|
363
|
-
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
364
|
-
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
365
|
-
while (1) switch (_context4.prev = _context4.next) {
|
|
366
|
-
case 0:
|
|
367
|
-
_context4.next = 2;
|
|
368
|
-
return defaultTransport(event, _this.opts);
|
|
369
|
-
case 2:
|
|
370
|
-
case "end":
|
|
371
|
-
return _context4.stop();
|
|
372
|
-
}
|
|
373
|
-
}, _callee4);
|
|
374
|
-
}));
|
|
375
|
-
return function sendFn() {
|
|
376
|
-
return _ref2.apply(this, arguments);
|
|
377
|
-
};
|
|
378
|
-
}(); // 如果启用重试
|
|
379
|
-
if (!(this.opts.enableRetry && this.retryManager)) {
|
|
380
|
-
_context5.next = 6;
|
|
381
|
-
break;
|
|
382
|
-
}
|
|
383
|
-
_context5.next = 4;
|
|
384
|
-
return this.retryManager.executeWithRetry(event.logId, sendFn);
|
|
385
|
-
case 4:
|
|
386
|
-
_context5.next = 8;
|
|
387
|
-
break;
|
|
388
|
-
case 6:
|
|
389
|
-
_context5.next = 8;
|
|
390
|
-
return sendFn();
|
|
391
|
-
case 8:
|
|
392
|
-
case "end":
|
|
393
|
-
return _context5.stop();
|
|
394
|
-
}
|
|
395
|
-
}, _callee5, this);
|
|
396
|
-
}));
|
|
397
|
-
function sendEvent(_x2) {
|
|
398
|
-
return _sendEvent.apply(this, arguments);
|
|
399
|
-
}
|
|
400
|
-
return sendEvent;
|
|
401
|
-
}()
|
|
402
|
-
/**
|
|
403
|
-
* 批量发送事件(带重试)
|
|
404
|
-
*/
|
|
405
|
-
}, {
|
|
406
|
-
key: "sendBatch",
|
|
407
|
-
value: (function () {
|
|
408
|
-
var _sendBatch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(events) {
|
|
409
|
-
var _this2 = this;
|
|
410
|
-
var batchId, sendFn;
|
|
411
|
-
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
412
|
-
while (1) switch (_context7.prev = _context7.next) {
|
|
413
|
-
case 0:
|
|
414
|
-
if (!(events.length === 0)) {
|
|
415
|
-
_context7.next = 2;
|
|
416
|
-
break;
|
|
417
|
-
}
|
|
418
|
-
return _context7.abrupt("return");
|
|
419
|
-
case 2:
|
|
420
|
-
// 生成批次 ID
|
|
421
|
-
batchId = "batch_".concat(now(), "_").concat(Math.random().toString(36).substring(2, 9));
|
|
422
|
-
sendFn = /*#__PURE__*/function () {
|
|
423
|
-
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
424
|
-
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
425
|
-
while (1) switch (_context6.prev = _context6.next) {
|
|
426
|
-
case 0:
|
|
427
|
-
_context6.next = 2;
|
|
428
|
-
return defaultTransport(events, _this2.opts);
|
|
429
|
-
case 2:
|
|
430
|
-
case "end":
|
|
431
|
-
return _context6.stop();
|
|
432
|
-
}
|
|
433
|
-
}, _callee6);
|
|
434
|
-
}));
|
|
435
|
-
return function sendFn() {
|
|
436
|
-
return _ref3.apply(this, arguments);
|
|
437
|
-
};
|
|
438
|
-
}(); // 如果启用重试
|
|
439
|
-
if (!(this.opts.enableRetry && this.retryManager)) {
|
|
440
|
-
_context7.next = 9;
|
|
441
|
-
break;
|
|
442
|
-
}
|
|
443
|
-
_context7.next = 7;
|
|
444
|
-
return this.retryManager.executeWithRetry(batchId, sendFn);
|
|
445
|
-
case 7:
|
|
446
|
-
_context7.next = 11;
|
|
447
|
-
break;
|
|
448
|
-
case 9:
|
|
449
|
-
_context7.next = 11;
|
|
450
|
-
return sendFn();
|
|
451
|
-
case 11:
|
|
452
|
-
case "end":
|
|
453
|
-
return _context7.stop();
|
|
454
|
-
}
|
|
455
|
-
}, _callee7, this);
|
|
456
|
-
}));
|
|
457
|
-
function sendBatch(_x3) {
|
|
458
|
-
return _sendBatch.apply(this, arguments);
|
|
459
|
-
}
|
|
460
|
-
return sendBatch;
|
|
461
|
-
}()
|
|
462
|
-
/**
|
|
463
|
-
* 启动批量上报定时器
|
|
464
|
-
*/
|
|
465
|
-
)
|
|
466
|
-
}, {
|
|
467
|
-
key: "startBatchTimer",
|
|
468
|
-
value: function startBatchTimer() {
|
|
469
|
-
var _this3 = this;
|
|
470
|
-
if (this.batchTimer) return;
|
|
471
|
-
this.batchTimer = setInterval(function () {
|
|
472
|
-
if (!_this3.closed && _this3.queueManager && _this3.queueManager.size() > 0) {
|
|
473
|
-
logDebug(_this3.opts.debug, 'Batch timer triggered, flushing queue');
|
|
474
|
-
_this3.flush().catch(function (error) {
|
|
475
|
-
logDebug(_this3.opts.debug, 'Batch timer flush failed', error);
|
|
476
|
-
});
|
|
477
|
-
}
|
|
478
|
-
}, this.opts.batchInterval);
|
|
479
|
-
logDebug(this.opts.debug, "Batch timer started with interval ".concat(this.opts.batchInterval, "ms"));
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
// ========== 自动采集 ===========
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* 监听页面卸载事件
|
|
486
|
-
*/
|
|
487
|
-
}, {
|
|
488
|
-
key: "attachUnloadHandlers",
|
|
489
|
-
value: function attachUnloadHandlers() {
|
|
490
|
-
var _this4 = this;
|
|
491
|
-
// 微信小程序环境
|
|
492
|
-
if (isWeChatMiniProgram()) {
|
|
493
|
-
// 微信小程序使用 App 生命周期
|
|
494
|
-
// 注意:这里需要在 App() 中配置,此处仅做示意
|
|
495
|
-
logDebug(this.opts.debug, 'WeChat MiniProgram environment detected');
|
|
496
|
-
return;
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
// 浏览器环境
|
|
500
|
-
if (!isBrowser()) return;
|
|
501
|
-
var win = window;
|
|
502
|
-
|
|
503
|
-
// 页面可见性变化
|
|
504
|
-
document.addEventListener && document.addEventListener('visibilitychange', function () {
|
|
505
|
-
try {
|
|
506
|
-
if (document.visibilityState === 'hidden') {
|
|
507
|
-
// 页面隐藏时刷新队列
|
|
508
|
-
_this4.flush().catch(function () {
|
|
509
|
-
// 忽略错误
|
|
510
|
-
});
|
|
511
|
-
logDebug(_this4.opts.debug, 'Page hidden, flushed queue');
|
|
512
|
-
}
|
|
513
|
-
} catch (_unused) {
|
|
514
|
-
//
|
|
515
|
-
}
|
|
516
|
-
});
|
|
517
|
-
|
|
518
|
-
// 页面隐藏
|
|
519
|
-
win.addEventListener && win.addEventListener('pagehide', function () {
|
|
520
|
-
try {
|
|
521
|
-
// 页面隐藏时刷新队列(同步)
|
|
522
|
-
logDebug(_this4.opts.debug, 'Page hide, flushed queue');
|
|
523
|
-
_this4.flush().catch(function () {
|
|
524
|
-
// 忽略错误
|
|
525
|
-
});
|
|
526
|
-
logDebug(_this4.opts.debug, 'Page hide, flushed queue');
|
|
527
|
-
} catch (_unused2) {
|
|
528
|
-
//
|
|
529
|
-
}
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
// 页面卸载前
|
|
533
|
-
win.addEventListener && win.addEventListener('beforeunload', function () {
|
|
534
|
-
try {
|
|
535
|
-
// 页面卸载前刷新队列(同步)
|
|
536
|
-
logDebug(_this4.opts.debug, 'Page unload, flushed queue');
|
|
537
|
-
_this4.flush().catch(function () {
|
|
538
|
-
// 忽略错误
|
|
539
|
-
});
|
|
540
|
-
logDebug(_this4.opts.debug, 'Page unload, flushed queue');
|
|
541
|
-
} catch (_unused3) {
|
|
542
|
-
//
|
|
543
|
-
}
|
|
544
|
-
});
|
|
545
|
-
}
|
|
546
|
-
}], [{
|
|
547
|
-
key: "getInstance",
|
|
548
|
-
value: function getInstance() {
|
|
549
|
-
if (!LoggerSDK.instance) {
|
|
550
|
-
LoggerSDK.instance = new LoggerSDK();
|
|
551
|
-
}
|
|
552
|
-
return LoggerSDK.instance;
|
|
553
|
-
}
|
|
554
|
-
}]);
|
|
555
|
-
return LoggerSDK;
|
|
556
|
-
}();
|
|
557
|
-
|
|
558
|
-
// 兼容导出(UMD/ESM)
|
|
559
|
-
_defineProperty(LoggerSDK, "instance", void 0);
|
|
560
|
-
export default LoggerSDK;
|