@be-link/cls-logger 1.0.1-beta.9 → 1.0.4
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 +115 -31
- package/dist/ClsLogger.d.ts.map +1 -1
- package/dist/ClsLoggerCore.d.ts +4 -3
- package/dist/ClsLoggerCore.d.ts.map +1 -1
- package/dist/behaviorMonitor.d.ts.map +1 -1
- package/dist/errorMonitor.d.ts.map +1 -1
- package/dist/index.esm.js +176 -44
- package/dist/index.js +176 -44
- package/dist/index.umd.js +176 -44
- package/dist/mini.esm.js +111 -19
- package/dist/mini.js +111 -19
- package/dist/performanceMonitor.d.ts.map +1 -1
- package/dist/types.d.ts +19 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/web.esm.js +111 -19
- package/dist/web.js +111 -19
- package/package.json +1 -1
package/dist/web.js
CHANGED
|
@@ -583,6 +583,19 @@ function getPagePath$1() {
|
|
|
583
583
|
return '';
|
|
584
584
|
}
|
|
585
585
|
}
|
|
586
|
+
function getMpPagePath() {
|
|
587
|
+
try {
|
|
588
|
+
const pages = globalThis.getCurrentPages?.();
|
|
589
|
+
if (Array.isArray(pages) && pages.length > 0) {
|
|
590
|
+
const page = pages[pages.length - 1];
|
|
591
|
+
return page.route || page.__route__ || '';
|
|
592
|
+
}
|
|
593
|
+
return '';
|
|
594
|
+
}
|
|
595
|
+
catch {
|
|
596
|
+
return '';
|
|
597
|
+
}
|
|
598
|
+
}
|
|
586
599
|
function normalizeErrorLike(err, maxTextLength) {
|
|
587
600
|
if (err && typeof err === 'object') {
|
|
588
601
|
const anyErr = err;
|
|
@@ -742,6 +755,7 @@ function installMiniProgramErrorMonitor(report, options) {
|
|
|
742
755
|
return;
|
|
743
756
|
const e = normalizeErrorLike(msg, options.maxTextLength);
|
|
744
757
|
const payload = {
|
|
758
|
+
pagePath: getMpPagePath(),
|
|
745
759
|
source: 'wx.onError',
|
|
746
760
|
message: e.message,
|
|
747
761
|
errorName: e.name,
|
|
@@ -768,6 +782,7 @@ function installMiniProgramErrorMonitor(report, options) {
|
|
|
768
782
|
return;
|
|
769
783
|
const e = normalizeErrorLike(res?.reason, options.maxTextLength);
|
|
770
784
|
const payload = {
|
|
785
|
+
pagePath: getMpPagePath(),
|
|
771
786
|
source: 'wx.onUnhandledRejection',
|
|
772
787
|
message: e.message,
|
|
773
788
|
errorName: e.name,
|
|
@@ -799,6 +814,7 @@ function installMiniProgramErrorMonitor(report, options) {
|
|
|
799
814
|
if (sampleHit$1(options.sampleRate)) {
|
|
800
815
|
const e = normalizeErrorLike(args?.[0], options.maxTextLength);
|
|
801
816
|
const payload = {
|
|
817
|
+
pagePath: getMpPagePath(),
|
|
802
818
|
source: 'App.onError',
|
|
803
819
|
message: e.message,
|
|
804
820
|
errorName: e.name,
|
|
@@ -822,6 +838,7 @@ function installMiniProgramErrorMonitor(report, options) {
|
|
|
822
838
|
const reason = args?.[0]?.reason ?? args?.[0];
|
|
823
839
|
const e = normalizeErrorLike(reason, options.maxTextLength);
|
|
824
840
|
const payload = {
|
|
841
|
+
pagePath: getMpPagePath(),
|
|
825
842
|
source: 'App.onUnhandledRejection',
|
|
826
843
|
message: e.message,
|
|
827
844
|
errorName: e.name,
|
|
@@ -1129,18 +1146,28 @@ function installMiniProgramPerformanceMonitor(report, options) {
|
|
|
1129
1146
|
continue;
|
|
1130
1147
|
// Page Render: firstRender
|
|
1131
1148
|
if (entry.entryType === 'render' && entry.name === 'firstRender') {
|
|
1149
|
+
const duration = typeof entry.duration === 'number'
|
|
1150
|
+
? entry.duration
|
|
1151
|
+
: typeof entry.startTime === 'number' && typeof entry.endTime === 'number'
|
|
1152
|
+
? entry.endTime - entry.startTime
|
|
1153
|
+
: 0;
|
|
1132
1154
|
report(options.reportType, {
|
|
1133
1155
|
metric: 'page-render',
|
|
1134
|
-
duration
|
|
1156
|
+
duration,
|
|
1135
1157
|
pagePath: entry.path || '',
|
|
1136
1158
|
unit: 'ms',
|
|
1137
1159
|
});
|
|
1138
1160
|
}
|
|
1139
1161
|
// Route Switch: route
|
|
1140
1162
|
else if (entry.entryType === 'navigation' && entry.name === 'route') {
|
|
1163
|
+
const duration = typeof entry.duration === 'number'
|
|
1164
|
+
? entry.duration
|
|
1165
|
+
: typeof entry.startTime === 'number' && typeof entry.endTime === 'number'
|
|
1166
|
+
? entry.endTime - entry.startTime
|
|
1167
|
+
: 0;
|
|
1141
1168
|
report(options.reportType, {
|
|
1142
1169
|
metric: 'route',
|
|
1143
|
-
duration
|
|
1170
|
+
duration,
|
|
1144
1171
|
pagePath: entry.path || '',
|
|
1145
1172
|
unit: 'ms',
|
|
1146
1173
|
});
|
|
@@ -1484,7 +1511,7 @@ function installBehaviorMonitor(report, envType, options = {}) {
|
|
|
1484
1511
|
const tag = (el.tagName || '').toLowerCase();
|
|
1485
1512
|
const trackId = getAttr(el, clickTrackIdAttr);
|
|
1486
1513
|
// 过滤无效点击:白名单 tag + 没有 trackId
|
|
1487
|
-
if (clickWhiteList.includes(tag)
|
|
1514
|
+
if (clickWhiteList.includes(tag) || !trackId)
|
|
1488
1515
|
return;
|
|
1489
1516
|
void uvStatePromise.then(({ uvId, meta }) => {
|
|
1490
1517
|
if (destroyed)
|
|
@@ -1525,8 +1552,12 @@ function installBehaviorMonitor(report, envType, options = {}) {
|
|
|
1525
1552
|
g.Page = function patchedPage(conf) {
|
|
1526
1553
|
const originalOnShow = conf?.onShow;
|
|
1527
1554
|
conf.onShow = function (...args) {
|
|
1528
|
-
if (pvEnabled)
|
|
1529
|
-
|
|
1555
|
+
if (pvEnabled) {
|
|
1556
|
+
const pagePath = getPagePath();
|
|
1557
|
+
if (pagePath?.length > 0) {
|
|
1558
|
+
reportPv(pagePath);
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1530
1561
|
return typeof originalOnShow === 'function' ? originalOnShow.apply(this, args) : undefined;
|
|
1531
1562
|
};
|
|
1532
1563
|
// 点击:wrap 页面 methods(bindtap 等会调用到这里的 handler)
|
|
@@ -1880,6 +1911,7 @@ class ClsLoggerCore {
|
|
|
1880
1911
|
this.endpoint = 'ap-shanghai.cls.tencentcs.com';
|
|
1881
1912
|
this.retryTimes = 10;
|
|
1882
1913
|
this.source = '127.0.0.1';
|
|
1914
|
+
this.enabled = true;
|
|
1883
1915
|
this.projectId = '';
|
|
1884
1916
|
this.projectName = '';
|
|
1885
1917
|
this.appId = '';
|
|
@@ -1910,9 +1942,14 @@ class ClsLoggerCore {
|
|
|
1910
1942
|
* 子类可按需重写(默认检测 wx)
|
|
1911
1943
|
*/
|
|
1912
1944
|
detectEnvType() {
|
|
1913
|
-
const
|
|
1914
|
-
|
|
1945
|
+
const g = globalThis;
|
|
1946
|
+
// 微信、支付宝、字节跳动、UniApp 等小程序环境通常都有特定全局变量
|
|
1947
|
+
if ((g.wx && typeof g.wx.getSystemInfoSync === 'function') ||
|
|
1948
|
+
(g.my && typeof g.my.getSystemInfoSync === 'function') ||
|
|
1949
|
+
(g.tt && typeof g.tt.getSystemInfoSync === 'function') ||
|
|
1950
|
+
(g.uni && typeof g.uni.getSystemInfoSync === 'function')) {
|
|
1915
1951
|
return 'miniprogram';
|
|
1952
|
+
}
|
|
1916
1953
|
return 'browser';
|
|
1917
1954
|
}
|
|
1918
1955
|
init(options) {
|
|
@@ -1950,6 +1987,7 @@ class ClsLoggerCore {
|
|
|
1950
1987
|
this.appId = options.appId ?? this.appId;
|
|
1951
1988
|
this.appVersion = options.appVersion ?? this.appVersion;
|
|
1952
1989
|
this.envType = nextEnvType;
|
|
1990
|
+
this.enabled = options.enabled ?? true;
|
|
1953
1991
|
// 可选:外部注入 SDK(优先级:sdkLoader > sdk)
|
|
1954
1992
|
this.sdkLoaderOverride = options.sdkLoader ?? this.sdkLoaderOverride;
|
|
1955
1993
|
this.sdkOverride = options.sdk ?? this.sdkOverride;
|
|
@@ -1966,15 +2004,17 @@ class ClsLoggerCore {
|
|
|
1966
2004
|
void this.getInstance().catch(() => {
|
|
1967
2005
|
// ignore
|
|
1968
2006
|
});
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
2007
|
+
if (this.enabled) {
|
|
2008
|
+
// 启动时尝试发送失败缓存
|
|
2009
|
+
this.flushFailed();
|
|
2010
|
+
// 初始化后立即启动请求监听
|
|
2011
|
+
this.startRequestMonitor(options.requestMonitor);
|
|
2012
|
+
// 初始化后立即启动错误监控/性能监控
|
|
2013
|
+
this.startErrorMonitor(options.errorMonitor);
|
|
2014
|
+
this.startPerformanceMonitor(options.performanceMonitor);
|
|
2015
|
+
// 初始化后立即启动行为埋点(PV/UV/点击)
|
|
2016
|
+
this.startBehaviorMonitor(options.behaviorMonitor);
|
|
2017
|
+
}
|
|
1978
2018
|
}
|
|
1979
2019
|
getBaseFields() {
|
|
1980
2020
|
let auto = undefined;
|
|
@@ -2099,6 +2139,8 @@ class ClsLoggerCore {
|
|
|
2099
2139
|
* - 最终会把 fields 展开成 CLS 的 content(key/value 都会转成 string)
|
|
2100
2140
|
*/
|
|
2101
2141
|
put(fields, options = {}) {
|
|
2142
|
+
if (!this.enabled)
|
|
2143
|
+
return;
|
|
2102
2144
|
if (!fields)
|
|
2103
2145
|
return;
|
|
2104
2146
|
if (!this.topicId) {
|
|
@@ -2157,6 +2199,8 @@ class ClsLoggerCore {
|
|
|
2157
2199
|
* - 埋点入参必须是一维(扁平)Object,非原始值会被 stringify
|
|
2158
2200
|
*/
|
|
2159
2201
|
enqueue(fields, options = {}) {
|
|
2202
|
+
if (!this.enabled)
|
|
2203
|
+
return;
|
|
2160
2204
|
if (!fields)
|
|
2161
2205
|
return;
|
|
2162
2206
|
const time = Date.now();
|
|
@@ -2195,6 +2239,8 @@ class ClsLoggerCore {
|
|
|
2195
2239
|
* 批量上报(每条 item.data 展开为 log content)
|
|
2196
2240
|
*/
|
|
2197
2241
|
putBatch(queue) {
|
|
2242
|
+
if (!this.enabled)
|
|
2243
|
+
return;
|
|
2198
2244
|
if (!queue || queue.length === 0)
|
|
2199
2245
|
return;
|
|
2200
2246
|
if (!this.topicId) {
|
|
@@ -2238,6 +2284,8 @@ class ClsLoggerCore {
|
|
|
2238
2284
|
* 参考《一、概述》:统一上报入口(内存队列 + 批量发送)
|
|
2239
2285
|
*/
|
|
2240
2286
|
report(log) {
|
|
2287
|
+
if (!this.enabled)
|
|
2288
|
+
return;
|
|
2241
2289
|
if (!log?.type)
|
|
2242
2290
|
return;
|
|
2243
2291
|
if (!this.topicId) {
|
|
@@ -2285,15 +2333,57 @@ class ClsLoggerCore {
|
|
|
2285
2333
|
return nowTs + this.batchIntervalMs;
|
|
2286
2334
|
}
|
|
2287
2335
|
info(message, data = {}) {
|
|
2288
|
-
|
|
2336
|
+
let msg = '';
|
|
2337
|
+
let extra = {};
|
|
2338
|
+
if (message instanceof Error) {
|
|
2339
|
+
msg = message.message;
|
|
2340
|
+
extra = {
|
|
2341
|
+
stack: message.stack,
|
|
2342
|
+
name: message.name,
|
|
2343
|
+
...data,
|
|
2344
|
+
};
|
|
2345
|
+
}
|
|
2346
|
+
else {
|
|
2347
|
+
msg = String(message);
|
|
2348
|
+
extra = data;
|
|
2349
|
+
}
|
|
2350
|
+
const payload = normalizeFlatFields({ message: msg, ...extra }, 'info');
|
|
2289
2351
|
this.report({ type: 'info', data: payload, timestamp: Date.now() });
|
|
2290
2352
|
}
|
|
2291
2353
|
warn(message, data = {}) {
|
|
2292
|
-
|
|
2354
|
+
let msg = '';
|
|
2355
|
+
let extra = {};
|
|
2356
|
+
if (message instanceof Error) {
|
|
2357
|
+
msg = message.message;
|
|
2358
|
+
extra = {
|
|
2359
|
+
stack: message.stack,
|
|
2360
|
+
name: message.name,
|
|
2361
|
+
...data,
|
|
2362
|
+
};
|
|
2363
|
+
}
|
|
2364
|
+
else {
|
|
2365
|
+
msg = String(message);
|
|
2366
|
+
extra = data;
|
|
2367
|
+
}
|
|
2368
|
+
const payload = normalizeFlatFields({ message: msg, ...extra }, 'warn');
|
|
2293
2369
|
this.report({ type: 'warn', data: payload, timestamp: Date.now() });
|
|
2294
2370
|
}
|
|
2295
2371
|
error(message, data = {}) {
|
|
2296
|
-
|
|
2372
|
+
let msg = '';
|
|
2373
|
+
let extra = {};
|
|
2374
|
+
if (message instanceof Error) {
|
|
2375
|
+
msg = message.message;
|
|
2376
|
+
extra = {
|
|
2377
|
+
stack: message.stack,
|
|
2378
|
+
name: message.name,
|
|
2379
|
+
...data,
|
|
2380
|
+
};
|
|
2381
|
+
}
|
|
2382
|
+
else {
|
|
2383
|
+
msg = String(message);
|
|
2384
|
+
extra = data;
|
|
2385
|
+
}
|
|
2386
|
+
const payload = normalizeFlatFields({ message: msg, ...extra }, 'error');
|
|
2297
2387
|
this.report({ type: 'error', data: payload, timestamp: Date.now() });
|
|
2298
2388
|
}
|
|
2299
2389
|
track(trackType, data = {}) {
|
|
@@ -2400,6 +2490,8 @@ class ClsLoggerCore {
|
|
|
2400
2490
|
writeStringStorage(this.failedCacheKey, JSON.stringify(next));
|
|
2401
2491
|
}
|
|
2402
2492
|
flushFailed() {
|
|
2493
|
+
if (!this.enabled)
|
|
2494
|
+
return;
|
|
2403
2495
|
const raw = readStringStorage(this.failedCacheKey);
|
|
2404
2496
|
if (!raw)
|
|
2405
2497
|
return;
|