@be-link/cls-logger 1.0.10 → 1.0.12
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 +433 -65
- package/dist/ClsLoggerCore.d.ts +37 -5
- package/dist/ClsLoggerCore.d.ts.map +1 -1
- package/dist/index.esm.js +384 -67
- package/dist/index.js +384 -67
- package/dist/index.umd.js +384 -67
- package/dist/mini/behaviorMonitor.d.ts.map +1 -1
- package/dist/mini/deviceInfo.d.ts.map +1 -1
- package/dist/mini/errorMonitor.d.ts.map +1 -1
- package/dist/mini.esm.js +283 -54
- package/dist/mini.js +283 -54
- package/dist/types.d.ts +48 -9
- package/dist/types.d.ts.map +1 -1
- package/dist/web/deviceInfo.d.ts.map +1 -1
- package/dist/web/errorMonitor.d.ts.map +1 -1
- package/dist/web/performanceMonitor.d.ts.map +1 -1
- package/dist/web.esm.js +308 -43
- package/dist/web.js +308 -43
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"behaviorMonitor.d.ts","sourceRoot":"","sources":["../../src/mini/behaviorMonitor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,UAAU,EAAU,MAAM,UAAU,CAAC;AAG3E,KAAK,QAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"behaviorMonitor.d.ts","sourceRoot":"","sources":["../../src/mini/behaviorMonitor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,UAAU,EAAU,MAAM,UAAU,CAAC;AAG3E,KAAK,QAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;AAgFzD,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,QAAQ,EAAE,OAAO,GAAE,sBAA2B,GAAG,MAAM,IAAI,CA6N7G"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deviceInfo.d.ts","sourceRoot":"","sources":["../../src/mini/deviceInfo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"deviceInfo.d.ts","sourceRoot":"","sources":["../../src/mini/deviceInfo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAuE9D,wBAAgB,8BAA8B,CAC5C,IAAI,EAAE,OAAO,GAAG,iBAAiB,GAAG,SAAS,GAC5C,CAAC,MAAM,UAAU,CAAC,GAAG,IAAI,CAqC3B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorMonitor.d.ts","sourceRoot":"","sources":["../../src/mini/errorMonitor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAGhE,KAAK,QAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"errorMonitor.d.ts","sourceRoot":"","sources":["../../src/mini/errorMonitor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAGhE,KAAK,QAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;AAkQzD,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,GAAE,OAAO,GAAG,mBAAmB,GAAG,SAAc,GAAG,IAAI,CAiBpH"}
|
package/dist/mini.esm.js
CHANGED
|
@@ -156,9 +156,9 @@ class ClsLoggerCore {
|
|
|
156
156
|
this.sdkLoaderOverride = null;
|
|
157
157
|
this.client = null;
|
|
158
158
|
this.clientPromise = null;
|
|
159
|
-
this.topicId =
|
|
160
|
-
this.endpoint = 'ap-shanghai.cls.tencentcs.com';
|
|
161
|
-
this.retryTimes =
|
|
159
|
+
this.topicId = '17475bcd-6315-4b20-859c-e7b087fb3683';
|
|
160
|
+
this.endpoint = 'https://ap-shanghai.cls.tencentcs.com';
|
|
161
|
+
this.retryTimes = 3;
|
|
162
162
|
this.source = '127.0.0.1';
|
|
163
163
|
this.enabled = true;
|
|
164
164
|
this.projectId = '';
|
|
@@ -178,6 +178,11 @@ class ClsLoggerCore {
|
|
|
178
178
|
this.batchTimerDueAt = null;
|
|
179
179
|
this.initTs = 0;
|
|
180
180
|
this.startupDelayMs = 0;
|
|
181
|
+
this.startupMaxSize = 0; // 启动窗口内的 maxSize,0 表示使用默认计算值
|
|
182
|
+
this.useIdleCallback = false;
|
|
183
|
+
this.idleTimeout = 3000;
|
|
184
|
+
this.pendingIdleCallback = null; // requestIdleCallback 的 id
|
|
185
|
+
this.visibilityCleanup = null;
|
|
181
186
|
// 参考文档:失败缓存 + 重试
|
|
182
187
|
this.failedCacheKey = 'cls_failed_logs';
|
|
183
188
|
this.failedCacheMax = 200;
|
|
@@ -201,16 +206,16 @@ class ClsLoggerCore {
|
|
|
201
206
|
}
|
|
202
207
|
return 'browser';
|
|
203
208
|
}
|
|
204
|
-
init(options) {
|
|
209
|
+
async init(options) {
|
|
205
210
|
this.initTs = Date.now();
|
|
206
|
-
const topicId = options?.tencentCloud?.topicID ?? options?.topic_id ?? options?.topicID ?? this.topicId
|
|
211
|
+
const topicId = options?.tencentCloud?.topicID ?? options?.topic_id ?? options?.topicID ?? this.topicId;
|
|
207
212
|
const endpoint = options?.tencentCloud?.endpoint ?? options?.endpoint ?? this.endpoint;
|
|
208
213
|
const retryTimes = options?.tencentCloud?.retry_times ?? options?.retry_times ?? this.retryTimes;
|
|
209
|
-
const source = options?.
|
|
214
|
+
const source = options?.source ?? this.source;
|
|
210
215
|
if (!topicId) {
|
|
211
216
|
// eslint-disable-next-line no-console
|
|
212
217
|
console.warn('ClsLogger.init 没有传 topicID/topic_id');
|
|
213
|
-
return;
|
|
218
|
+
return false;
|
|
214
219
|
}
|
|
215
220
|
const nextEnvType = options.envType ?? this.detectEnvType();
|
|
216
221
|
// envType/endpoint/retryTimes 变化时:重置 client(以及可能的 sdk)
|
|
@@ -247,15 +252,21 @@ class ClsLoggerCore {
|
|
|
247
252
|
this.batchMaxSize = options.batch?.maxSize ?? this.batchMaxSize;
|
|
248
253
|
this.batchIntervalMs = options.batch?.intervalMs ?? this.batchIntervalMs;
|
|
249
254
|
this.startupDelayMs = options.batch?.startupDelayMs ?? this.startupDelayMs;
|
|
255
|
+
this.useIdleCallback = options.batch?.useIdleCallback ?? this.useIdleCallback;
|
|
256
|
+
this.idleTimeout = options.batch?.idleTimeout ?? this.idleTimeout;
|
|
257
|
+
// startupMaxSize:启动窗口内的队列阈值,默认为 maxSize * 10(至少 200)
|
|
258
|
+
this.startupMaxSize = options.batch?.startupMaxSize ?? Math.max(this.batchMaxSize * 10, 200);
|
|
250
259
|
this.failedCacheKey = options.failedCacheKey ?? this.failedCacheKey;
|
|
251
260
|
this.failedCacheMax = options.failedCacheMax ?? this.failedCacheMax;
|
|
252
261
|
// 预热(避免首条日志触发 import/初始化开销)
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
262
|
+
const sdkReadyPromise = this.getInstance()
|
|
263
|
+
.then(() => true)
|
|
264
|
+
.catch(() => false);
|
|
256
265
|
if (this.enabled) {
|
|
257
266
|
// 启动时尝试发送失败缓存
|
|
258
267
|
this.flushFailed();
|
|
268
|
+
// 添加页面可见性监听(确保页面关闭时数据不丢失)
|
|
269
|
+
this.setupVisibilityListener();
|
|
259
270
|
// 初始化后立即启动请求监听
|
|
260
271
|
this.startRequestMonitor(options.requestMonitor);
|
|
261
272
|
// 初始化后立即启动错误监控/性能监控
|
|
@@ -264,6 +275,7 @@ class ClsLoggerCore {
|
|
|
264
275
|
// 初始化后立即启动行为埋点(PV/UV/点击)
|
|
265
276
|
this.startBehaviorMonitor(options.behaviorMonitor);
|
|
266
277
|
}
|
|
278
|
+
return sdkReadyPromise;
|
|
267
279
|
}
|
|
268
280
|
getBaseFields() {
|
|
269
281
|
let auto = undefined;
|
|
@@ -292,6 +304,96 @@ class ClsLoggerCore {
|
|
|
292
304
|
return auto;
|
|
293
305
|
return undefined;
|
|
294
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* 设置页面可见性监听
|
|
309
|
+
* - visibilitychange: 页面隐藏时使用 sendBeacon 发送队列
|
|
310
|
+
* - pagehide: 作为移动端 fallback
|
|
311
|
+
*/
|
|
312
|
+
setupVisibilityListener() {
|
|
313
|
+
if (typeof document === 'undefined' || typeof window === 'undefined')
|
|
314
|
+
return;
|
|
315
|
+
// 避免重复监听
|
|
316
|
+
if (this.visibilityCleanup)
|
|
317
|
+
return;
|
|
318
|
+
const handleVisibilityChange = () => {
|
|
319
|
+
if (document.visibilityState === 'hidden') {
|
|
320
|
+
// 使用微任务延迟 flush,确保 web-vitals 等第三方库的 visibilitychange 回调先执行
|
|
321
|
+
// 这样 LCP/CLS/INP 等指标能先入队,再被 flush 发送
|
|
322
|
+
// 注意:queueMicrotask 比 setTimeout(0) 更可靠,不会被延迟太久
|
|
323
|
+
queueMicrotask(() => {
|
|
324
|
+
this.flushBatchSync();
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
const handlePageHide = () => {
|
|
329
|
+
// pagehide 不能延迟,因为浏览器可能立即关闭页面
|
|
330
|
+
// 但 pagehide 通常在 visibilitychange 之后触发,此时队列应该已经包含 web-vitals 指标
|
|
331
|
+
this.flushBatchSync();
|
|
332
|
+
};
|
|
333
|
+
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
334
|
+
window.addEventListener('pagehide', handlePageHide);
|
|
335
|
+
this.visibilityCleanup = () => {
|
|
336
|
+
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
337
|
+
window.removeEventListener('pagehide', handlePageHide);
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* 同步发送内存队列(使用 sendBeacon)
|
|
342
|
+
* - 用于页面关闭时确保数据发送
|
|
343
|
+
* - sendBeacon 不可用时降级为缓存到 localStorage
|
|
344
|
+
*/
|
|
345
|
+
flushBatchSync() {
|
|
346
|
+
if (this.memoryQueue.length === 0)
|
|
347
|
+
return;
|
|
348
|
+
// 清除定时器
|
|
349
|
+
if (this.batchTimer) {
|
|
350
|
+
try {
|
|
351
|
+
if (this.useIdleCallback && typeof cancelIdleCallback !== 'undefined') {
|
|
352
|
+
cancelIdleCallback(this.batchTimer);
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
clearTimeout(this.batchTimer);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
// ignore
|
|
360
|
+
}
|
|
361
|
+
this.batchTimer = null;
|
|
362
|
+
}
|
|
363
|
+
this.batchTimerDueAt = null;
|
|
364
|
+
const logs = [...this.memoryQueue];
|
|
365
|
+
this.memoryQueue = [];
|
|
366
|
+
// 优先使用 sendBeacon(页面关闭时可靠发送)
|
|
367
|
+
if (typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function') {
|
|
368
|
+
try {
|
|
369
|
+
const payload = this.buildSendBeaconPayload(logs);
|
|
370
|
+
const blob = new Blob([payload], { type: 'application/json' });
|
|
371
|
+
const url = `${this.endpoint}/structuredlog?topic_id=${this.topicId}`;
|
|
372
|
+
const success = navigator.sendBeacon(url, blob);
|
|
373
|
+
if (!success) {
|
|
374
|
+
// sendBeacon 返回 false 时,降级缓存
|
|
375
|
+
this.cacheFailedReportLogs(logs);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
this.cacheFailedReportLogs(logs);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
// 不支持 sendBeacon,降级缓存到 localStorage
|
|
384
|
+
this.cacheFailedReportLogs(logs);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* 构建 sendBeacon 的 payload
|
|
389
|
+
*/
|
|
390
|
+
buildSendBeaconPayload(logs) {
|
|
391
|
+
const logList = logs.map((log) => this.buildReportFields(log));
|
|
392
|
+
return JSON.stringify({
|
|
393
|
+
source: this.source,
|
|
394
|
+
logs: logList,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
295
397
|
startRequestMonitor(requestMonitor) {
|
|
296
398
|
if (this.requestMonitorStarted)
|
|
297
399
|
return;
|
|
@@ -537,32 +639,75 @@ class ClsLoggerCore {
|
|
|
537
639
|
return;
|
|
538
640
|
}
|
|
539
641
|
this.memoryQueue.push(log);
|
|
540
|
-
|
|
642
|
+
const now = Date.now();
|
|
643
|
+
// 判断是否在启动合并窗口内
|
|
644
|
+
const inStartupWindow = this.startupDelayMs > 0 && now - this.initTs < this.startupDelayMs;
|
|
645
|
+
// 启动窗口内使用 startupMaxSize,正常情况使用 batchMaxSize
|
|
646
|
+
const effectiveMaxSize = inStartupWindow ? this.startupMaxSize : this.batchMaxSize;
|
|
647
|
+
if (this.memoryQueue.length >= effectiveMaxSize) {
|
|
541
648
|
void this.flushBatch();
|
|
542
649
|
return;
|
|
543
650
|
}
|
|
544
|
-
const now = Date.now();
|
|
545
651
|
const desiredDueAt = this.getDesiredBatchFlushDueAt(now);
|
|
546
652
|
const desiredDelay = Math.max(0, desiredDueAt - now);
|
|
547
653
|
if (!this.batchTimer) {
|
|
548
654
|
this.batchTimerDueAt = desiredDueAt;
|
|
655
|
+
this.scheduleFlush(desiredDelay);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
// 启动合并窗口内:如果当前 timer 会"更早"触发,则延后到窗口结束,尽量减少多次发送
|
|
659
|
+
if (this.batchTimerDueAt !== null && this.batchTimerDueAt < desiredDueAt) {
|
|
660
|
+
this.cancelScheduledFlush();
|
|
661
|
+
this.batchTimerDueAt = desiredDueAt;
|
|
662
|
+
this.scheduleFlush(desiredDelay);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* 调度批量发送
|
|
667
|
+
* - 先使用 setTimeout 保证最小延迟(desiredDelay)
|
|
668
|
+
* - 若开启 useIdleCallback,在延迟结束后等待浏览器空闲再执行
|
|
669
|
+
*/
|
|
670
|
+
scheduleFlush(desiredDelay) {
|
|
671
|
+
if (this.useIdleCallback && typeof requestIdleCallback !== 'undefined') {
|
|
672
|
+
// 先 setTimeout 保证最小延迟,再 requestIdleCallback 在空闲时执行
|
|
673
|
+
this.batchTimer = setTimeout(() => {
|
|
674
|
+
const idleId = requestIdleCallback(() => {
|
|
675
|
+
this.pendingIdleCallback = null;
|
|
676
|
+
void this.flushBatch();
|
|
677
|
+
}, { timeout: this.idleTimeout });
|
|
678
|
+
this.pendingIdleCallback = idleId;
|
|
679
|
+
}, desiredDelay);
|
|
680
|
+
}
|
|
681
|
+
else {
|
|
549
682
|
this.batchTimer = setTimeout(() => {
|
|
550
683
|
void this.flushBatch();
|
|
551
684
|
}, desiredDelay);
|
|
552
|
-
return;
|
|
553
685
|
}
|
|
554
|
-
|
|
555
|
-
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* 取消已调度的批量发送
|
|
689
|
+
* - 同时清理 setTimeout 和可能的 requestIdleCallback
|
|
690
|
+
*/
|
|
691
|
+
cancelScheduledFlush() {
|
|
692
|
+
// 清理 setTimeout
|
|
693
|
+
if (this.batchTimer) {
|
|
556
694
|
try {
|
|
557
695
|
clearTimeout(this.batchTimer);
|
|
558
696
|
}
|
|
559
697
|
catch {
|
|
560
698
|
// ignore
|
|
561
699
|
}
|
|
562
|
-
this.
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
700
|
+
this.batchTimer = null;
|
|
701
|
+
}
|
|
702
|
+
// 清理可能的 pendingIdleCallback
|
|
703
|
+
if (this.pendingIdleCallback !== null && typeof cancelIdleCallback !== 'undefined') {
|
|
704
|
+
try {
|
|
705
|
+
cancelIdleCallback(this.pendingIdleCallback);
|
|
706
|
+
}
|
|
707
|
+
catch {
|
|
708
|
+
// ignore
|
|
709
|
+
}
|
|
710
|
+
this.pendingIdleCallback = null;
|
|
566
711
|
}
|
|
567
712
|
}
|
|
568
713
|
getDesiredBatchFlushDueAt(nowTs) {
|
|
@@ -575,7 +720,7 @@ class ClsLoggerCore {
|
|
|
575
720
|
}
|
|
576
721
|
return nowTs + this.batchIntervalMs;
|
|
577
722
|
}
|
|
578
|
-
info(message, data = {}) {
|
|
723
|
+
info(message, data = {}, options) {
|
|
579
724
|
let msg = '';
|
|
580
725
|
let extra = {};
|
|
581
726
|
if (message instanceof Error) {
|
|
@@ -591,9 +736,18 @@ class ClsLoggerCore {
|
|
|
591
736
|
extra = data;
|
|
592
737
|
}
|
|
593
738
|
const payload = normalizeFlatFields({ message: msg, ...extra }, 'info');
|
|
594
|
-
|
|
739
|
+
const log = { type: 'info', data: payload, timestamp: Date.now() };
|
|
740
|
+
// info 默认走批量队列,支持 immediate 选项立即发送
|
|
741
|
+
if (options?.immediate) {
|
|
742
|
+
void this.sendReportLogs([log]).catch(() => {
|
|
743
|
+
this.cacheFailedReportLogs([log]);
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
else {
|
|
747
|
+
this.report(log);
|
|
748
|
+
}
|
|
595
749
|
}
|
|
596
|
-
warn(message, data = {}) {
|
|
750
|
+
warn(message, data = {}, options) {
|
|
597
751
|
let msg = '';
|
|
598
752
|
let extra = {};
|
|
599
753
|
if (message instanceof Error) {
|
|
@@ -609,9 +763,18 @@ class ClsLoggerCore {
|
|
|
609
763
|
extra = data;
|
|
610
764
|
}
|
|
611
765
|
const payload = normalizeFlatFields({ message: msg, ...extra }, 'warn');
|
|
612
|
-
|
|
766
|
+
const log = { type: 'warn', data: payload, timestamp: Date.now() };
|
|
767
|
+
// warn 默认走批量队列,支持 immediate 选项立即发送
|
|
768
|
+
if (options?.immediate) {
|
|
769
|
+
void this.sendReportLogs([log]).catch(() => {
|
|
770
|
+
this.cacheFailedReportLogs([log]);
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
else {
|
|
774
|
+
this.report(log);
|
|
775
|
+
}
|
|
613
776
|
}
|
|
614
|
-
error(message, data = {}) {
|
|
777
|
+
error(message, data = {}, options) {
|
|
615
778
|
let msg = '';
|
|
616
779
|
let extra = {};
|
|
617
780
|
if (message instanceof Error) {
|
|
@@ -627,7 +790,17 @@ class ClsLoggerCore {
|
|
|
627
790
|
extra = data;
|
|
628
791
|
}
|
|
629
792
|
const payload = normalizeFlatFields({ message: msg, ...extra }, 'error');
|
|
630
|
-
|
|
793
|
+
const log = { type: 'error', data: payload, timestamp: Date.now() };
|
|
794
|
+
// error 默认即时上报,除非显式指定 immediate: false
|
|
795
|
+
const immediate = options?.immediate ?? true;
|
|
796
|
+
if (immediate) {
|
|
797
|
+
void this.sendReportLogs([log]).catch(() => {
|
|
798
|
+
this.cacheFailedReportLogs([log]);
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
this.report(log);
|
|
803
|
+
}
|
|
631
804
|
}
|
|
632
805
|
track(trackType, data = {}) {
|
|
633
806
|
if (!trackType)
|
|
@@ -642,10 +815,7 @@ class ClsLoggerCore {
|
|
|
642
815
|
* 立即发送内存队列
|
|
643
816
|
*/
|
|
644
817
|
async flushBatch() {
|
|
645
|
-
|
|
646
|
-
clearTimeout(this.batchTimer);
|
|
647
|
-
this.batchTimer = null;
|
|
648
|
-
}
|
|
818
|
+
this.cancelScheduledFlush();
|
|
649
819
|
this.batchTimerDueAt = null;
|
|
650
820
|
if (this.memoryQueue.length === 0)
|
|
651
821
|
return;
|
|
@@ -751,7 +921,14 @@ class ClsLoggerCore {
|
|
|
751
921
|
// 先清空,再尝试发送
|
|
752
922
|
writeStringStorage(this.failedCacheKey, JSON.stringify([]));
|
|
753
923
|
this.memoryQueue.unshift(...logs);
|
|
754
|
-
|
|
924
|
+
// 触发定时器而非直接 flush,以尊重 startupDelayMs 配置
|
|
925
|
+
if (!this.batchTimer) {
|
|
926
|
+
const now = Date.now();
|
|
927
|
+
const desiredDueAt = this.getDesiredBatchFlushDueAt(now);
|
|
928
|
+
const desiredDelay = Math.max(0, desiredDueAt - now);
|
|
929
|
+
this.batchTimerDueAt = desiredDueAt;
|
|
930
|
+
this.scheduleFlush(desiredDelay);
|
|
931
|
+
}
|
|
755
932
|
}
|
|
756
933
|
/**
|
|
757
934
|
* 统计/计数类日志:按字段展开上报(若 data 为空默认 1)
|
|
@@ -987,6 +1164,13 @@ function installMiniRequestMonitor(report, opts = {}) {
|
|
|
987
1164
|
const DEFAULT_MAX_TEXT = 4000;
|
|
988
1165
|
const DEFAULT_DEDUPE_WINDOW_MS = 3000;
|
|
989
1166
|
const DEFAULT_DEDUPE_MAX_KEYS = 200;
|
|
1167
|
+
/** 默认忽略的无意义错误信息 */
|
|
1168
|
+
const DEFAULT_IGNORE_MESSAGES = [
|
|
1169
|
+
'Script error.',
|
|
1170
|
+
'Script error',
|
|
1171
|
+
/^ResizeObserver loop/,
|
|
1172
|
+
'Permission was denied',
|
|
1173
|
+
];
|
|
990
1174
|
function truncate(s, maxLen) {
|
|
991
1175
|
if (!s)
|
|
992
1176
|
return s;
|
|
@@ -999,12 +1183,33 @@ function sampleHit$1(sampleRate) {
|
|
|
999
1183
|
return false;
|
|
1000
1184
|
return Math.random() < sampleRate;
|
|
1001
1185
|
}
|
|
1186
|
+
/** 检查消息是否应该被忽略 */
|
|
1187
|
+
function shouldIgnoreMessage(message, ignorePatterns) {
|
|
1188
|
+
if (!message || ignorePatterns.length === 0)
|
|
1189
|
+
return false;
|
|
1190
|
+
return ignorePatterns.some((pattern) => {
|
|
1191
|
+
if (typeof pattern === 'string') {
|
|
1192
|
+
return message === pattern || message.includes(pattern);
|
|
1193
|
+
}
|
|
1194
|
+
return pattern.test(message);
|
|
1195
|
+
});
|
|
1196
|
+
}
|
|
1002
1197
|
function getMpPagePath() {
|
|
1003
1198
|
try {
|
|
1004
1199
|
const pages = globalThis.getCurrentPages?.();
|
|
1005
1200
|
if (Array.isArray(pages) && pages.length > 0) {
|
|
1006
1201
|
const page = pages[pages.length - 1];
|
|
1007
|
-
|
|
1202
|
+
const route = page.route || page.__route__;
|
|
1203
|
+
if (typeof route === 'string') {
|
|
1204
|
+
let path = route.startsWith('/') ? route : `/${route}`;
|
|
1205
|
+
const options = page.options || {};
|
|
1206
|
+
const keys = Object.keys(options);
|
|
1207
|
+
if (keys.length > 0) {
|
|
1208
|
+
const qs = keys.map((k) => `${k}=${options[k]}`).join('&');
|
|
1209
|
+
path = `${path}?${qs}`;
|
|
1210
|
+
}
|
|
1211
|
+
return path;
|
|
1212
|
+
}
|
|
1008
1213
|
}
|
|
1009
1214
|
return '';
|
|
1010
1215
|
}
|
|
@@ -1093,6 +1298,9 @@ function installMiniProgramErrorMonitor(report, options) {
|
|
|
1093
1298
|
if (!sampleHit$1(options.sampleRate))
|
|
1094
1299
|
return;
|
|
1095
1300
|
const e = normalizeErrorLike(msg, options.maxTextLength);
|
|
1301
|
+
// 检查是否应该忽略此错误
|
|
1302
|
+
if (shouldIgnoreMessage(e.message, options.ignoreMessages))
|
|
1303
|
+
return;
|
|
1096
1304
|
const payload = {
|
|
1097
1305
|
pagePath: getMpPagePath(),
|
|
1098
1306
|
source: 'wx.onError',
|
|
@@ -1120,6 +1328,9 @@ function installMiniProgramErrorMonitor(report, options) {
|
|
|
1120
1328
|
if (!sampleHit$1(options.sampleRate))
|
|
1121
1329
|
return;
|
|
1122
1330
|
const e = normalizeErrorLike(res?.reason, options.maxTextLength);
|
|
1331
|
+
// 检查是否应该忽略此错误
|
|
1332
|
+
if (shouldIgnoreMessage(e.message, options.ignoreMessages))
|
|
1333
|
+
return;
|
|
1123
1334
|
const payload = {
|
|
1124
1335
|
pagePath: getMpPagePath(),
|
|
1125
1336
|
source: 'wx.onUnhandledRejection',
|
|
@@ -1151,15 +1362,18 @@ function installMiniProgramErrorMonitor(report, options) {
|
|
|
1151
1362
|
try {
|
|
1152
1363
|
if (sampleHit$1(options.sampleRate)) {
|
|
1153
1364
|
const e = normalizeErrorLike(args?.[0], options.maxTextLength);
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1365
|
+
// 检查是否应该忽略此错误
|
|
1366
|
+
if (!shouldIgnoreMessage(e.message, options.ignoreMessages)) {
|
|
1367
|
+
const payload = {
|
|
1368
|
+
pagePath: getMpPagePath(),
|
|
1369
|
+
source: 'App.onError',
|
|
1370
|
+
message: e.message,
|
|
1371
|
+
errorName: e.name,
|
|
1372
|
+
stack: e.stack,
|
|
1373
|
+
};
|
|
1374
|
+
if (shouldReport(buildErrorKey(options.reportType, payload)))
|
|
1375
|
+
report(options.reportType, payload);
|
|
1376
|
+
}
|
|
1163
1377
|
}
|
|
1164
1378
|
}
|
|
1165
1379
|
catch {
|
|
@@ -1175,15 +1389,18 @@ function installMiniProgramErrorMonitor(report, options) {
|
|
|
1175
1389
|
if (sampleHit$1(options.sampleRate)) {
|
|
1176
1390
|
const reason = args?.[0]?.reason ?? args?.[0];
|
|
1177
1391
|
const e = normalizeErrorLike(reason, options.maxTextLength);
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1392
|
+
// 检查是否应该忽略此错误
|
|
1393
|
+
if (!shouldIgnoreMessage(e.message, options.ignoreMessages)) {
|
|
1394
|
+
const payload = {
|
|
1395
|
+
pagePath: getMpPagePath(),
|
|
1396
|
+
source: 'App.onUnhandledRejection',
|
|
1397
|
+
message: e.message,
|
|
1398
|
+
errorName: e.name,
|
|
1399
|
+
stack: e.stack,
|
|
1400
|
+
};
|
|
1401
|
+
if (shouldReport(buildErrorKey(options.reportType, payload)))
|
|
1402
|
+
report(options.reportType, payload);
|
|
1403
|
+
}
|
|
1187
1404
|
}
|
|
1188
1405
|
}
|
|
1189
1406
|
catch {
|
|
@@ -1214,6 +1431,7 @@ function installMiniErrorMonitor(report, opts = {}) {
|
|
|
1214
1431
|
maxTextLength: raw.maxTextLength ?? DEFAULT_MAX_TEXT,
|
|
1215
1432
|
dedupeWindowMs: raw.dedupeWindowMs ?? DEFAULT_DEDUPE_WINDOW_MS,
|
|
1216
1433
|
dedupeMaxKeys: raw.dedupeMaxKeys ?? DEFAULT_DEDUPE_MAX_KEYS,
|
|
1434
|
+
ignoreMessages: raw.ignoreMessages ?? DEFAULT_IGNORE_MESSAGES,
|
|
1217
1435
|
};
|
|
1218
1436
|
installMiniProgramErrorMonitor(report, options);
|
|
1219
1437
|
}
|
|
@@ -1356,7 +1574,16 @@ function getMiniProgramPagePath() {
|
|
|
1356
1574
|
const pages = typeof g.getCurrentPages === 'function' ? g.getCurrentPages() : [];
|
|
1357
1575
|
const last = Array.isArray(pages) ? pages[pages.length - 1] : undefined;
|
|
1358
1576
|
const route = (last?.route || last?.__route__);
|
|
1359
|
-
|
|
1577
|
+
if (typeof route !== 'string')
|
|
1578
|
+
return '';
|
|
1579
|
+
let path = route.startsWith('/') ? route : `/${route}`;
|
|
1580
|
+
const options = last?.options || {};
|
|
1581
|
+
const keys = Object.keys(options);
|
|
1582
|
+
if (keys.length > 0) {
|
|
1583
|
+
const qs = keys.map((k) => `${k}=${options[k]}`).join('&');
|
|
1584
|
+
path = `${path}?${qs}`;
|
|
1585
|
+
}
|
|
1586
|
+
return path;
|
|
1360
1587
|
}
|
|
1361
1588
|
catch {
|
|
1362
1589
|
return '';
|
|
@@ -1619,7 +1846,7 @@ function getMiniProgramDeviceInfo(options) {
|
|
|
1619
1846
|
try {
|
|
1620
1847
|
if (typeof wxAny.getNetworkTypeSync === 'function') {
|
|
1621
1848
|
const n = wxAny.getNetworkTypeSync();
|
|
1622
|
-
out.networkType = n?.networkType ? String(n.networkType) : '';
|
|
1849
|
+
out.networkType = n?.networkType ? String(n.networkType).toUpperCase() : '';
|
|
1623
1850
|
}
|
|
1624
1851
|
else if (typeof wxAny.getNetworkType === 'function') {
|
|
1625
1852
|
// 异步更新:先不阻塞初始化
|
|
@@ -1629,7 +1856,9 @@ function getMiniProgramDeviceInfo(options) {
|
|
|
1629
1856
|
const g = globalThis;
|
|
1630
1857
|
if (!g.__beLinkClsLoggerDeviceInfo__)
|
|
1631
1858
|
g.__beLinkClsLoggerDeviceInfo__ = {};
|
|
1632
|
-
g.__beLinkClsLoggerDeviceInfo__.networkType = res?.networkType
|
|
1859
|
+
g.__beLinkClsLoggerDeviceInfo__.networkType = res?.networkType
|
|
1860
|
+
? String(res.networkType).toUpperCase()
|
|
1861
|
+
: '';
|
|
1633
1862
|
}
|
|
1634
1863
|
catch {
|
|
1635
1864
|
// ignore
|
|
@@ -1648,7 +1877,7 @@ function getMiniProgramDeviceInfo(options) {
|
|
|
1648
1877
|
const g = globalThis;
|
|
1649
1878
|
if (!g.__beLinkClsLoggerDeviceInfo__)
|
|
1650
1879
|
g.__beLinkClsLoggerDeviceInfo__ = {};
|
|
1651
|
-
g.__beLinkClsLoggerDeviceInfo__.networkType = res?.networkType ? String(res.networkType) : '';
|
|
1880
|
+
g.__beLinkClsLoggerDeviceInfo__.networkType = res?.networkType ? String(res.networkType).toUpperCase() : '';
|
|
1652
1881
|
}
|
|
1653
1882
|
catch {
|
|
1654
1883
|
// ignore
|
|
@@ -1689,7 +1918,7 @@ function createMiniDeviceInfoBaseFields(opts) {
|
|
|
1689
1918
|
try {
|
|
1690
1919
|
const g = globalThis;
|
|
1691
1920
|
const extra = g[globalKey];
|
|
1692
|
-
if (extra &&
|
|
1921
|
+
if (extra && typeof extra === 'object')
|
|
1693
1922
|
return { ...base, ...extra };
|
|
1694
1923
|
}
|
|
1695
1924
|
catch {
|