@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/mini.js CHANGED
@@ -586,7 +586,17 @@ function getPagePath$1() {
586
586
  function normalizeErrorLike(err, maxTextLength) {
587
587
  if (err && typeof err === 'object') {
588
588
  const anyErr = err;
589
- const message = truncate$2(String(anyErr.message ?? anyErr.toString?.() ?? ''), maxTextLength);
589
+ let rawMsg = anyErr.message;
590
+ if (!rawMsg) {
591
+ const str = anyErr.toString?.();
592
+ if (!str || str === '[object Object]') {
593
+ rawMsg = stringifyLogValue(anyErr);
594
+ }
595
+ else {
596
+ rawMsg = str;
597
+ }
598
+ }
599
+ const message = truncate$2(String(rawMsg ?? ''), maxTextLength);
590
600
  const name = truncate$2(String(anyErr.name ?? ''), 200);
591
601
  const stack = truncate$2(String(anyErr.stack ?? ''), maxTextLength);
592
602
  return { message, name, stack };
@@ -730,9 +740,12 @@ function installMiniProgramErrorMonitor(report, options) {
730
740
  try {
731
741
  if (!sampleHit$1(options.sampleRate))
732
742
  return;
743
+ const e = normalizeErrorLike(msg, options.maxTextLength);
733
744
  const payload = {
734
745
  source: 'wx.onError',
735
- message: truncate$2(String(msg ?? ''), options.maxTextLength),
746
+ message: e.message,
747
+ errorName: e.name,
748
+ stack: e.stack,
736
749
  };
737
750
  if (!shouldReport(buildErrorKey(options.reportType, payload)))
738
751
  return;
@@ -784,9 +797,12 @@ function installMiniProgramErrorMonitor(report, options) {
784
797
  next.onError = function (...args) {
785
798
  try {
786
799
  if (sampleHit$1(options.sampleRate)) {
800
+ const e = normalizeErrorLike(args?.[0], options.maxTextLength);
787
801
  const payload = {
788
802
  source: 'App.onError',
789
- message: truncate$2(String(args?.[0] ?? ''), options.maxTextLength),
803
+ message: e.message,
804
+ errorName: e.name,
805
+ stack: e.stack,
790
806
  };
791
807
  if (shouldReport(buildErrorKey(options.reportType, payload)))
792
808
  report(options.reportType, payload);
@@ -1093,140 +1109,58 @@ function installBrowserPerformanceMonitor(report, options) {
1093
1109
  }
1094
1110
  }
1095
1111
  }
1096
- function wrapMiniProgramRouteApi(report, reportType, apiName, options) {
1097
- const wxAny = globalThis.wx;
1098
- if (!wxAny || typeof wxAny[apiName] !== 'function')
1099
- return;
1100
- const flagKey = `__beLinkClsLoggerMpRouteWrapped__${apiName}`;
1101
- if (wxAny[flagKey])
1102
- return;
1103
- wxAny[flagKey] = true;
1104
- const raw = wxAny[apiName].bind(wxAny);
1105
- wxAny[apiName] = (opts) => {
1106
- const start = Date.now();
1107
- const url = opts?.url ? String(opts.url) : '';
1108
- const wrapCb = (cb, success) => {
1109
- return (...args) => {
1110
- try {
1111
- if (sampleHit(options.sampleRate)) {
1112
- report(reportType, {
1113
- metric: 'route',
1114
- api: apiName,
1115
- url,
1116
- duration: Date.now() - start,
1117
- success: success ? 1 : 0,
1118
- error: success ? '' : truncate$1(stringifyLogValue(args?.[0]), options.maxTextLength),
1119
- unit: 'ms',
1120
- });
1121
- }
1122
- }
1123
- catch {
1124
- // ignore
1125
- }
1126
- if (typeof cb === 'function')
1127
- return cb(...args);
1128
- return undefined;
1129
- };
1130
- };
1131
- const next = { ...(opts ?? {}) };
1132
- next.success = wrapCb(next.success, true);
1133
- next.fail = wrapCb(next.fail, false);
1134
- return raw(next);
1135
- };
1136
- }
1137
- function installMiniProgramPageRenderMonitor(report, reportType, options) {
1138
- const g = globalThis;
1139
- if (typeof g.Page !== 'function')
1140
- return;
1141
- if (g.__beLinkClsLoggerPageWrapped__)
1142
- return;
1143
- g.__beLinkClsLoggerPageWrapped__ = true;
1144
- const rawPage = g.Page;
1145
- g.Page = (pageOptions) => {
1146
- const next = { ...(pageOptions ?? {}) };
1147
- const rawOnLoad = next.onLoad;
1148
- const rawOnReady = next.onReady;
1149
- next.onLoad = function (...args) {
1150
- try {
1151
- this.__beLinkClsLoggerPageLoadTs__ = Date.now();
1152
- }
1153
- catch {
1154
- // ignore
1155
- }
1156
- if (typeof rawOnLoad === 'function')
1157
- return rawOnLoad.apply(this, args);
1158
- return undefined;
1159
- };
1160
- next.onReady = function (...args) {
1161
- try {
1162
- const start = this.__beLinkClsLoggerPageLoadTs__;
1163
- if (typeof start === 'number' && sampleHit(options.sampleRate)) {
1164
- report(reportType, {
1165
- metric: 'page-render',
1166
- route: this?.route ? String(this.route) : '',
1167
- duration: Date.now() - start,
1168
- unit: 'ms',
1169
- });
1170
- }
1171
- }
1172
- catch {
1173
- // ignore
1174
- }
1175
- if (typeof rawOnReady === 'function')
1176
- return rawOnReady.apply(this, args);
1177
- return undefined;
1178
- };
1179
- return rawPage(next);
1180
- };
1181
- }
1182
1112
  function installMiniProgramPerformanceMonitor(report, options) {
1183
1113
  const g = globalThis;
1114
+ const ctx = g.wx || g.Taro;
1115
+ if (!ctx || typeof ctx.getPerformance !== 'function')
1116
+ return;
1184
1117
  if (g.__beLinkClsLoggerMpPerfInstalled__)
1185
1118
  return;
1186
1119
  g.__beLinkClsLoggerMpPerfInstalled__ = true;
1187
- // 路由切换耗时(用 API 回调近似)
1188
- for (const apiName of ['navigateTo', 'redirectTo', 'switchTab', 'reLaunch']) {
1189
- try {
1190
- wrapMiniProgramRouteApi(report, options.reportType, apiName, options);
1191
- }
1192
- catch {
1193
- // ignore
1194
- }
1195
- }
1196
- // 页面渲染耗时(onLoad -> onReady)
1197
- try {
1198
- installMiniProgramPageRenderMonitor(report, options.reportType, options);
1199
- }
1200
- catch {
1201
- // ignore
1202
- }
1203
- // wx.getPerformance()(若可用,尝试读取已有 entries)
1204
1120
  try {
1205
- const wxAny = globalThis.wx;
1206
- if (wxAny && typeof wxAny.getPerformance === 'function') {
1207
- const perf = wxAny.getPerformance();
1208
- if (perf && isPlainObject(perf)) {
1209
- // 不同基础库实现差异较大:尽量容错
1210
- setTimeout(() => {
1211
- try {
1212
- if (!sampleHit(options.sampleRate))
1213
- return;
1214
- const entries = typeof perf.getEntries === 'function'
1215
- ? perf.getEntries()
1216
- : typeof perf.getEntriesByType === 'function'
1217
- ? perf.getEntriesByType('navigation')
1218
- : [];
1121
+ const perf = ctx.getPerformance();
1122
+ if (!perf || typeof perf.createObserver !== 'function')
1123
+ return;
1124
+ const observer = perf.createObserver((entryList) => {
1125
+ try {
1126
+ const entries = entryList.getEntries();
1127
+ for (const entry of entries) {
1128
+ if (!sampleHit(options.sampleRate))
1129
+ continue;
1130
+ // Page Render: firstRender
1131
+ if (entry.entryType === 'render' && entry.name === 'firstRender') {
1219
1132
  report(options.reportType, {
1220
- metric: 'mp-performance',
1221
- entries: truncate$1(stringifyLogValue(entries), options.maxTextLength),
1133
+ metric: 'page-render',
1134
+ duration: entry.duration,
1135
+ pagePath: entry.path || '',
1136
+ unit: 'ms',
1222
1137
  });
1223
1138
  }
1224
- catch {
1225
- // ignore
1139
+ // Route Switch: route
1140
+ else if (entry.entryType === 'navigation' && entry.name === 'route') {
1141
+ report(options.reportType, {
1142
+ metric: 'route',
1143
+ duration: entry.duration,
1144
+ pagePath: entry.path || '',
1145
+ unit: 'ms',
1146
+ });
1226
1147
  }
1227
- }, 0);
1148
+ // App Launch: appLaunch (Cold)
1149
+ else if (entry.entryType === 'navigation' && entry.name === 'appLaunch') {
1150
+ report(options.reportType, {
1151
+ metric: 'app-launch',
1152
+ duration: entry.duration,
1153
+ launchType: 'cold',
1154
+ unit: 'ms',
1155
+ });
1156
+ }
1157
+ }
1228
1158
  }
1229
- }
1159
+ catch {
1160
+ // ignore
1161
+ }
1162
+ });
1163
+ observer.observe({ entryTypes: ['navigation', 'render'] });
1230
1164
  }
1231
1165
  catch {
1232
1166
  // ignore
@@ -2056,7 +1990,7 @@ class ClsLoggerCore {
2056
1990
  try {
2057
1991
  const userRaw = this.userGenerateBaseFields ? this.userGenerateBaseFields() : undefined;
2058
1992
  if (userRaw && isPlainObject(userRaw))
2059
- user = normalizeFlatFields({ ...userRaw, userId: this.userId, userName: this.userName }, 'generateBaseFields');
1993
+ user = normalizeFlatFields({ ...userRaw }, 'generateBaseFields');
2060
1994
  }
2061
1995
  catch {
2062
1996
  user = undefined;
@@ -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;AAuSzD,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,QAAQ,EAChB,IAAI,GAAE,OAAO,GAAG,yBAAyB,GAAG,SAAc,GACzD,IAAI,CAsBN"}
package/dist/web.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;