@be-link/cls-logger 1.0.1-beta.8 → 1.0.3

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,
@@ -1109,140 +1126,68 @@ function installBrowserPerformanceMonitor(report, options) {
1109
1126
  }
1110
1127
  }
1111
1128
  }
1112
- function wrapMiniProgramRouteApi(report, reportType, apiName, options) {
1113
- const wxAny = globalThis.wx;
1114
- if (!wxAny || typeof wxAny[apiName] !== 'function')
1115
- return;
1116
- const flagKey = `__beLinkClsLoggerMpRouteWrapped__${apiName}`;
1117
- if (wxAny[flagKey])
1118
- return;
1119
- wxAny[flagKey] = true;
1120
- const raw = wxAny[apiName].bind(wxAny);
1121
- wxAny[apiName] = (opts) => {
1122
- const start = Date.now();
1123
- const url = opts?.url ? String(opts.url) : '';
1124
- const wrapCb = (cb, success) => {
1125
- return (...args) => {
1126
- try {
1127
- if (sampleHit(options.sampleRate)) {
1128
- report(reportType, {
1129
- metric: 'route',
1130
- api: apiName,
1131
- url,
1132
- duration: Date.now() - start,
1133
- success: success ? 1 : 0,
1134
- error: success ? '' : truncate$1(stringifyLogValue(args?.[0]), options.maxTextLength),
1135
- unit: 'ms',
1136
- });
1137
- }
1138
- }
1139
- catch {
1140
- // ignore
1141
- }
1142
- if (typeof cb === 'function')
1143
- return cb(...args);
1144
- return undefined;
1145
- };
1146
- };
1147
- const next = { ...(opts ?? {}) };
1148
- next.success = wrapCb(next.success, true);
1149
- next.fail = wrapCb(next.fail, false);
1150
- return raw(next);
1151
- };
1152
- }
1153
- function installMiniProgramPageRenderMonitor(report, reportType, options) {
1154
- const g = globalThis;
1155
- if (typeof g.Page !== 'function')
1156
- return;
1157
- if (g.__beLinkClsLoggerPageWrapped__)
1158
- return;
1159
- g.__beLinkClsLoggerPageWrapped__ = true;
1160
- const rawPage = g.Page;
1161
- g.Page = (pageOptions) => {
1162
- const next = { ...(pageOptions ?? {}) };
1163
- const rawOnLoad = next.onLoad;
1164
- const rawOnReady = next.onReady;
1165
- next.onLoad = function (...args) {
1166
- try {
1167
- this.__beLinkClsLoggerPageLoadTs__ = Date.now();
1168
- }
1169
- catch {
1170
- // ignore
1171
- }
1172
- if (typeof rawOnLoad === 'function')
1173
- return rawOnLoad.apply(this, args);
1174
- return undefined;
1175
- };
1176
- next.onReady = function (...args) {
1177
- try {
1178
- const start = this.__beLinkClsLoggerPageLoadTs__;
1179
- if (typeof start === 'number' && sampleHit(options.sampleRate)) {
1180
- report(reportType, {
1181
- metric: 'page-render',
1182
- route: this?.route ? String(this.route) : '',
1183
- duration: Date.now() - start,
1184
- unit: 'ms',
1185
- });
1186
- }
1187
- }
1188
- catch {
1189
- // ignore
1190
- }
1191
- if (typeof rawOnReady === 'function')
1192
- return rawOnReady.apply(this, args);
1193
- return undefined;
1194
- };
1195
- return rawPage(next);
1196
- };
1197
- }
1198
1129
  function installMiniProgramPerformanceMonitor(report, options) {
1199
1130
  const g = globalThis;
1131
+ const ctx = g.wx || g.Taro;
1132
+ if (!ctx || typeof ctx.getPerformance !== 'function')
1133
+ return;
1200
1134
  if (g.__beLinkClsLoggerMpPerfInstalled__)
1201
1135
  return;
1202
1136
  g.__beLinkClsLoggerMpPerfInstalled__ = true;
1203
- // 路由切换耗时(用 API 回调近似)
1204
- for (const apiName of ['navigateTo', 'redirectTo', 'switchTab', 'reLaunch']) {
1205
- try {
1206
- wrapMiniProgramRouteApi(report, options.reportType, apiName, options);
1207
- }
1208
- catch {
1209
- // ignore
1210
- }
1211
- }
1212
- // 页面渲染耗时(onLoad -> onReady)
1213
- try {
1214
- installMiniProgramPageRenderMonitor(report, options.reportType, options);
1215
- }
1216
- catch {
1217
- // ignore
1218
- }
1219
- // wx.getPerformance()(若可用,尝试读取已有 entries)
1220
1137
  try {
1221
- const wxAny = globalThis.wx;
1222
- if (wxAny && typeof wxAny.getPerformance === 'function') {
1223
- const perf = wxAny.getPerformance();
1224
- if (perf && isPlainObject(perf)) {
1225
- // 不同基础库实现差异较大:尽量容错
1226
- setTimeout(() => {
1227
- try {
1228
- if (!sampleHit(options.sampleRate))
1229
- return;
1230
- const entries = typeof perf.getEntries === 'function'
1231
- ? perf.getEntries()
1232
- : typeof perf.getEntriesByType === 'function'
1233
- ? perf.getEntriesByType('navigation')
1234
- : [];
1138
+ const perf = ctx.getPerformance();
1139
+ if (!perf || typeof perf.createObserver !== 'function')
1140
+ return;
1141
+ const observer = perf.createObserver((entryList) => {
1142
+ try {
1143
+ const entries = entryList.getEntries();
1144
+ for (const entry of entries) {
1145
+ if (!sampleHit(options.sampleRate))
1146
+ continue;
1147
+ // Page Render: firstRender
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;
1235
1154
  report(options.reportType, {
1236
- metric: 'mp-performance',
1237
- entries: truncate$1(stringifyLogValue(entries), options.maxTextLength),
1155
+ metric: 'page-render',
1156
+ duration,
1157
+ pagePath: entry.path || '',
1158
+ unit: 'ms',
1238
1159
  });
1239
1160
  }
1240
- catch {
1241
- // ignore
1161
+ // Route Switch: route
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;
1168
+ report(options.reportType, {
1169
+ metric: 'route',
1170
+ duration,
1171
+ pagePath: entry.path || '',
1172
+ unit: 'ms',
1173
+ });
1242
1174
  }
1243
- }, 0);
1175
+ // App Launch: appLaunch (Cold)
1176
+ else if (entry.entryType === 'navigation' && entry.name === 'appLaunch') {
1177
+ report(options.reportType, {
1178
+ metric: 'app-launch',
1179
+ duration: entry.duration,
1180
+ launchType: 'cold',
1181
+ unit: 'ms',
1182
+ });
1183
+ }
1184
+ }
1244
1185
  }
1245
- }
1186
+ catch {
1187
+ // ignore
1188
+ }
1189
+ });
1190
+ observer.observe({ entryTypes: ['navigation', 'render'] });
1246
1191
  }
1247
1192
  catch {
1248
1193
  // ignore
@@ -1566,7 +1511,7 @@ function installBehaviorMonitor(report, envType, options = {}) {
1566
1511
  const tag = (el.tagName || '').toLowerCase();
1567
1512
  const trackId = getAttr(el, clickTrackIdAttr);
1568
1513
  // 过滤无效点击:白名单 tag + 没有 trackId
1569
- if (clickWhiteList.includes(tag) && !trackId)
1514
+ if (clickWhiteList.includes(tag) || !trackId)
1570
1515
  return;
1571
1516
  void uvStatePromise.then(({ uvId, meta }) => {
1572
1517
  if (destroyed)
@@ -1607,8 +1552,12 @@ function installBehaviorMonitor(report, envType, options = {}) {
1607
1552
  g.Page = function patchedPage(conf) {
1608
1553
  const originalOnShow = conf?.onShow;
1609
1554
  conf.onShow = function (...args) {
1610
- if (pvEnabled)
1611
- reportPv(getPagePath());
1555
+ if (pvEnabled) {
1556
+ const pagePath = getPagePath();
1557
+ if (pagePath?.length > 0) {
1558
+ reportPv(pagePath);
1559
+ }
1560
+ }
1612
1561
  return typeof originalOnShow === 'function' ? originalOnShow.apply(this, args) : undefined;
1613
1562
  };
1614
1563
  // 点击:wrap 页面 methods(bindtap 等会调用到这里的 handler)
@@ -1992,9 +1941,14 @@ class ClsLoggerCore {
1992
1941
  * 子类可按需重写(默认检测 wx)
1993
1942
  */
1994
1943
  detectEnvType() {
1995
- const wxAny = globalThis.wx;
1996
- if (wxAny && typeof wxAny.getSystemInfoSync === 'function')
1944
+ const g = globalThis;
1945
+ // 微信、支付宝、字节跳动、UniApp 等小程序环境通常都有特定全局变量
1946
+ if ((g.wx && typeof g.wx.getSystemInfoSync === 'function') ||
1947
+ (g.my && typeof g.my.getSystemInfoSync === 'function') ||
1948
+ (g.tt && typeof g.tt.getSystemInfoSync === 'function') ||
1949
+ (g.uni && typeof g.uni.getSystemInfoSync === 'function')) {
1997
1950
  return 'miniprogram';
1951
+ }
1998
1952
  return 'browser';
1999
1953
  }
2000
1954
  init(options) {
@@ -2367,15 +2321,57 @@ class ClsLoggerCore {
2367
2321
  return nowTs + this.batchIntervalMs;
2368
2322
  }
2369
2323
  info(message, data = {}) {
2370
- const payload = normalizeFlatFields({ message, ...data }, 'info');
2324
+ let msg = '';
2325
+ let extra = {};
2326
+ if (message instanceof Error) {
2327
+ msg = message.message;
2328
+ extra = {
2329
+ stack: message.stack,
2330
+ name: message.name,
2331
+ ...data,
2332
+ };
2333
+ }
2334
+ else {
2335
+ msg = String(message);
2336
+ extra = data;
2337
+ }
2338
+ const payload = normalizeFlatFields({ message: msg, ...extra }, 'info');
2371
2339
  this.report({ type: 'info', data: payload, timestamp: Date.now() });
2372
2340
  }
2373
2341
  warn(message, data = {}) {
2374
- const payload = normalizeFlatFields({ message, ...data }, 'warn');
2342
+ let msg = '';
2343
+ let extra = {};
2344
+ if (message instanceof Error) {
2345
+ msg = message.message;
2346
+ extra = {
2347
+ stack: message.stack,
2348
+ name: message.name,
2349
+ ...data,
2350
+ };
2351
+ }
2352
+ else {
2353
+ msg = String(message);
2354
+ extra = data;
2355
+ }
2356
+ const payload = normalizeFlatFields({ message: msg, ...extra }, 'warn');
2375
2357
  this.report({ type: 'warn', data: payload, timestamp: Date.now() });
2376
2358
  }
2377
2359
  error(message, data = {}) {
2378
- const payload = normalizeFlatFields({ message, ...data }, 'error');
2360
+ let msg = '';
2361
+ let extra = {};
2362
+ if (message instanceof Error) {
2363
+ msg = message.message;
2364
+ extra = {
2365
+ stack: message.stack,
2366
+ name: message.name,
2367
+ ...data,
2368
+ };
2369
+ }
2370
+ else {
2371
+ msg = String(message);
2372
+ extra = data;
2373
+ }
2374
+ const payload = normalizeFlatFields({ message: msg, ...extra }, 'error');
2379
2375
  this.report({ type: 'error', data: payload, timestamp: Date.now() });
2380
2376
  }
2381
2377
  track(trackType, data = {}) {
@@ -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;AAiYzD,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"}