@pluve/logger-sdk 0.0.6 → 0.0.7
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.md +122 -455
- package/dist/loggerSDK.d.ts +8 -15
- package/dist/loggerSDK.js +118 -131
- package/dist/queueManager.d.ts +0 -4
- package/dist/queueManager.js +10 -25
- package/dist/retryManager.d.ts +2 -2
- package/dist/retryManager.js +36 -35
- package/dist/transportAdapter.js +2 -2
- package/dist/types.d.ts +29 -19
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +43 -1
- package/package.json +2 -2
- package/README.html +0 -982
package/dist/loggerSDK.js
CHANGED
|
@@ -9,14 +9,16 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
|
|
|
9
9
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
10
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
11
|
import { defaultTransport } from "./transportAdapter";
|
|
12
|
-
import { isBrowser, isWeChatMiniProgram, now, getSessionId, getCurrentUrl,
|
|
12
|
+
import { isBrowser, isWeChatMiniProgram, now, getSessionId, getCurrentUrl, collectEnvironmentTags, generateUUID, logDebug } from "./utils";
|
|
13
13
|
import { QueueManager } from "./queueManager";
|
|
14
14
|
import { RetryManager } from "./retryManager";
|
|
15
15
|
export var LoggerSDK = /*#__PURE__*/function () {
|
|
16
|
-
function LoggerSDK(
|
|
16
|
+
function LoggerSDK() {
|
|
17
17
|
_classCallCheck(this, LoggerSDK);
|
|
18
18
|
_defineProperty(this, "opts", void 0);
|
|
19
|
+
/** 事件序列编号,用于事件去重 */
|
|
19
20
|
_defineProperty(this, "seq", 0);
|
|
21
|
+
/** 是否已关闭 */
|
|
20
22
|
_defineProperty(this, "closed", false);
|
|
21
23
|
/** 预收集的环境信息 tags(仅用于初始化上报) */
|
|
22
24
|
_defineProperty(this, "envTags", {});
|
|
@@ -32,9 +34,8 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
32
34
|
_defineProperty(this, "batchTimer", void 0);
|
|
33
35
|
/** 是否正在上报 */
|
|
34
36
|
_defineProperty(this, "isSending", false);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
37
|
+
// 初始化时生成并锁定会话标识
|
|
38
|
+
this.sessionId = getSessionId();
|
|
38
39
|
}
|
|
39
40
|
_createClass(LoggerSDK, [{
|
|
40
41
|
key: "init",
|
|
@@ -43,15 +44,16 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
43
44
|
* 初始化:配置参数、收集环境信息与 UA,并生成 sessionId,完成初始上报
|
|
44
45
|
*/
|
|
45
46
|
function init(options) {
|
|
46
|
-
var _this = this;
|
|
47
47
|
this.opts = {
|
|
48
48
|
endpoint: options.endpoint,
|
|
49
49
|
appId: options.appId || 'unknown',
|
|
50
50
|
env: options.env || 'dev',
|
|
51
51
|
debug: !!options.debug,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
/** 是否启用 gzip 压缩,默认 true */
|
|
53
|
+
enableGzip: options.enableGzip !== false,
|
|
54
|
+
// 默认启用
|
|
55
|
+
/** 最大像素图 URL 长度,默认 1900 */
|
|
56
|
+
maxPixelUrlLen: options.maxPixelUrlLen || 4096,
|
|
55
57
|
// 批量上报配置(默认关闭,需显式开启)
|
|
56
58
|
enableBatch: options.enableBatch === true,
|
|
57
59
|
batchSize: options.batchSize || 10,
|
|
@@ -93,16 +95,20 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
93
95
|
this.attachUnloadHandlers();
|
|
94
96
|
|
|
95
97
|
// 生成并缓存 sessionId(便于 UV 统计),并锁定到实例生命周期
|
|
98
|
+
// TODO 通过后端接口获取 sessionId
|
|
96
99
|
this.sessionId = getSessionId();
|
|
97
100
|
|
|
98
101
|
// 收集 UA / 环境信息并完成初始上报(便于后端统计 UA 和 UV)
|
|
99
|
-
this.envTags = this.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
+
// });
|
|
106
112
|
|
|
107
113
|
// 启动批量上报定时器
|
|
108
114
|
if (this.opts.enableBatch) {
|
|
@@ -110,51 +116,25 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
110
116
|
}
|
|
111
117
|
this.initialized = true;
|
|
112
118
|
}
|
|
113
|
-
}, {
|
|
114
|
-
key: "logDebug",
|
|
115
|
-
value: function logDebug() {
|
|
116
|
-
var _this$opts, _console;
|
|
117
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
118
|
-
args[_key] = arguments[_key];
|
|
119
|
-
}
|
|
120
|
-
if ((_this$opts = this.opts) !== null && _this$opts !== void 0 && _this$opts.debug) (_console = console).debug.apply(_console, ['[LoggerSDK]'].concat(args));
|
|
121
|
-
}
|
|
122
119
|
|
|
123
120
|
/**
|
|
124
|
-
*
|
|
121
|
+
* 设置用户信息
|
|
125
122
|
*/
|
|
126
123
|
}, {
|
|
127
|
-
key: "
|
|
128
|
-
value: function
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
// 浏览器环境
|
|
135
|
-
if (envInfo.platform === 'browser' && envInfo.userAgent) {
|
|
136
|
-
var browserInfo = parseBrowserInfo(envInfo.userAgent);
|
|
137
|
-
tags.browser = browserInfo.browser;
|
|
138
|
-
tags.browserVersion = browserInfo.browserVersion;
|
|
139
|
-
tags.os = browserInfo.os;
|
|
140
|
-
tags.osVersion = browserInfo.osVersion;
|
|
141
|
-
tags.screenWidth = envInfo.screenWidth;
|
|
142
|
-
tags.screenHeight = envInfo.screenHeight;
|
|
143
|
-
tags.language = envInfo.language;
|
|
124
|
+
key: "identify",
|
|
125
|
+
value: function identify(userId) {
|
|
126
|
+
logDebug(this.opts.debug, 'identify', userId);
|
|
127
|
+
if (this.opts) {
|
|
128
|
+
this.opts.userId = userId;
|
|
144
129
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
tags.screenWidth = envInfo.screenWidth;
|
|
153
|
-
tags.screenHeight = envInfo.screenHeight;
|
|
154
|
-
tags.language = envInfo.language;
|
|
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;
|
|
155
137
|
}
|
|
156
|
-
this.logDebug('Environment tags collected:', tags);
|
|
157
|
-
return tags;
|
|
158
138
|
}
|
|
159
139
|
|
|
160
140
|
/**
|
|
@@ -163,100 +143,105 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
163
143
|
}, {
|
|
164
144
|
key: "track",
|
|
165
145
|
value: (function () {
|
|
166
|
-
var _track = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(
|
|
167
|
-
var
|
|
146
|
+
var _track = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
147
|
+
var _error$stack;
|
|
148
|
+
var message, error, traceId, logLevel, logEvent;
|
|
168
149
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
169
150
|
while (1) switch (_context.prev = _context.next) {
|
|
170
151
|
case 0:
|
|
152
|
+
message = _ref.message, error = _ref.error, traceId = _ref.traceId, logLevel = _ref.logLevel;
|
|
171
153
|
if (!this.closed) {
|
|
172
|
-
_context.next =
|
|
154
|
+
_context.next = 3;
|
|
173
155
|
break;
|
|
174
156
|
}
|
|
175
157
|
return _context.abrupt("return");
|
|
176
|
-
case
|
|
158
|
+
case 3:
|
|
177
159
|
if (this.initialized) {
|
|
178
|
-
_context.next =
|
|
160
|
+
_context.next = 6;
|
|
179
161
|
break;
|
|
180
162
|
}
|
|
181
|
-
this.
|
|
163
|
+
logDebug(this.opts.debug, 'SDK not initialized, skip track');
|
|
182
164
|
return _context.abrupt("return");
|
|
183
|
-
case
|
|
165
|
+
case 6:
|
|
184
166
|
this.seq += 1;
|
|
185
167
|
|
|
186
168
|
// 构建标准化日志格式
|
|
187
169
|
logEvent = {
|
|
170
|
+
/** 日志 ID */
|
|
188
171
|
logId: "".concat(this.opts.appId).concat(generateUUID()).concat(now()),
|
|
189
172
|
// UUID
|
|
190
|
-
|
|
191
|
-
eventType: eventType,
|
|
192
|
-
ts: now(),
|
|
173
|
+
/** 应用标识 */
|
|
193
174
|
appId: this.opts.appId || 'unknown',
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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 */
|
|
198
185
|
url: getCurrentUrl(),
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
|
203
198
|
};
|
|
204
|
-
this.
|
|
199
|
+
logDebug(this.opts.debug, 'track', logEvent);
|
|
205
200
|
|
|
206
201
|
// 如果启用批量上报,添加到队列
|
|
207
202
|
if (!(this.opts.enableBatch && this.queueManager)) {
|
|
208
|
-
_context.next =
|
|
203
|
+
_context.next = 17;
|
|
209
204
|
break;
|
|
210
205
|
}
|
|
211
206
|
this.queueManager.enqueue(logEvent);
|
|
212
207
|
// 如果队列已满,立即上报
|
|
213
208
|
if (!(this.queueManager.size() >= this.opts.batchSize)) {
|
|
214
|
-
_context.next =
|
|
209
|
+
_context.next = 15;
|
|
215
210
|
break;
|
|
216
211
|
}
|
|
217
|
-
this.
|
|
218
|
-
_context.next =
|
|
212
|
+
logDebug(this.opts.debug, 'Queue size reached batch size, flushing immediately');
|
|
213
|
+
_context.next = 15;
|
|
219
214
|
return this.flush();
|
|
220
|
-
case
|
|
221
|
-
_context.next =
|
|
215
|
+
case 15:
|
|
216
|
+
_context.next = 25;
|
|
222
217
|
break;
|
|
223
|
-
case
|
|
224
|
-
_context.prev =
|
|
225
|
-
_context.next =
|
|
218
|
+
case 17:
|
|
219
|
+
_context.prev = 17;
|
|
220
|
+
_context.next = 20;
|
|
226
221
|
return this.sendEvent(logEvent);
|
|
227
|
-
case
|
|
228
|
-
_context.next =
|
|
222
|
+
case 20:
|
|
223
|
+
_context.next = 25;
|
|
229
224
|
break;
|
|
230
|
-
case
|
|
231
|
-
_context.prev =
|
|
232
|
-
_context.t0 = _context["catch"](
|
|
233
|
-
this.
|
|
225
|
+
case 22:
|
|
226
|
+
_context.prev = 22;
|
|
227
|
+
_context.t0 = _context["catch"](17);
|
|
228
|
+
logDebug(this.opts.debug, 'track failed', _context.t0);
|
|
234
229
|
// 静默失败,不影响主流程
|
|
235
|
-
case
|
|
230
|
+
case 25:
|
|
236
231
|
case "end":
|
|
237
232
|
return _context.stop();
|
|
238
233
|
}
|
|
239
|
-
}, _callee, this, [[
|
|
234
|
+
}, _callee, this, [[17, 22]]);
|
|
240
235
|
}));
|
|
241
|
-
function track(_x
|
|
236
|
+
function track(_x) {
|
|
242
237
|
return _track.apply(this, arguments);
|
|
243
238
|
}
|
|
244
239
|
return track;
|
|
245
240
|
}()
|
|
246
|
-
/**
|
|
247
|
-
* 设置用户信息
|
|
248
|
-
*/
|
|
249
|
-
)
|
|
250
|
-
}, {
|
|
251
|
-
key: "identify",
|
|
252
|
-
value: function identify(userId) {
|
|
253
|
-
this.logDebug('identify', userId);
|
|
254
|
-
// userId 将在 track 时传入
|
|
255
|
-
}
|
|
256
|
-
|
|
257
241
|
/**
|
|
258
242
|
* 手动刷新队列,立即上报所有待发送日志
|
|
259
243
|
*/
|
|
244
|
+
)
|
|
260
245
|
}, {
|
|
261
246
|
key: "flush",
|
|
262
247
|
value: (function () {
|
|
@@ -275,11 +260,11 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
275
260
|
_context2.next = 5;
|
|
276
261
|
break;
|
|
277
262
|
}
|
|
278
|
-
this.
|
|
263
|
+
logDebug(this.opts.debug, 'Already sending, skip flush');
|
|
279
264
|
return _context2.abrupt("return");
|
|
280
265
|
case 5:
|
|
281
266
|
this.isSending = true;
|
|
282
|
-
this.
|
|
267
|
+
logDebug(this.opts.debug, "Flushing ".concat(this.queueManager.size(), " events"));
|
|
283
268
|
_context2.prev = 7;
|
|
284
269
|
// 获取所有待发送的日志(不移除)
|
|
285
270
|
events = this.queueManager.peek(this.queueManager.size());
|
|
@@ -294,13 +279,13 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
294
279
|
case 13:
|
|
295
280
|
// 发送成功后,从队列中移除
|
|
296
281
|
this.queueManager.dequeue(events.length);
|
|
297
|
-
this.
|
|
282
|
+
logDebug(this.opts.debug, "Flushed ".concat(events.length, " events successfully"));
|
|
298
283
|
_context2.next = 20;
|
|
299
284
|
break;
|
|
300
285
|
case 17:
|
|
301
286
|
_context2.prev = 17;
|
|
302
287
|
_context2.t0 = _context2["catch"](7);
|
|
303
|
-
this.
|
|
288
|
+
logDebug(this.opts.debug, 'Flush failed', _context2.t0);
|
|
304
289
|
// 失败不移除队列,下次继续重试
|
|
305
290
|
case 20:
|
|
306
291
|
_context2.prev = 20;
|
|
@@ -369,18 +354,18 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
369
354
|
key: "sendEvent",
|
|
370
355
|
value: function () {
|
|
371
356
|
var _sendEvent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(event) {
|
|
372
|
-
var
|
|
357
|
+
var _this = this;
|
|
373
358
|
var sendFn;
|
|
374
359
|
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
375
360
|
while (1) switch (_context5.prev = _context5.next) {
|
|
376
361
|
case 0:
|
|
377
362
|
sendFn = /*#__PURE__*/function () {
|
|
378
|
-
var
|
|
363
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
379
364
|
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
380
365
|
while (1) switch (_context4.prev = _context4.next) {
|
|
381
366
|
case 0:
|
|
382
367
|
_context4.next = 2;
|
|
383
|
-
return defaultTransport(event,
|
|
368
|
+
return defaultTransport(event, _this.opts);
|
|
384
369
|
case 2:
|
|
385
370
|
case "end":
|
|
386
371
|
return _context4.stop();
|
|
@@ -388,7 +373,7 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
388
373
|
}, _callee4);
|
|
389
374
|
}));
|
|
390
375
|
return function sendFn() {
|
|
391
|
-
return
|
|
376
|
+
return _ref2.apply(this, arguments);
|
|
392
377
|
};
|
|
393
378
|
}(); // 如果启用重试
|
|
394
379
|
if (!(this.opts.enableRetry && this.retryManager)) {
|
|
@@ -409,7 +394,7 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
409
394
|
}
|
|
410
395
|
}, _callee5, this);
|
|
411
396
|
}));
|
|
412
|
-
function sendEvent(
|
|
397
|
+
function sendEvent(_x2) {
|
|
413
398
|
return _sendEvent.apply(this, arguments);
|
|
414
399
|
}
|
|
415
400
|
return sendEvent;
|
|
@@ -421,7 +406,7 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
421
406
|
key: "sendBatch",
|
|
422
407
|
value: (function () {
|
|
423
408
|
var _sendBatch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(events) {
|
|
424
|
-
var
|
|
409
|
+
var _this2 = this;
|
|
425
410
|
var batchId, sendFn;
|
|
426
411
|
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
427
412
|
while (1) switch (_context7.prev = _context7.next) {
|
|
@@ -435,12 +420,12 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
435
420
|
// 生成批次 ID
|
|
436
421
|
batchId = "batch_".concat(now(), "_").concat(Math.random().toString(36).substring(2, 9));
|
|
437
422
|
sendFn = /*#__PURE__*/function () {
|
|
438
|
-
var
|
|
423
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
439
424
|
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
440
425
|
while (1) switch (_context6.prev = _context6.next) {
|
|
441
426
|
case 0:
|
|
442
427
|
_context6.next = 2;
|
|
443
|
-
return defaultTransport(events,
|
|
428
|
+
return defaultTransport(events, _this2.opts);
|
|
444
429
|
case 2:
|
|
445
430
|
case "end":
|
|
446
431
|
return _context6.stop();
|
|
@@ -448,7 +433,7 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
448
433
|
}, _callee6);
|
|
449
434
|
}));
|
|
450
435
|
return function sendFn() {
|
|
451
|
-
return
|
|
436
|
+
return _ref3.apply(this, arguments);
|
|
452
437
|
};
|
|
453
438
|
}(); // 如果启用重试
|
|
454
439
|
if (!(this.opts.enableRetry && this.retryManager)) {
|
|
@@ -469,7 +454,7 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
469
454
|
}
|
|
470
455
|
}, _callee7, this);
|
|
471
456
|
}));
|
|
472
|
-
function sendBatch(
|
|
457
|
+
function sendBatch(_x3) {
|
|
473
458
|
return _sendBatch.apply(this, arguments);
|
|
474
459
|
}
|
|
475
460
|
return sendBatch;
|
|
@@ -481,17 +466,17 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
481
466
|
}, {
|
|
482
467
|
key: "startBatchTimer",
|
|
483
468
|
value: function startBatchTimer() {
|
|
484
|
-
var
|
|
469
|
+
var _this3 = this;
|
|
485
470
|
if (this.batchTimer) return;
|
|
486
471
|
this.batchTimer = setInterval(function () {
|
|
487
|
-
if (!
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
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);
|
|
491
476
|
});
|
|
492
477
|
}
|
|
493
478
|
}, this.opts.batchInterval);
|
|
494
|
-
this.
|
|
479
|
+
logDebug(this.opts.debug, "Batch timer started with interval ".concat(this.opts.batchInterval, "ms"));
|
|
495
480
|
}
|
|
496
481
|
|
|
497
482
|
// ========== 自动采集 ===========
|
|
@@ -502,12 +487,12 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
502
487
|
}, {
|
|
503
488
|
key: "attachUnloadHandlers",
|
|
504
489
|
value: function attachUnloadHandlers() {
|
|
505
|
-
var
|
|
490
|
+
var _this4 = this;
|
|
506
491
|
// 微信小程序环境
|
|
507
492
|
if (isWeChatMiniProgram()) {
|
|
508
493
|
// 微信小程序使用 App 生命周期
|
|
509
494
|
// 注意:这里需要在 App() 中配置,此处仅做示意
|
|
510
|
-
this.
|
|
495
|
+
logDebug(this.opts.debug, 'WeChat MiniProgram environment detected');
|
|
511
496
|
return;
|
|
512
497
|
}
|
|
513
498
|
|
|
@@ -520,10 +505,10 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
520
505
|
try {
|
|
521
506
|
if (document.visibilityState === 'hidden') {
|
|
522
507
|
// 页面隐藏时刷新队列
|
|
523
|
-
|
|
508
|
+
_this4.flush().catch(function () {
|
|
524
509
|
// 忽略错误
|
|
525
510
|
});
|
|
526
|
-
|
|
511
|
+
logDebug(_this4.opts.debug, 'Page hidden, flushed queue');
|
|
527
512
|
}
|
|
528
513
|
} catch (_unused) {
|
|
529
514
|
//
|
|
@@ -534,10 +519,11 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
534
519
|
win.addEventListener && win.addEventListener('pagehide', function () {
|
|
535
520
|
try {
|
|
536
521
|
// 页面隐藏时刷新队列(同步)
|
|
537
|
-
|
|
522
|
+
logDebug(_this4.opts.debug, 'Page hide, flushed queue');
|
|
523
|
+
_this4.flush().catch(function () {
|
|
538
524
|
// 忽略错误
|
|
539
525
|
});
|
|
540
|
-
|
|
526
|
+
logDebug(_this4.opts.debug, 'Page hide, flushed queue');
|
|
541
527
|
} catch (_unused2) {
|
|
542
528
|
//
|
|
543
529
|
}
|
|
@@ -547,10 +533,11 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
547
533
|
win.addEventListener && win.addEventListener('beforeunload', function () {
|
|
548
534
|
try {
|
|
549
535
|
// 页面卸载前刷新队列(同步)
|
|
550
|
-
|
|
536
|
+
logDebug(_this4.opts.debug, 'Page unload, flushed queue');
|
|
537
|
+
_this4.flush().catch(function () {
|
|
551
538
|
// 忽略错误
|
|
552
539
|
});
|
|
553
|
-
|
|
540
|
+
logDebug(_this4.opts.debug, 'Page unload, flushed queue');
|
|
554
541
|
} catch (_unused3) {
|
|
555
542
|
//
|
|
556
543
|
}
|
package/dist/queueManager.d.ts
CHANGED
package/dist/queueManager.js
CHANGED
|
@@ -9,12 +9,12 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
|
|
|
9
9
|
* @Author : 黄震 huangzhen@yfpharmacy.com
|
|
10
10
|
* @Date : 2025-12-18
|
|
11
11
|
* @LastEditors : 黄震 huangzhen@yfpharmacy.com
|
|
12
|
-
* @LastEditTime :
|
|
12
|
+
* @LastEditTime : 2026-01-14 09:33:41
|
|
13
13
|
* @Description : 队列管理器 - 支持内存队列和持久化存储
|
|
14
14
|
* Copyright (c) 2025 by 益丰大药房连锁股份有限公司, All Rights Reserved.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import { isBrowser, isWeChatMiniProgram, safeStringify } from "./utils";
|
|
17
|
+
import { isBrowser, isWeChatMiniProgram, logDebug, safeStringify } from "./utils";
|
|
18
18
|
|
|
19
19
|
/** 队列配置选项 */
|
|
20
20
|
|
|
@@ -47,10 +47,10 @@ export var QueueManager = /*#__PURE__*/function () {
|
|
|
47
47
|
// 队列已满,丢弃最旧的日志
|
|
48
48
|
if (this.queue.length >= this.opts.maxSize) {
|
|
49
49
|
this.queue.shift();
|
|
50
|
-
this.
|
|
50
|
+
logDebug(this.opts.debug, 'Queue full, dropped oldest event');
|
|
51
51
|
}
|
|
52
52
|
this.queue.push(event);
|
|
53
|
-
this.
|
|
53
|
+
logDebug(this.opts.debug, 'Enqueued event', event);
|
|
54
54
|
|
|
55
55
|
// 持久化到存储
|
|
56
56
|
if (this.opts.enableStorage) {
|
|
@@ -75,7 +75,7 @@ export var QueueManager = /*#__PURE__*/function () {
|
|
|
75
75
|
key: "dequeue",
|
|
76
76
|
value: function dequeue(count) {
|
|
77
77
|
var items = this.queue.splice(0, count);
|
|
78
|
-
this.
|
|
78
|
+
logDebug(this.opts.debug, "Dequeued ".concat(items.length, " events"));
|
|
79
79
|
|
|
80
80
|
// 更新持久化存储
|
|
81
81
|
if (this.opts.enableStorage) {
|
|
@@ -100,7 +100,7 @@ export var QueueManager = /*#__PURE__*/function () {
|
|
|
100
100
|
key: "clear",
|
|
101
101
|
value: function clear() {
|
|
102
102
|
this.queue = [];
|
|
103
|
-
this.
|
|
103
|
+
logDebug(this.opts.debug, 'Queue cleared');
|
|
104
104
|
if (this.opts.enableStorage) {
|
|
105
105
|
this.removeFromStorage();
|
|
106
106
|
}
|
|
@@ -128,11 +128,11 @@ export var QueueManager = /*#__PURE__*/function () {
|
|
|
128
128
|
var parsed = JSON.parse(stored);
|
|
129
129
|
if (Array.isArray(parsed)) {
|
|
130
130
|
this.queue = parsed.slice(0, this.opts.maxSize);
|
|
131
|
-
this.
|
|
131
|
+
logDebug(this.opts.debug, "Loaded ".concat(this.queue.length, " events from storage"));
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
} catch (error) {
|
|
135
|
-
this.
|
|
135
|
+
logDebug(this.opts.debug, 'Failed to load queue from storage', error);
|
|
136
136
|
// 加载失败不影响运行,使用空队列
|
|
137
137
|
}
|
|
138
138
|
}
|
|
@@ -156,7 +156,7 @@ export var QueueManager = /*#__PURE__*/function () {
|
|
|
156
156
|
localStorage.setItem(this.storageKey, data);
|
|
157
157
|
}
|
|
158
158
|
} catch (error) {
|
|
159
|
-
this.
|
|
159
|
+
logDebug(this.opts.debug, 'Failed to save queue to storage', error);
|
|
160
160
|
// 保存失败不影响运行
|
|
161
161
|
}
|
|
162
162
|
}
|
|
@@ -178,22 +178,7 @@ export var QueueManager = /*#__PURE__*/function () {
|
|
|
178
178
|
localStorage.removeItem(this.storageKey);
|
|
179
179
|
}
|
|
180
180
|
} catch (error) {
|
|
181
|
-
this.
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* 调试日志
|
|
187
|
-
*/
|
|
188
|
-
}, {
|
|
189
|
-
key: "logDebug",
|
|
190
|
-
value: function logDebug() {
|
|
191
|
-
if (this.opts.debug) {
|
|
192
|
-
var _console;
|
|
193
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
194
|
-
args[_key] = arguments[_key];
|
|
195
|
-
}
|
|
196
|
-
(_console = console).debug.apply(_console, ['[QueueManager]'].concat(args));
|
|
181
|
+
logDebug(this.opts.debug, 'Failed to remove queue from storage', error);
|
|
197
182
|
}
|
|
198
183
|
}
|
|
199
184
|
}]);
|
package/dist/retryManager.d.ts
CHANGED
|
@@ -37,11 +37,11 @@ export declare class RetryManager {
|
|
|
37
37
|
* @param useBackoff - 是否使用指数退避
|
|
38
38
|
* @returns 延迟时间(毫秒)
|
|
39
39
|
*/
|
|
40
|
-
private calculateDelay;
|
|
40
|
+
private static calculateDelay;
|
|
41
41
|
/**
|
|
42
42
|
* 睡眠函数
|
|
43
43
|
*/
|
|
44
|
-
private sleep;
|
|
44
|
+
private static sleep;
|
|
45
45
|
/**
|
|
46
46
|
* 获取正在重试的任务数量
|
|
47
47
|
*/
|