@pluve/logger-sdk 0.0.3 → 0.0.5
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 +226 -3
- package/dist/index.d.ts +9 -0
- package/dist/index.js +6 -2
- package/dist/loggerSDK.d.ts +26 -2
- package/dist/loggerSDK.js +334 -31
- package/dist/queueManager.d.ts +59 -0
- package/dist/queueManager.js +201 -0
- package/dist/retryManager.d.ts +57 -0
- package/dist/retryManager.js +223 -0
- package/dist/transportAdapter.js +34 -10
- package/dist/types.d.ts +25 -1
- package/dist/utils.d.ts +17 -0
- package/dist/utils.js +134 -2
- package/package.json +4 -6
- package/lib/dbQueue.d.ts +0 -10
- package/lib/dbQueue.js +0 -133
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -9
- package/lib/loggerSDK.d.ts +0 -29
- package/lib/loggerSDK.js +0 -571
- package/lib/storeAdapter.d.ts +0 -7
- package/lib/storeAdapter.js +0 -99
- package/lib/transportAdapter.d.ts +0 -66
- package/lib/transportAdapter.js +0 -406
- package/lib/types.d.ts +0 -35
- package/lib/types.js +0 -1
- package/lib/utils.d.ts +0 -5
- package/lib/utils.js +0 -50
package/dist/loggerSDK.js
CHANGED
|
@@ -11,7 +11,9 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
|
|
|
11
11
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
12
12
|
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); }
|
|
13
13
|
import { defaultTransport } from "./transportAdapter";
|
|
14
|
-
import { isBrowser, isWeChatMiniProgram, now, getSessionId, getCurrentUrl, getEnvironmentInfo, parseBrowserInfo } from "./utils";
|
|
14
|
+
import { isBrowser, isWeChatMiniProgram, now, getSessionId, getCurrentUrl, getEnvironmentInfo, parseBrowserInfo, generateUUID } from "./utils";
|
|
15
|
+
import { QueueManager } from "./queueManager";
|
|
16
|
+
import { RetryManager } from "./retryManager";
|
|
15
17
|
export var LoggerSDK = /*#__PURE__*/function () {
|
|
16
18
|
function LoggerSDK(options) {
|
|
17
19
|
_classCallCheck(this, LoggerSDK);
|
|
@@ -20,18 +22,68 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
20
22
|
_defineProperty(this, "closed", false);
|
|
21
23
|
/** 预收集的环境信息 tags */
|
|
22
24
|
_defineProperty(this, "envTags", void 0);
|
|
25
|
+
/** 队列管理器 */
|
|
26
|
+
_defineProperty(this, "queueManager", void 0);
|
|
27
|
+
/** 重试管理器 */
|
|
28
|
+
_defineProperty(this, "retryManager", void 0);
|
|
29
|
+
/** 批量上报定时器 */
|
|
30
|
+
_defineProperty(this, "batchTimer", void 0);
|
|
31
|
+
/** 是否正在上报 */
|
|
32
|
+
_defineProperty(this, "isSending", false);
|
|
23
33
|
this.opts = {
|
|
24
34
|
endpoint: options.endpoint,
|
|
25
35
|
appId: options.appId || 'unknown',
|
|
26
36
|
env: options.env || 'dev',
|
|
27
37
|
debug: !!options.debug,
|
|
28
38
|
pixelParam: options.pixelParam || 'data',
|
|
29
|
-
maxPixelUrlLen: options.maxPixelUrlLen || 1900
|
|
39
|
+
maxPixelUrlLen: options.maxPixelUrlLen || 1900,
|
|
40
|
+
enableGzip: !!options.enableGzip,
|
|
41
|
+
// 批量上报配置
|
|
42
|
+
enableBatch: options.enableBatch !== false,
|
|
43
|
+
// 默认启用
|
|
44
|
+
batchSize: options.batchSize || 10,
|
|
45
|
+
batchInterval: options.batchInterval || 5000,
|
|
46
|
+
maxQueueSize: options.maxQueueSize || 100,
|
|
47
|
+
// 持久化存储配置
|
|
48
|
+
enableStorage: options.enableStorage !== false,
|
|
49
|
+
// 默认启用
|
|
50
|
+
storagePrefix: options.storagePrefix || 'logger_sdk',
|
|
51
|
+
// 重试配置
|
|
52
|
+
enableRetry: options.enableRetry !== false,
|
|
53
|
+
// 默认启用
|
|
54
|
+
maxRetries: options.maxRetries || 3,
|
|
55
|
+
retryDelay: options.retryDelay || 1000,
|
|
56
|
+
retryBackoff: options.retryBackoff !== false // 默认启用
|
|
30
57
|
};
|
|
31
58
|
|
|
32
59
|
// 初始化时收集环境信息
|
|
33
60
|
this.envTags = this.collectEnvironmentTags();
|
|
34
61
|
|
|
62
|
+
// 初始化队列管理器
|
|
63
|
+
if (this.opts.enableBatch) {
|
|
64
|
+
this.queueManager = new QueueManager({
|
|
65
|
+
maxSize: this.opts.maxQueueSize,
|
|
66
|
+
enableStorage: this.opts.enableStorage,
|
|
67
|
+
storagePrefix: this.opts.storagePrefix,
|
|
68
|
+
debug: this.opts.debug
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 初始化重试管理器
|
|
73
|
+
if (this.opts.enableRetry) {
|
|
74
|
+
this.retryManager = new RetryManager({
|
|
75
|
+
maxRetries: this.opts.maxRetries,
|
|
76
|
+
baseDelay: this.opts.retryDelay,
|
|
77
|
+
useBackoff: this.opts.retryBackoff,
|
|
78
|
+
debug: this.opts.debug
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 启动批量上报定时器
|
|
83
|
+
if (this.opts.enableBatch) {
|
|
84
|
+
this.startBatchTimer();
|
|
85
|
+
}
|
|
86
|
+
|
|
35
87
|
// 监听页面卸载事件
|
|
36
88
|
this.attachUnloadHandlers();
|
|
37
89
|
}
|
|
@@ -88,7 +140,7 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
88
140
|
}, {
|
|
89
141
|
key: "track",
|
|
90
142
|
value: (function () {
|
|
91
|
-
var _track = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(eventType, message, options) {
|
|
143
|
+
var _track = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(eventType, message, traceId, options) {
|
|
92
144
|
var logEvent;
|
|
93
145
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
94
146
|
while (1) switch (_context.prev = _context.next) {
|
|
@@ -103,6 +155,9 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
103
155
|
|
|
104
156
|
// 构建标准化日志格式
|
|
105
157
|
logEvent = {
|
|
158
|
+
logId: "".concat(this.opts.appId).concat(generateUUID()).concat(now()),
|
|
159
|
+
// UUID
|
|
160
|
+
traceId: traceId,
|
|
106
161
|
eventType: eventType,
|
|
107
162
|
ts: now(),
|
|
108
163
|
appId: this.opts.appId || 'unknown',
|
|
@@ -117,24 +172,43 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
117
172
|
tags: _objectSpread(_objectSpread({}, this.envTags), (options === null || options === void 0 ? void 0 : options.tags) || {})
|
|
118
173
|
};
|
|
119
174
|
this.logDebug('track', logEvent);
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
175
|
+
|
|
176
|
+
// 如果启用批量上报,添加到队列
|
|
177
|
+
if (!(this.opts.enableBatch && this.queueManager)) {
|
|
178
|
+
_context.next = 13;
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
this.queueManager.enqueue(logEvent);
|
|
182
|
+
// 如果队列已满,立即上报
|
|
183
|
+
if (!(this.queueManager.size() >= this.opts.batchSize)) {
|
|
184
|
+
_context.next = 11;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
this.logDebug('Queue size reached batch size, flushing immediately');
|
|
188
|
+
_context.next = 11;
|
|
189
|
+
return this.flush();
|
|
190
|
+
case 11:
|
|
191
|
+
_context.next = 21;
|
|
192
|
+
break;
|
|
193
|
+
case 13:
|
|
194
|
+
_context.prev = 13;
|
|
195
|
+
_context.next = 16;
|
|
196
|
+
return this.sendEvent(logEvent);
|
|
197
|
+
case 16:
|
|
198
|
+
_context.next = 21;
|
|
125
199
|
break;
|
|
126
|
-
case
|
|
127
|
-
_context.prev =
|
|
128
|
-
_context.t0 = _context["catch"](
|
|
200
|
+
case 18:
|
|
201
|
+
_context.prev = 18;
|
|
202
|
+
_context.t0 = _context["catch"](13);
|
|
129
203
|
this.logDebug('track failed', _context.t0);
|
|
130
204
|
// 静默失败,不影响主流程
|
|
131
|
-
case
|
|
205
|
+
case 21:
|
|
132
206
|
case "end":
|
|
133
207
|
return _context.stop();
|
|
134
208
|
}
|
|
135
|
-
}, _callee, this, [[
|
|
209
|
+
}, _callee, this, [[13, 18]]);
|
|
136
210
|
}));
|
|
137
|
-
function track(_x, _x2, _x3) {
|
|
211
|
+
function track(_x, _x2, _x3, _x4) {
|
|
138
212
|
return _track.apply(this, arguments);
|
|
139
213
|
}
|
|
140
214
|
return track;
|
|
@@ -150,13 +224,241 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
150
224
|
// userId 将在 track 时传入
|
|
151
225
|
}
|
|
152
226
|
|
|
227
|
+
/**
|
|
228
|
+
* 手动刷新队列,立即上报所有待发送日志
|
|
229
|
+
*/
|
|
230
|
+
}, {
|
|
231
|
+
key: "flush",
|
|
232
|
+
value: (function () {
|
|
233
|
+
var _flush = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
234
|
+
var events;
|
|
235
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
236
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
237
|
+
case 0:
|
|
238
|
+
if (!(!this.queueManager || this.queueManager.size() === 0)) {
|
|
239
|
+
_context2.next = 2;
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
return _context2.abrupt("return");
|
|
243
|
+
case 2:
|
|
244
|
+
if (!this.isSending) {
|
|
245
|
+
_context2.next = 5;
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
this.logDebug('Already sending, skip flush');
|
|
249
|
+
return _context2.abrupt("return");
|
|
250
|
+
case 5:
|
|
251
|
+
this.isSending = true;
|
|
252
|
+
this.logDebug("Flushing ".concat(this.queueManager.size(), " events"));
|
|
253
|
+
_context2.prev = 7;
|
|
254
|
+
// 获取所有待发送的日志(不移除)
|
|
255
|
+
events = this.queueManager.peek(this.queueManager.size());
|
|
256
|
+
if (!(events.length === 0)) {
|
|
257
|
+
_context2.next = 11;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
return _context2.abrupt("return");
|
|
261
|
+
case 11:
|
|
262
|
+
_context2.next = 13;
|
|
263
|
+
return this.sendBatch(events);
|
|
264
|
+
case 13:
|
|
265
|
+
// 发送成功后,从队列中移除
|
|
266
|
+
this.queueManager.dequeue(events.length);
|
|
267
|
+
this.logDebug("Flushed ".concat(events.length, " events successfully"));
|
|
268
|
+
_context2.next = 20;
|
|
269
|
+
break;
|
|
270
|
+
case 17:
|
|
271
|
+
_context2.prev = 17;
|
|
272
|
+
_context2.t0 = _context2["catch"](7);
|
|
273
|
+
this.logDebug('Flush failed', _context2.t0);
|
|
274
|
+
// 失败不移除队列,下次继续重试
|
|
275
|
+
case 20:
|
|
276
|
+
_context2.prev = 20;
|
|
277
|
+
this.isSending = false;
|
|
278
|
+
return _context2.finish(20);
|
|
279
|
+
case 23:
|
|
280
|
+
case "end":
|
|
281
|
+
return _context2.stop();
|
|
282
|
+
}
|
|
283
|
+
}, _callee2, this, [[7, 17, 20, 23]]);
|
|
284
|
+
}));
|
|
285
|
+
function flush() {
|
|
286
|
+
return _flush.apply(this, arguments);
|
|
287
|
+
}
|
|
288
|
+
return flush;
|
|
289
|
+
}()
|
|
153
290
|
/**
|
|
154
291
|
* 销毁实例
|
|
155
292
|
*/
|
|
293
|
+
)
|
|
156
294
|
}, {
|
|
157
295
|
key: "destroy",
|
|
158
|
-
value: function
|
|
159
|
-
|
|
296
|
+
value: (function () {
|
|
297
|
+
var _destroy = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
298
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
299
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
300
|
+
case 0:
|
|
301
|
+
this.closed = true;
|
|
302
|
+
|
|
303
|
+
// 停止批量上报定时器
|
|
304
|
+
if (this.batchTimer) {
|
|
305
|
+
clearInterval(this.batchTimer);
|
|
306
|
+
this.batchTimer = null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// 刷新队列
|
|
310
|
+
_context3.next = 4;
|
|
311
|
+
return this.flush();
|
|
312
|
+
case 4:
|
|
313
|
+
// 清理资源
|
|
314
|
+
if (this.queueManager) {
|
|
315
|
+
this.queueManager.clear();
|
|
316
|
+
}
|
|
317
|
+
if (this.retryManager) {
|
|
318
|
+
this.retryManager.clear();
|
|
319
|
+
}
|
|
320
|
+
case 6:
|
|
321
|
+
case "end":
|
|
322
|
+
return _context3.stop();
|
|
323
|
+
}
|
|
324
|
+
}, _callee3, this);
|
|
325
|
+
}));
|
|
326
|
+
function destroy() {
|
|
327
|
+
return _destroy.apply(this, arguments);
|
|
328
|
+
}
|
|
329
|
+
return destroy;
|
|
330
|
+
}() // ========== 内部方法 ===========
|
|
331
|
+
/**
|
|
332
|
+
* 发送单个事件(带重试)
|
|
333
|
+
*/
|
|
334
|
+
)
|
|
335
|
+
}, {
|
|
336
|
+
key: "sendEvent",
|
|
337
|
+
value: function () {
|
|
338
|
+
var _sendEvent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(event) {
|
|
339
|
+
var _this = this;
|
|
340
|
+
var sendFn;
|
|
341
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
342
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
343
|
+
case 0:
|
|
344
|
+
sendFn = /*#__PURE__*/function () {
|
|
345
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
346
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
347
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
348
|
+
case 0:
|
|
349
|
+
_context4.next = 2;
|
|
350
|
+
return defaultTransport(event, _this.opts);
|
|
351
|
+
case 2:
|
|
352
|
+
case "end":
|
|
353
|
+
return _context4.stop();
|
|
354
|
+
}
|
|
355
|
+
}, _callee4);
|
|
356
|
+
}));
|
|
357
|
+
return function sendFn() {
|
|
358
|
+
return _ref.apply(this, arguments);
|
|
359
|
+
};
|
|
360
|
+
}(); // 如果启用重试
|
|
361
|
+
if (!(this.opts.enableRetry && this.retryManager)) {
|
|
362
|
+
_context5.next = 6;
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
_context5.next = 4;
|
|
366
|
+
return this.retryManager.executeWithRetry(event.logId, sendFn);
|
|
367
|
+
case 4:
|
|
368
|
+
_context5.next = 8;
|
|
369
|
+
break;
|
|
370
|
+
case 6:
|
|
371
|
+
_context5.next = 8;
|
|
372
|
+
return sendFn();
|
|
373
|
+
case 8:
|
|
374
|
+
case "end":
|
|
375
|
+
return _context5.stop();
|
|
376
|
+
}
|
|
377
|
+
}, _callee5, this);
|
|
378
|
+
}));
|
|
379
|
+
function sendEvent(_x5) {
|
|
380
|
+
return _sendEvent.apply(this, arguments);
|
|
381
|
+
}
|
|
382
|
+
return sendEvent;
|
|
383
|
+
}()
|
|
384
|
+
/**
|
|
385
|
+
* 批量发送事件(带重试)
|
|
386
|
+
*/
|
|
387
|
+
}, {
|
|
388
|
+
key: "sendBatch",
|
|
389
|
+
value: (function () {
|
|
390
|
+
var _sendBatch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(events) {
|
|
391
|
+
var _this2 = this;
|
|
392
|
+
var batchId, sendFn;
|
|
393
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
394
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
395
|
+
case 0:
|
|
396
|
+
if (!(events.length === 0)) {
|
|
397
|
+
_context7.next = 2;
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
return _context7.abrupt("return");
|
|
401
|
+
case 2:
|
|
402
|
+
// 生成批次 ID
|
|
403
|
+
batchId = "batch_".concat(now(), "_").concat(Math.random().toString(36).substring(2, 9));
|
|
404
|
+
sendFn = /*#__PURE__*/function () {
|
|
405
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
406
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
407
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
408
|
+
case 0:
|
|
409
|
+
_context6.next = 2;
|
|
410
|
+
return defaultTransport(events, _this2.opts);
|
|
411
|
+
case 2:
|
|
412
|
+
case "end":
|
|
413
|
+
return _context6.stop();
|
|
414
|
+
}
|
|
415
|
+
}, _callee6);
|
|
416
|
+
}));
|
|
417
|
+
return function sendFn() {
|
|
418
|
+
return _ref2.apply(this, arguments);
|
|
419
|
+
};
|
|
420
|
+
}(); // 如果启用重试
|
|
421
|
+
if (!(this.opts.enableRetry && this.retryManager)) {
|
|
422
|
+
_context7.next = 9;
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
_context7.next = 7;
|
|
426
|
+
return this.retryManager.executeWithRetry(batchId, sendFn);
|
|
427
|
+
case 7:
|
|
428
|
+
_context7.next = 11;
|
|
429
|
+
break;
|
|
430
|
+
case 9:
|
|
431
|
+
_context7.next = 11;
|
|
432
|
+
return sendFn();
|
|
433
|
+
case 11:
|
|
434
|
+
case "end":
|
|
435
|
+
return _context7.stop();
|
|
436
|
+
}
|
|
437
|
+
}, _callee7, this);
|
|
438
|
+
}));
|
|
439
|
+
function sendBatch(_x6) {
|
|
440
|
+
return _sendBatch.apply(this, arguments);
|
|
441
|
+
}
|
|
442
|
+
return sendBatch;
|
|
443
|
+
}()
|
|
444
|
+
/**
|
|
445
|
+
* 启动批量上报定时器
|
|
446
|
+
*/
|
|
447
|
+
)
|
|
448
|
+
}, {
|
|
449
|
+
key: "startBatchTimer",
|
|
450
|
+
value: function startBatchTimer() {
|
|
451
|
+
var _this3 = this;
|
|
452
|
+
if (this.batchTimer) return;
|
|
453
|
+
this.batchTimer = setInterval(function () {
|
|
454
|
+
if (!_this3.closed && _this3.queueManager && _this3.queueManager.size() > 0) {
|
|
455
|
+
_this3.logDebug('Batch timer triggered, flushing queue');
|
|
456
|
+
_this3.flush().catch(function (error) {
|
|
457
|
+
_this3.logDebug('Batch timer flush failed', error);
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
}, this.opts.batchInterval);
|
|
461
|
+
this.logDebug("Batch timer started with interval ".concat(this.opts.batchInterval, "ms"));
|
|
160
462
|
}
|
|
161
463
|
|
|
162
464
|
// ========== 自动采集 ===========
|
|
@@ -167,6 +469,7 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
167
469
|
}, {
|
|
168
470
|
key: "attachUnloadHandlers",
|
|
169
471
|
value: function attachUnloadHandlers() {
|
|
472
|
+
var _this4 = this;
|
|
170
473
|
// 微信小程序环境
|
|
171
474
|
if (isWeChatMiniProgram()) {
|
|
172
475
|
// 微信小程序使用 App 生命周期
|
|
@@ -183,11 +486,11 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
183
486
|
document.addEventListener && document.addEventListener('visibilitychange', function () {
|
|
184
487
|
try {
|
|
185
488
|
if (document.visibilityState === 'hidden') {
|
|
186
|
-
//
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
console.log('Page hidden');
|
|
489
|
+
// 页面隐藏时刷新队列
|
|
490
|
+
_this4.flush().catch(function () {
|
|
491
|
+
// 忽略错误
|
|
492
|
+
});
|
|
493
|
+
console.log('Page hidden, flushed queue');
|
|
191
494
|
}
|
|
192
495
|
} catch (_unused) {
|
|
193
496
|
//
|
|
@@ -197,11 +500,11 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
197
500
|
// 页面隐藏
|
|
198
501
|
win.addEventListener && win.addEventListener('pagehide', function () {
|
|
199
502
|
try {
|
|
200
|
-
//
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
console.log('Page hide');
|
|
503
|
+
// 页面隐藏时刷新队列(同步)
|
|
504
|
+
_this4.flush().catch(function () {
|
|
505
|
+
// 忽略错误
|
|
506
|
+
});
|
|
507
|
+
console.log('Page hide, flushed queue');
|
|
205
508
|
} catch (_unused2) {
|
|
206
509
|
//
|
|
207
510
|
}
|
|
@@ -210,11 +513,11 @@ export var LoggerSDK = /*#__PURE__*/function () {
|
|
|
210
513
|
// 页面卸载前
|
|
211
514
|
win.addEventListener && win.addEventListener('beforeunload', function () {
|
|
212
515
|
try {
|
|
213
|
-
//
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
console.log('Page unload');
|
|
516
|
+
// 页面卸载前刷新队列(同步)
|
|
517
|
+
_this4.flush().catch(function () {
|
|
518
|
+
// 忽略错误
|
|
519
|
+
});
|
|
520
|
+
console.log('Page unload, flushed queue');
|
|
218
521
|
} catch (_unused3) {
|
|
219
522
|
//
|
|
220
523
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { LogEvent } from './types';
|
|
2
|
+
/** 队列配置选项 */
|
|
3
|
+
export interface QueueOptions {
|
|
4
|
+
/** 队列最大长度 */
|
|
5
|
+
maxSize: number;
|
|
6
|
+
/** 是否启用持久化存储 */
|
|
7
|
+
enableStorage: boolean;
|
|
8
|
+
/** 存储 key 前缀 */
|
|
9
|
+
storagePrefix: string;
|
|
10
|
+
/** 调试模式 */
|
|
11
|
+
debug?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 队列管理器
|
|
15
|
+
* - 内存队列:快速访问
|
|
16
|
+
* - 持久化存储:防止页面刷新/关闭时数据丢失
|
|
17
|
+
*/
|
|
18
|
+
export declare class QueueManager {
|
|
19
|
+
private queue;
|
|
20
|
+
private opts;
|
|
21
|
+
private storageKey;
|
|
22
|
+
constructor(options: QueueOptions);
|
|
23
|
+
/**
|
|
24
|
+
* 添加日志到队列
|
|
25
|
+
*/
|
|
26
|
+
enqueue(event: LogEvent): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* 批量获取日志(不移除)
|
|
29
|
+
*/
|
|
30
|
+
peek(count: number): LogEvent[];
|
|
31
|
+
/**
|
|
32
|
+
* 批量移除日志
|
|
33
|
+
*/
|
|
34
|
+
dequeue(count: number): LogEvent[];
|
|
35
|
+
/**
|
|
36
|
+
* 获取队列长度
|
|
37
|
+
*/
|
|
38
|
+
size(): number;
|
|
39
|
+
/**
|
|
40
|
+
* 清空队列
|
|
41
|
+
*/
|
|
42
|
+
clear(): void;
|
|
43
|
+
/**
|
|
44
|
+
* 从持久化存储加载队列
|
|
45
|
+
*/
|
|
46
|
+
private loadFromStorage;
|
|
47
|
+
/**
|
|
48
|
+
* 保存队列到持久化存储
|
|
49
|
+
*/
|
|
50
|
+
private saveToStorage;
|
|
51
|
+
/**
|
|
52
|
+
* 从持久化存储移除队列
|
|
53
|
+
*/
|
|
54
|
+
private removeFromStorage;
|
|
55
|
+
/**
|
|
56
|
+
* 调试日志
|
|
57
|
+
*/
|
|
58
|
+
private logDebug;
|
|
59
|
+
}
|