jason-trace-log 1.0.14 → 1.1.0

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.
@@ -1144,61 +1144,71 @@ var BaseTrace = /** @class */ (function () {
1144
1144
  * @param fields 自定义全局字段
1145
1145
  */
1146
1146
  BaseTrace.prototype.setCustomGlobalFields = function (fields) {
1147
- this.customGlobalFields = __assign(__assign({}, this.customGlobalFields), fields);
1147
+ try {
1148
+ this.customGlobalFields = __assign(__assign({}, this.customGlobalFields), fields);
1149
+ }
1150
+ catch (error) {
1151
+ console.error('Failed to set custom global fields:', error);
1152
+ }
1148
1153
  };
1149
1154
  // 初始化实例
1150
1155
  BaseTrace.init = function (options) {
1151
- var traceSdk = new BaseTrace(options);
1152
- traceSdk.onGlobalError();
1153
- // traceSdk.onGlobalClick()
1154
- traceSdk.onObserverResource();
1155
- window.fetch = interceptFetch({
1156
- pagePath: '',
1157
- onError: function (error) {
1158
- traceSdk.onFetchError(error);
1159
- },
1160
- onBefore: function (props) {
1161
- traceSdk.saveBreadcrumb({
1162
- name: 'fetch',
1163
- level: TraceDataSeverity.Normal,
1164
- type: BreadcrumbTypes.FETCH,
1165
- category: BreadcrumbsCategorys.Http,
1166
- message: props.url,
1167
- time: getTimestamp(),
1168
- request: {
1169
- method: props.method,
1170
- url: props.url,
1171
- options: props.options
1172
- }
1173
- });
1174
- },
1175
- onAfter: function (result) {
1176
- traceSdk.saveBreadcrumb({
1177
- name: 'fetch',
1178
- level: TraceDataSeverity.Normal,
1179
- type: BreadcrumbTypes.FETCH,
1180
- category: BreadcrumbsCategorys.Http,
1181
- message: result.status,
1182
- time: getTimestamp(),
1183
- response: {
1184
- status: result.status,
1185
- statusText: result.statusText
1186
- }
1187
- });
1156
+ try {
1157
+ var traceSdk_1 = new BaseTrace(options);
1158
+ traceSdk_1.onGlobalError();
1159
+ // traceSdk.onGlobalClick()
1160
+ traceSdk_1.onObserverResource();
1161
+ window.fetch = interceptFetch({
1162
+ pagePath: '',
1163
+ onError: function (error) {
1164
+ traceSdk_1.onFetchError(error);
1165
+ },
1166
+ onBefore: function (props) {
1167
+ traceSdk_1.saveBreadcrumb({
1168
+ name: 'fetch',
1169
+ level: TraceDataSeverity.Normal,
1170
+ type: BreadcrumbTypes.FETCH,
1171
+ category: BreadcrumbsCategorys.Http,
1172
+ message: props.url,
1173
+ time: getTimestamp(),
1174
+ request: {
1175
+ method: props.method,
1176
+ url: props.url,
1177
+ options: props.options
1178
+ }
1179
+ });
1180
+ },
1181
+ onAfter: function (result) {
1182
+ traceSdk_1.saveBreadcrumb({
1183
+ name: 'fetch',
1184
+ level: TraceDataSeverity.Normal,
1185
+ type: BreadcrumbTypes.FETCH,
1186
+ category: BreadcrumbsCategorys.Http,
1187
+ message: result.status,
1188
+ time: getTimestamp(),
1189
+ response: {
1190
+ status: result.status,
1191
+ statusText: result.statusText
1192
+ }
1193
+ });
1194
+ }
1195
+ });
1196
+ // 监听页面性能
1197
+ onVitals(traceSdk_1.createPerfReport());
1198
+ if (traceSdk_1.sendMethod !== SendMethod.LTS) {
1199
+ setInterval(function () {
1200
+ traceSdk_1.debug && console.log('[queue] traceSdk.queue: ', traceSdk_1.queue);
1201
+ var data = traceSdk_1.queue.shift();
1202
+ traceSdk_1.debug && console.log('[queue] data: ', data);
1203
+ if (data)
1204
+ send(traceSdk_1.sendMethod, traceSdk_1.dsn, traceSdk_1.ltsConfig, data);
1205
+ }, traceSdk_1.sendTimer);
1188
1206
  }
1189
- });
1190
- // 监听页面性能
1191
- onVitals(traceSdk.createPerfReport());
1192
- if (traceSdk.sendMethod !== SendMethod.LTS) {
1193
- setInterval(function () {
1194
- traceSdk.debug && console.log('[queue] traceSdk.queue: ', traceSdk.queue);
1195
- var data = traceSdk.queue.shift();
1196
- traceSdk.debug && console.log('[queue] data: ', data);
1197
- if (data)
1198
- send(traceSdk.sendMethod, traceSdk.dsn, traceSdk.ltsConfig, data);
1199
- }, traceSdk.sendTimer);
1207
+ return traceSdk_1;
1208
+ }
1209
+ catch (error) {
1210
+ console.error('Failed to initialize trace log:', error);
1200
1211
  }
1201
- return traceSdk;
1202
1212
  };
1203
1213
  return BaseTrace;
1204
1214
  }());
@@ -1211,7 +1221,15 @@ var TraceSdk = /** @class */ (function (_super) {
1211
1221
  return TraceSdk;
1212
1222
  }(BaseTrace));
1213
1223
 
1214
- var instance;
1224
+ var instance = null;
1225
+ // 空实现,用于未初始化时的安全调用
1226
+ var noOpInstance = {
1227
+ info: function () { },
1228
+ warn: function () { },
1229
+ error: function () { },
1230
+ send: function () { },
1231
+ setCustomGlobalFields: function () { }
1232
+ };
1215
1233
  var init = function (options) {
1216
1234
  if (instance) {
1217
1235
  return instance;
@@ -1225,23 +1243,27 @@ var getInstance = function () {
1225
1243
  }
1226
1244
  return instance;
1227
1245
  };
1246
+ // 获取实例,未初始化时返回空实现
1247
+ var getInstanceOrNoOp = function () {
1248
+ return instance || noOpInstance;
1249
+ };
1228
1250
  var traceLogAPI = {
1229
1251
  init: init,
1230
1252
  getInstance: getInstance,
1231
1253
  get info() {
1232
- return getInstance().info.bind(getInstance());
1254
+ return getInstanceOrNoOp().info.bind(getInstanceOrNoOp());
1233
1255
  },
1234
1256
  get warn() {
1235
- return getInstance().warn.bind(getInstance());
1257
+ return getInstanceOrNoOp().warn.bind(getInstanceOrNoOp());
1236
1258
  },
1237
1259
  get error() {
1238
- return getInstance().error.bind(getInstance());
1260
+ return getInstanceOrNoOp().error.bind(getInstanceOrNoOp());
1239
1261
  },
1240
1262
  get send() {
1241
- return getInstance().send.bind(getInstance());
1263
+ return getInstanceOrNoOp().send.bind(getInstanceOrNoOp());
1242
1264
  },
1243
1265
  get setCustomGlobalFields() {
1244
- return getInstance().setCustomGlobalFields.bind(getInstance());
1266
+ return getInstanceOrNoOp().setCustomGlobalFields.bind(getInstanceOrNoOp());
1245
1267
  }
1246
1268
  };
1247
1269