@be-link/cls-logger 1.0.1-beta.7 → 1.0.1-beta.9

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/index.umd.js CHANGED
@@ -567,7 +567,17 @@
567
567
  function normalizeErrorLike(err, maxTextLength) {
568
568
  if (err && typeof err === 'object') {
569
569
  const anyErr = err;
570
- const message = truncate$2(String(anyErr.message ?? anyErr.toString?.() ?? ''), maxTextLength);
570
+ let rawMsg = anyErr.message;
571
+ if (!rawMsg) {
572
+ const str = anyErr.toString?.();
573
+ if (!str || str === '[object Object]') {
574
+ rawMsg = stringifyLogValue(anyErr);
575
+ }
576
+ else {
577
+ rawMsg = str;
578
+ }
579
+ }
580
+ const message = truncate$2(String(rawMsg ?? ''), maxTextLength);
571
581
  const name = truncate$2(String(anyErr.name ?? ''), 200);
572
582
  const stack = truncate$2(String(anyErr.stack ?? ''), maxTextLength);
573
583
  return { message, name, stack };
@@ -711,9 +721,12 @@
711
721
  try {
712
722
  if (!sampleHit$1(options.sampleRate))
713
723
  return;
724
+ const e = normalizeErrorLike(msg, options.maxTextLength);
714
725
  const payload = {
715
726
  source: 'wx.onError',
716
- message: truncate$2(String(msg ?? ''), options.maxTextLength),
727
+ message: e.message,
728
+ errorName: e.name,
729
+ stack: e.stack,
717
730
  };
718
731
  if (!shouldReport(buildErrorKey(options.reportType, payload)))
719
732
  return;
@@ -765,9 +778,12 @@
765
778
  next.onError = function (...args) {
766
779
  try {
767
780
  if (sampleHit$1(options.sampleRate)) {
781
+ const e = normalizeErrorLike(args?.[0], options.maxTextLength);
768
782
  const payload = {
769
783
  source: 'App.onError',
770
- message: truncate$2(String(args?.[0] ?? ''), options.maxTextLength),
784
+ message: e.message,
785
+ errorName: e.name,
786
+ stack: e.stack,
771
787
  };
772
788
  if (shouldReport(buildErrorKey(options.reportType, payload)))
773
789
  report(options.reportType, payload);
@@ -1074,140 +1090,58 @@
1074
1090
  }
1075
1091
  }
1076
1092
  }
1077
- function wrapMiniProgramRouteApi(report, reportType, apiName, options) {
1078
- const wxAny = globalThis.wx;
1079
- if (!wxAny || typeof wxAny[apiName] !== 'function')
1080
- return;
1081
- const flagKey = `__beLinkClsLoggerMpRouteWrapped__${apiName}`;
1082
- if (wxAny[flagKey])
1083
- return;
1084
- wxAny[flagKey] = true;
1085
- const raw = wxAny[apiName].bind(wxAny);
1086
- wxAny[apiName] = (opts) => {
1087
- const start = Date.now();
1088
- const url = opts?.url ? String(opts.url) : '';
1089
- const wrapCb = (cb, success) => {
1090
- return (...args) => {
1091
- try {
1092
- if (sampleHit(options.sampleRate)) {
1093
- report(reportType, {
1094
- metric: 'route',
1095
- api: apiName,
1096
- url,
1097
- duration: Date.now() - start,
1098
- success: success ? 1 : 0,
1099
- error: success ? '' : truncate$1(stringifyLogValue(args?.[0]), options.maxTextLength),
1100
- unit: 'ms',
1101
- });
1102
- }
1103
- }
1104
- catch {
1105
- // ignore
1106
- }
1107
- if (typeof cb === 'function')
1108
- return cb(...args);
1109
- return undefined;
1110
- };
1111
- };
1112
- const next = { ...(opts ?? {}) };
1113
- next.success = wrapCb(next.success, true);
1114
- next.fail = wrapCb(next.fail, false);
1115
- return raw(next);
1116
- };
1117
- }
1118
- function installMiniProgramPageRenderMonitor(report, reportType, options) {
1119
- const g = globalThis;
1120
- if (typeof g.Page !== 'function')
1121
- return;
1122
- if (g.__beLinkClsLoggerPageWrapped__)
1123
- return;
1124
- g.__beLinkClsLoggerPageWrapped__ = true;
1125
- const rawPage = g.Page;
1126
- g.Page = (pageOptions) => {
1127
- const next = { ...(pageOptions ?? {}) };
1128
- const rawOnLoad = next.onLoad;
1129
- const rawOnReady = next.onReady;
1130
- next.onLoad = function (...args) {
1131
- try {
1132
- this.__beLinkClsLoggerPageLoadTs__ = Date.now();
1133
- }
1134
- catch {
1135
- // ignore
1136
- }
1137
- if (typeof rawOnLoad === 'function')
1138
- return rawOnLoad.apply(this, args);
1139
- return undefined;
1140
- };
1141
- next.onReady = function (...args) {
1142
- try {
1143
- const start = this.__beLinkClsLoggerPageLoadTs__;
1144
- if (typeof start === 'number' && sampleHit(options.sampleRate)) {
1145
- report(reportType, {
1146
- metric: 'page-render',
1147
- route: this?.route ? String(this.route) : '',
1148
- duration: Date.now() - start,
1149
- unit: 'ms',
1150
- });
1151
- }
1152
- }
1153
- catch {
1154
- // ignore
1155
- }
1156
- if (typeof rawOnReady === 'function')
1157
- return rawOnReady.apply(this, args);
1158
- return undefined;
1159
- };
1160
- return rawPage(next);
1161
- };
1162
- }
1163
1093
  function installMiniProgramPerformanceMonitor(report, options) {
1164
1094
  const g = globalThis;
1095
+ const ctx = g.wx || g.Taro;
1096
+ if (!ctx || typeof ctx.getPerformance !== 'function')
1097
+ return;
1165
1098
  if (g.__beLinkClsLoggerMpPerfInstalled__)
1166
1099
  return;
1167
1100
  g.__beLinkClsLoggerMpPerfInstalled__ = true;
1168
- // 路由切换耗时(用 API 回调近似)
1169
- for (const apiName of ['navigateTo', 'redirectTo', 'switchTab', 'reLaunch']) {
1170
- try {
1171
- wrapMiniProgramRouteApi(report, options.reportType, apiName, options);
1172
- }
1173
- catch {
1174
- // ignore
1175
- }
1176
- }
1177
- // 页面渲染耗时(onLoad -> onReady)
1178
- try {
1179
- installMiniProgramPageRenderMonitor(report, options.reportType, options);
1180
- }
1181
- catch {
1182
- // ignore
1183
- }
1184
- // wx.getPerformance()(若可用,尝试读取已有 entries)
1185
1101
  try {
1186
- const wxAny = globalThis.wx;
1187
- if (wxAny && typeof wxAny.getPerformance === 'function') {
1188
- const perf = wxAny.getPerformance();
1189
- if (perf && isPlainObject(perf)) {
1190
- // 不同基础库实现差异较大:尽量容错
1191
- setTimeout(() => {
1192
- try {
1193
- if (!sampleHit(options.sampleRate))
1194
- return;
1195
- const entries = typeof perf.getEntries === 'function'
1196
- ? perf.getEntries()
1197
- : typeof perf.getEntriesByType === 'function'
1198
- ? perf.getEntriesByType('navigation')
1199
- : [];
1102
+ const perf = ctx.getPerformance();
1103
+ if (!perf || typeof perf.createObserver !== 'function')
1104
+ return;
1105
+ const observer = perf.createObserver((entryList) => {
1106
+ try {
1107
+ const entries = entryList.getEntries();
1108
+ for (const entry of entries) {
1109
+ if (!sampleHit(options.sampleRate))
1110
+ continue;
1111
+ // Page Render: firstRender
1112
+ if (entry.entryType === 'render' && entry.name === 'firstRender') {
1200
1113
  report(options.reportType, {
1201
- metric: 'mp-performance',
1202
- entries: truncate$1(stringifyLogValue(entries), options.maxTextLength),
1114
+ metric: 'page-render',
1115
+ duration: entry.duration,
1116
+ pagePath: entry.path || '',
1117
+ unit: 'ms',
1203
1118
  });
1204
1119
  }
1205
- catch {
1206
- // ignore
1120
+ // Route Switch: route
1121
+ else if (entry.entryType === 'navigation' && entry.name === 'route') {
1122
+ report(options.reportType, {
1123
+ metric: 'route',
1124
+ duration: entry.duration,
1125
+ pagePath: entry.path || '',
1126
+ unit: 'ms',
1127
+ });
1207
1128
  }
1208
- }, 0);
1129
+ // App Launch: appLaunch (Cold)
1130
+ else if (entry.entryType === 'navigation' && entry.name === 'appLaunch') {
1131
+ report(options.reportType, {
1132
+ metric: 'app-launch',
1133
+ duration: entry.duration,
1134
+ launchType: 'cold',
1135
+ unit: 'ms',
1136
+ });
1137
+ }
1138
+ }
1209
1139
  }
1210
- }
1140
+ catch {
1141
+ // ignore
1142
+ }
1143
+ });
1144
+ observer.observe({ entryTypes: ['navigation', 'render'] });
1211
1145
  }
1212
1146
  catch {
1213
1147
  // ignore
@@ -2037,7 +1971,7 @@
2037
1971
  try {
2038
1972
  const userRaw = this.userGenerateBaseFields ? this.userGenerateBaseFields() : undefined;
2039
1973
  if (userRaw && isPlainObject(userRaw))
2040
- user = normalizeFlatFields({ ...userRaw, userId: this.userId, userName: this.userName }, 'generateBaseFields');
1974
+ user = normalizeFlatFields({ ...userRaw }, 'generateBaseFields');
2041
1975
  }
2042
1976
  catch {
2043
1977
  user = undefined;
package/dist/mini.esm.js CHANGED
@@ -563,7 +563,17 @@ function getPagePath$1() {
563
563
  function normalizeErrorLike(err, maxTextLength) {
564
564
  if (err && typeof err === 'object') {
565
565
  const anyErr = err;
566
- const message = truncate$2(String(anyErr.message ?? anyErr.toString?.() ?? ''), maxTextLength);
566
+ let rawMsg = anyErr.message;
567
+ if (!rawMsg) {
568
+ const str = anyErr.toString?.();
569
+ if (!str || str === '[object Object]') {
570
+ rawMsg = stringifyLogValue(anyErr);
571
+ }
572
+ else {
573
+ rawMsg = str;
574
+ }
575
+ }
576
+ const message = truncate$2(String(rawMsg ?? ''), maxTextLength);
567
577
  const name = truncate$2(String(anyErr.name ?? ''), 200);
568
578
  const stack = truncate$2(String(anyErr.stack ?? ''), maxTextLength);
569
579
  return { message, name, stack };
@@ -707,9 +717,12 @@ function installMiniProgramErrorMonitor(report, options) {
707
717
  try {
708
718
  if (!sampleHit$1(options.sampleRate))
709
719
  return;
720
+ const e = normalizeErrorLike(msg, options.maxTextLength);
710
721
  const payload = {
711
722
  source: 'wx.onError',
712
- message: truncate$2(String(msg ?? ''), options.maxTextLength),
723
+ message: e.message,
724
+ errorName: e.name,
725
+ stack: e.stack,
713
726
  };
714
727
  if (!shouldReport(buildErrorKey(options.reportType, payload)))
715
728
  return;
@@ -761,9 +774,12 @@ function installMiniProgramErrorMonitor(report, options) {
761
774
  next.onError = function (...args) {
762
775
  try {
763
776
  if (sampleHit$1(options.sampleRate)) {
777
+ const e = normalizeErrorLike(args?.[0], options.maxTextLength);
764
778
  const payload = {
765
779
  source: 'App.onError',
766
- message: truncate$2(String(args?.[0] ?? ''), options.maxTextLength),
780
+ message: e.message,
781
+ errorName: e.name,
782
+ stack: e.stack,
767
783
  };
768
784
  if (shouldReport(buildErrorKey(options.reportType, payload)))
769
785
  report(options.reportType, payload);
@@ -1070,140 +1086,58 @@ function installBrowserPerformanceMonitor(report, options) {
1070
1086
  }
1071
1087
  }
1072
1088
  }
1073
- function wrapMiniProgramRouteApi(report, reportType, apiName, options) {
1074
- const wxAny = globalThis.wx;
1075
- if (!wxAny || typeof wxAny[apiName] !== 'function')
1076
- return;
1077
- const flagKey = `__beLinkClsLoggerMpRouteWrapped__${apiName}`;
1078
- if (wxAny[flagKey])
1079
- return;
1080
- wxAny[flagKey] = true;
1081
- const raw = wxAny[apiName].bind(wxAny);
1082
- wxAny[apiName] = (opts) => {
1083
- const start = Date.now();
1084
- const url = opts?.url ? String(opts.url) : '';
1085
- const wrapCb = (cb, success) => {
1086
- return (...args) => {
1087
- try {
1088
- if (sampleHit(options.sampleRate)) {
1089
- report(reportType, {
1090
- metric: 'route',
1091
- api: apiName,
1092
- url,
1093
- duration: Date.now() - start,
1094
- success: success ? 1 : 0,
1095
- error: success ? '' : truncate$1(stringifyLogValue(args?.[0]), options.maxTextLength),
1096
- unit: 'ms',
1097
- });
1098
- }
1099
- }
1100
- catch {
1101
- // ignore
1102
- }
1103
- if (typeof cb === 'function')
1104
- return cb(...args);
1105
- return undefined;
1106
- };
1107
- };
1108
- const next = { ...(opts ?? {}) };
1109
- next.success = wrapCb(next.success, true);
1110
- next.fail = wrapCb(next.fail, false);
1111
- return raw(next);
1112
- };
1113
- }
1114
- function installMiniProgramPageRenderMonitor(report, reportType, options) {
1115
- const g = globalThis;
1116
- if (typeof g.Page !== 'function')
1117
- return;
1118
- if (g.__beLinkClsLoggerPageWrapped__)
1119
- return;
1120
- g.__beLinkClsLoggerPageWrapped__ = true;
1121
- const rawPage = g.Page;
1122
- g.Page = (pageOptions) => {
1123
- const next = { ...(pageOptions ?? {}) };
1124
- const rawOnLoad = next.onLoad;
1125
- const rawOnReady = next.onReady;
1126
- next.onLoad = function (...args) {
1127
- try {
1128
- this.__beLinkClsLoggerPageLoadTs__ = Date.now();
1129
- }
1130
- catch {
1131
- // ignore
1132
- }
1133
- if (typeof rawOnLoad === 'function')
1134
- return rawOnLoad.apply(this, args);
1135
- return undefined;
1136
- };
1137
- next.onReady = function (...args) {
1138
- try {
1139
- const start = this.__beLinkClsLoggerPageLoadTs__;
1140
- if (typeof start === 'number' && sampleHit(options.sampleRate)) {
1141
- report(reportType, {
1142
- metric: 'page-render',
1143
- route: this?.route ? String(this.route) : '',
1144
- duration: Date.now() - start,
1145
- unit: 'ms',
1146
- });
1147
- }
1148
- }
1149
- catch {
1150
- // ignore
1151
- }
1152
- if (typeof rawOnReady === 'function')
1153
- return rawOnReady.apply(this, args);
1154
- return undefined;
1155
- };
1156
- return rawPage(next);
1157
- };
1158
- }
1159
1089
  function installMiniProgramPerformanceMonitor(report, options) {
1160
1090
  const g = globalThis;
1091
+ const ctx = g.wx || g.Taro;
1092
+ if (!ctx || typeof ctx.getPerformance !== 'function')
1093
+ return;
1161
1094
  if (g.__beLinkClsLoggerMpPerfInstalled__)
1162
1095
  return;
1163
1096
  g.__beLinkClsLoggerMpPerfInstalled__ = true;
1164
- // 路由切换耗时(用 API 回调近似)
1165
- for (const apiName of ['navigateTo', 'redirectTo', 'switchTab', 'reLaunch']) {
1166
- try {
1167
- wrapMiniProgramRouteApi(report, options.reportType, apiName, options);
1168
- }
1169
- catch {
1170
- // ignore
1171
- }
1172
- }
1173
- // 页面渲染耗时(onLoad -> onReady)
1174
- try {
1175
- installMiniProgramPageRenderMonitor(report, options.reportType, options);
1176
- }
1177
- catch {
1178
- // ignore
1179
- }
1180
- // wx.getPerformance()(若可用,尝试读取已有 entries)
1181
1097
  try {
1182
- const wxAny = globalThis.wx;
1183
- if (wxAny && typeof wxAny.getPerformance === 'function') {
1184
- const perf = wxAny.getPerformance();
1185
- if (perf && isPlainObject(perf)) {
1186
- // 不同基础库实现差异较大:尽量容错
1187
- setTimeout(() => {
1188
- try {
1189
- if (!sampleHit(options.sampleRate))
1190
- return;
1191
- const entries = typeof perf.getEntries === 'function'
1192
- ? perf.getEntries()
1193
- : typeof perf.getEntriesByType === 'function'
1194
- ? perf.getEntriesByType('navigation')
1195
- : [];
1098
+ const perf = ctx.getPerformance();
1099
+ if (!perf || typeof perf.createObserver !== 'function')
1100
+ return;
1101
+ const observer = perf.createObserver((entryList) => {
1102
+ try {
1103
+ const entries = entryList.getEntries();
1104
+ for (const entry of entries) {
1105
+ if (!sampleHit(options.sampleRate))
1106
+ continue;
1107
+ // Page Render: firstRender
1108
+ if (entry.entryType === 'render' && entry.name === 'firstRender') {
1196
1109
  report(options.reportType, {
1197
- metric: 'mp-performance',
1198
- entries: truncate$1(stringifyLogValue(entries), options.maxTextLength),
1110
+ metric: 'page-render',
1111
+ duration: entry.duration,
1112
+ pagePath: entry.path || '',
1113
+ unit: 'ms',
1199
1114
  });
1200
1115
  }
1201
- catch {
1202
- // ignore
1116
+ // Route Switch: route
1117
+ else if (entry.entryType === 'navigation' && entry.name === 'route') {
1118
+ report(options.reportType, {
1119
+ metric: 'route',
1120
+ duration: entry.duration,
1121
+ pagePath: entry.path || '',
1122
+ unit: 'ms',
1123
+ });
1203
1124
  }
1204
- }, 0);
1125
+ // App Launch: appLaunch (Cold)
1126
+ else if (entry.entryType === 'navigation' && entry.name === 'appLaunch') {
1127
+ report(options.reportType, {
1128
+ metric: 'app-launch',
1129
+ duration: entry.duration,
1130
+ launchType: 'cold',
1131
+ unit: 'ms',
1132
+ });
1133
+ }
1134
+ }
1205
1135
  }
1206
- }
1136
+ catch {
1137
+ // ignore
1138
+ }
1139
+ });
1140
+ observer.observe({ entryTypes: ['navigation', 'render'] });
1207
1141
  }
1208
1142
  catch {
1209
1143
  // ignore
@@ -2033,7 +1967,7 @@ class ClsLoggerCore {
2033
1967
  try {
2034
1968
  const userRaw = this.userGenerateBaseFields ? this.userGenerateBaseFields() : undefined;
2035
1969
  if (userRaw && isPlainObject(userRaw))
2036
- user = normalizeFlatFields({ ...userRaw, userId: this.userId, userName: this.userName }, 'generateBaseFields');
1970
+ user = normalizeFlatFields({ ...userRaw }, 'generateBaseFields');
2037
1971
  }
2038
1972
  catch {
2039
1973
  user = undefined;