@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/dist/mini.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: entry.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: entry.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) && !trackId)
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
- reportPv(getPagePath());
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 wxAny = globalThis.wx;
1914
- if (wxAny && typeof wxAny.getSystemInfoSync === 'function')
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
- this.flushFailed();
1971
- // 初始化后立即启动请求监听
1972
- this.startRequestMonitor(options.requestMonitor);
1973
- // 初始化后立即启动错误监控/性能监控
1974
- this.startErrorMonitor(options.errorMonitor);
1975
- this.startPerformanceMonitor(options.performanceMonitor);
1976
- // 初始化后立即启动行为埋点(PV/UV/点击)
1977
- this.startBehaviorMonitor(options.behaviorMonitor);
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
- const payload = normalizeFlatFields({ message, ...data }, 'info');
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
- const payload = normalizeFlatFields({ message, ...data }, 'warn');
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
- const payload = normalizeFlatFields({ message, ...data }, 'error');
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;
@@ -1 +1 @@
1
- {"version":3,"file":"performanceMonitor.d.ts","sourceRoot":"","sources":["../src/performanceMonitor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,yBAAyB,EAAE,MAAM,SAAS,CAAC;AAGrE,KAAK,QAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;AAuSzD,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,QAAQ,EAChB,IAAI,GAAE,OAAO,GAAG,yBAAyB,GAAG,SAAc,GACzD,IAAI,CAsBN"}
1
+ {"version":3,"file":"performanceMonitor.d.ts","sourceRoot":"","sources":["../src/performanceMonitor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,yBAAyB,EAAE,MAAM,SAAS,CAAC;AAGrE,KAAK,QAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;AAqTzD,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,QAAQ,EAChB,IAAI,GAAE,OAAO,GAAG,yBAAyB,GAAG,SAAc,GACzD,IAAI,CAsBN"}
package/dist/types.d.ts CHANGED
@@ -36,7 +36,12 @@ export interface BatchOptions {
36
36
  */
37
37
  startupDelayMs?: number;
38
38
  }
39
- export interface ClsLoggerInitOptions {
39
+ export interface ClsLoggerBaseOptions {
40
+ /**
41
+ * 全局开关:是否开启 SDK,默认 true
42
+ * - false: 彻底关闭 SDK(不采集、不写入队列、不发送)
43
+ */
44
+ enabled?: boolean;
40
45
  /**
41
46
  * 参考《一、概述》:用于多项目日志隔离的项目标识
42
47
  * - 推荐使用 projectId
@@ -45,12 +50,8 @@ export interface ClsLoggerInitOptions {
45
50
  projectId?: string;
46
51
  /** 兼容旧字段:项目名 */
47
52
  projectName?: string;
48
- /** 应用 ID */
49
- appId?: string;
50
53
  /** 应用版本 */
51
54
  appVersion?: string;
52
- /** 运行环境类型(不传则自动识别) */
53
- envType?: EnvType;
54
55
  /**
55
56
  * 腾讯云 CLS 配置(推荐)
56
57
  * - endpoint/topicID/retry_times/source
@@ -118,6 +119,19 @@ export interface ClsLoggerInitOptions {
118
119
  */
119
120
  behaviorMonitor?: boolean | BehaviorMonitorOptions;
120
121
  }
122
+ export interface ClsLoggerWebOptions extends ClsLoggerBaseOptions {
123
+ /** 运行环境类型(不传则自动识别,Web 环境下默认为 browser) */
124
+ envType?: 'browser';
125
+ /** 应用 ID(Web 环境可选) */
126
+ appId?: string;
127
+ }
128
+ export interface ClsLoggerMiniProgramOptions extends ClsLoggerBaseOptions {
129
+ /** 运行环境类型:小程序(此时 appId 必填) */
130
+ envType: 'miniprogram';
131
+ /** 应用 ID(小程序必填) */
132
+ appId: string;
133
+ }
134
+ export type ClsLoggerInitOptions = ClsLoggerWebOptions | ClsLoggerMiniProgramOptions;
121
135
  export interface PutOptions {
122
136
  /** 是否合并 init.generateBaseFields() 的结果,默认 true */
123
137
  mergeBaseFields?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAC7D,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG,SAAS,EAAE,GAAG;IAAE,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEjF;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG,aAAa,GAAG,SAAS,GAAG,MAAM,CAAC;AAEhE,sCAAsC;AACtC,MAAM,MA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AAM,CAAC;IAEpB,0BAA0B;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,0BAA0B;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,4BAA4B;IAC5B,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEzC,kCAAkC;IAClC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,gDAAgD;IAChD,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,2DAA2D;IAC3D,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAEhC;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAE1B;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,wCAAwC;IACxC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,gCAAgC;IAChC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAE5B,8DAA8D;IAC9D,WAAW,CAAC,EAAE,MAAM,MAAM,CAAC;IAE3B;;OAEG;IACH,YAAY,CAAC,EAAE;QACb;;;;WAIG;QACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;QAC1B,yBAAyB;QACzB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB,CAAC;CACH"}
package/dist/web.esm.js CHANGED
@@ -560,6 +560,19 @@ function getPagePath$1() {
560
560
  return '';
561
561
  }
562
562
  }
563
+ function getMpPagePath() {
564
+ try {
565
+ const pages = globalThis.getCurrentPages?.();
566
+ if (Array.isArray(pages) && pages.length > 0) {
567
+ const page = pages[pages.length - 1];
568
+ return page.route || page.__route__ || '';
569
+ }
570
+ return '';
571
+ }
572
+ catch {
573
+ return '';
574
+ }
575
+ }
563
576
  function normalizeErrorLike(err, maxTextLength) {
564
577
  if (err && typeof err === 'object') {
565
578
  const anyErr = err;
@@ -719,6 +732,7 @@ function installMiniProgramErrorMonitor(report, options) {
719
732
  return;
720
733
  const e = normalizeErrorLike(msg, options.maxTextLength);
721
734
  const payload = {
735
+ pagePath: getMpPagePath(),
722
736
  source: 'wx.onError',
723
737
  message: e.message,
724
738
  errorName: e.name,
@@ -745,6 +759,7 @@ function installMiniProgramErrorMonitor(report, options) {
745
759
  return;
746
760
  const e = normalizeErrorLike(res?.reason, options.maxTextLength);
747
761
  const payload = {
762
+ pagePath: getMpPagePath(),
748
763
  source: 'wx.onUnhandledRejection',
749
764
  message: e.message,
750
765
  errorName: e.name,
@@ -776,6 +791,7 @@ function installMiniProgramErrorMonitor(report, options) {
776
791
  if (sampleHit$1(options.sampleRate)) {
777
792
  const e = normalizeErrorLike(args?.[0], options.maxTextLength);
778
793
  const payload = {
794
+ pagePath: getMpPagePath(),
779
795
  source: 'App.onError',
780
796
  message: e.message,
781
797
  errorName: e.name,
@@ -799,6 +815,7 @@ function installMiniProgramErrorMonitor(report, options) {
799
815
  const reason = args?.[0]?.reason ?? args?.[0];
800
816
  const e = normalizeErrorLike(reason, options.maxTextLength);
801
817
  const payload = {
818
+ pagePath: getMpPagePath(),
802
819
  source: 'App.onUnhandledRejection',
803
820
  message: e.message,
804
821
  errorName: e.name,
@@ -1106,18 +1123,28 @@ function installMiniProgramPerformanceMonitor(report, options) {
1106
1123
  continue;
1107
1124
  // Page Render: firstRender
1108
1125
  if (entry.entryType === 'render' && entry.name === 'firstRender') {
1126
+ const duration = typeof entry.duration === 'number'
1127
+ ? entry.duration
1128
+ : typeof entry.startTime === 'number' && typeof entry.endTime === 'number'
1129
+ ? entry.endTime - entry.startTime
1130
+ : 0;
1109
1131
  report(options.reportType, {
1110
1132
  metric: 'page-render',
1111
- duration: entry.duration,
1133
+ duration,
1112
1134
  pagePath: entry.path || '',
1113
1135
  unit: 'ms',
1114
1136
  });
1115
1137
  }
1116
1138
  // Route Switch: route
1117
1139
  else if (entry.entryType === 'navigation' && entry.name === 'route') {
1140
+ const duration = typeof entry.duration === 'number'
1141
+ ? entry.duration
1142
+ : typeof entry.startTime === 'number' && typeof entry.endTime === 'number'
1143
+ ? entry.endTime - entry.startTime
1144
+ : 0;
1118
1145
  report(options.reportType, {
1119
1146
  metric: 'route',
1120
- duration: entry.duration,
1147
+ duration,
1121
1148
  pagePath: entry.path || '',
1122
1149
  unit: 'ms',
1123
1150
  });
@@ -1461,7 +1488,7 @@ function installBehaviorMonitor(report, envType, options = {}) {
1461
1488
  const tag = (el.tagName || '').toLowerCase();
1462
1489
  const trackId = getAttr(el, clickTrackIdAttr);
1463
1490
  // 过滤无效点击:白名单 tag + 没有 trackId
1464
- if (clickWhiteList.includes(tag) && !trackId)
1491
+ if (clickWhiteList.includes(tag) || !trackId)
1465
1492
  return;
1466
1493
  void uvStatePromise.then(({ uvId, meta }) => {
1467
1494
  if (destroyed)
@@ -1502,8 +1529,12 @@ function installBehaviorMonitor(report, envType, options = {}) {
1502
1529
  g.Page = function patchedPage(conf) {
1503
1530
  const originalOnShow = conf?.onShow;
1504
1531
  conf.onShow = function (...args) {
1505
- if (pvEnabled)
1506
- reportPv(getPagePath());
1532
+ if (pvEnabled) {
1533
+ const pagePath = getPagePath();
1534
+ if (pagePath?.length > 0) {
1535
+ reportPv(pagePath);
1536
+ }
1537
+ }
1507
1538
  return typeof originalOnShow === 'function' ? originalOnShow.apply(this, args) : undefined;
1508
1539
  };
1509
1540
  // 点击:wrap 页面 methods(bindtap 等会调用到这里的 handler)
@@ -1857,6 +1888,7 @@ class ClsLoggerCore {
1857
1888
  this.endpoint = 'ap-shanghai.cls.tencentcs.com';
1858
1889
  this.retryTimes = 10;
1859
1890
  this.source = '127.0.0.1';
1891
+ this.enabled = true;
1860
1892
  this.projectId = '';
1861
1893
  this.projectName = '';
1862
1894
  this.appId = '';
@@ -1887,9 +1919,14 @@ class ClsLoggerCore {
1887
1919
  * 子类可按需重写(默认检测 wx)
1888
1920
  */
1889
1921
  detectEnvType() {
1890
- const wxAny = globalThis.wx;
1891
- if (wxAny && typeof wxAny.getSystemInfoSync === 'function')
1922
+ const g = globalThis;
1923
+ // 微信、支付宝、字节跳动、UniApp 等小程序环境通常都有特定全局变量
1924
+ if ((g.wx && typeof g.wx.getSystemInfoSync === 'function') ||
1925
+ (g.my && typeof g.my.getSystemInfoSync === 'function') ||
1926
+ (g.tt && typeof g.tt.getSystemInfoSync === 'function') ||
1927
+ (g.uni && typeof g.uni.getSystemInfoSync === 'function')) {
1892
1928
  return 'miniprogram';
1929
+ }
1893
1930
  return 'browser';
1894
1931
  }
1895
1932
  init(options) {
@@ -1927,6 +1964,7 @@ class ClsLoggerCore {
1927
1964
  this.appId = options.appId ?? this.appId;
1928
1965
  this.appVersion = options.appVersion ?? this.appVersion;
1929
1966
  this.envType = nextEnvType;
1967
+ this.enabled = options.enabled ?? true;
1930
1968
  // 可选:外部注入 SDK(优先级:sdkLoader > sdk)
1931
1969
  this.sdkLoaderOverride = options.sdkLoader ?? this.sdkLoaderOverride;
1932
1970
  this.sdkOverride = options.sdk ?? this.sdkOverride;
@@ -1943,15 +1981,17 @@ class ClsLoggerCore {
1943
1981
  void this.getInstance().catch(() => {
1944
1982
  // ignore
1945
1983
  });
1946
- // 启动时尝试发送失败缓存
1947
- this.flushFailed();
1948
- // 初始化后立即启动请求监听
1949
- this.startRequestMonitor(options.requestMonitor);
1950
- // 初始化后立即启动错误监控/性能监控
1951
- this.startErrorMonitor(options.errorMonitor);
1952
- this.startPerformanceMonitor(options.performanceMonitor);
1953
- // 初始化后立即启动行为埋点(PV/UV/点击)
1954
- this.startBehaviorMonitor(options.behaviorMonitor);
1984
+ if (this.enabled) {
1985
+ // 启动时尝试发送失败缓存
1986
+ this.flushFailed();
1987
+ // 初始化后立即启动请求监听
1988
+ this.startRequestMonitor(options.requestMonitor);
1989
+ // 初始化后立即启动错误监控/性能监控
1990
+ this.startErrorMonitor(options.errorMonitor);
1991
+ this.startPerformanceMonitor(options.performanceMonitor);
1992
+ // 初始化后立即启动行为埋点(PV/UV/点击)
1993
+ this.startBehaviorMonitor(options.behaviorMonitor);
1994
+ }
1955
1995
  }
1956
1996
  getBaseFields() {
1957
1997
  let auto = undefined;
@@ -2076,6 +2116,8 @@ class ClsLoggerCore {
2076
2116
  * - 最终会把 fields 展开成 CLS 的 content(key/value 都会转成 string)
2077
2117
  */
2078
2118
  put(fields, options = {}) {
2119
+ if (!this.enabled)
2120
+ return;
2079
2121
  if (!fields)
2080
2122
  return;
2081
2123
  if (!this.topicId) {
@@ -2134,6 +2176,8 @@ class ClsLoggerCore {
2134
2176
  * - 埋点入参必须是一维(扁平)Object,非原始值会被 stringify
2135
2177
  */
2136
2178
  enqueue(fields, options = {}) {
2179
+ if (!this.enabled)
2180
+ return;
2137
2181
  if (!fields)
2138
2182
  return;
2139
2183
  const time = Date.now();
@@ -2172,6 +2216,8 @@ class ClsLoggerCore {
2172
2216
  * 批量上报(每条 item.data 展开为 log content)
2173
2217
  */
2174
2218
  putBatch(queue) {
2219
+ if (!this.enabled)
2220
+ return;
2175
2221
  if (!queue || queue.length === 0)
2176
2222
  return;
2177
2223
  if (!this.topicId) {
@@ -2215,6 +2261,8 @@ class ClsLoggerCore {
2215
2261
  * 参考《一、概述》:统一上报入口(内存队列 + 批量发送)
2216
2262
  */
2217
2263
  report(log) {
2264
+ if (!this.enabled)
2265
+ return;
2218
2266
  if (!log?.type)
2219
2267
  return;
2220
2268
  if (!this.topicId) {
@@ -2262,15 +2310,57 @@ class ClsLoggerCore {
2262
2310
  return nowTs + this.batchIntervalMs;
2263
2311
  }
2264
2312
  info(message, data = {}) {
2265
- const payload = normalizeFlatFields({ message, ...data }, 'info');
2313
+ let msg = '';
2314
+ let extra = {};
2315
+ if (message instanceof Error) {
2316
+ msg = message.message;
2317
+ extra = {
2318
+ stack: message.stack,
2319
+ name: message.name,
2320
+ ...data,
2321
+ };
2322
+ }
2323
+ else {
2324
+ msg = String(message);
2325
+ extra = data;
2326
+ }
2327
+ const payload = normalizeFlatFields({ message: msg, ...extra }, 'info');
2266
2328
  this.report({ type: 'info', data: payload, timestamp: Date.now() });
2267
2329
  }
2268
2330
  warn(message, data = {}) {
2269
- const payload = normalizeFlatFields({ message, ...data }, 'warn');
2331
+ let msg = '';
2332
+ let extra = {};
2333
+ if (message instanceof Error) {
2334
+ msg = message.message;
2335
+ extra = {
2336
+ stack: message.stack,
2337
+ name: message.name,
2338
+ ...data,
2339
+ };
2340
+ }
2341
+ else {
2342
+ msg = String(message);
2343
+ extra = data;
2344
+ }
2345
+ const payload = normalizeFlatFields({ message: msg, ...extra }, 'warn');
2270
2346
  this.report({ type: 'warn', data: payload, timestamp: Date.now() });
2271
2347
  }
2272
2348
  error(message, data = {}) {
2273
- const payload = normalizeFlatFields({ message, ...data }, 'error');
2349
+ let msg = '';
2350
+ let extra = {};
2351
+ if (message instanceof Error) {
2352
+ msg = message.message;
2353
+ extra = {
2354
+ stack: message.stack,
2355
+ name: message.name,
2356
+ ...data,
2357
+ };
2358
+ }
2359
+ else {
2360
+ msg = String(message);
2361
+ extra = data;
2362
+ }
2363
+ const payload = normalizeFlatFields({ message: msg, ...extra }, 'error');
2274
2364
  this.report({ type: 'error', data: payload, timestamp: Date.now() });
2275
2365
  }
2276
2366
  track(trackType, data = {}) {
@@ -2377,6 +2467,8 @@ class ClsLoggerCore {
2377
2467
  writeStringStorage(this.failedCacheKey, JSON.stringify(next));
2378
2468
  }
2379
2469
  flushFailed() {
2470
+ if (!this.enabled)
2471
+ return;
2380
2472
  const raw = readStringStorage(this.failedCacheKey);
2381
2473
  if (!raw)
2382
2474
  return;