@remote-logger/sdk 0.1.0 → 0.1.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/index.d.mts CHANGED
@@ -84,7 +84,9 @@ declare class Logger {
84
84
  withGroup(group: string): GroupLogger;
85
85
  setTraceId(traceId: string | undefined): void;
86
86
  setGroup(group: string | undefined): void;
87
- interceptConsole(group?: string): void;
87
+ interceptConsole(group?: string, options?: {
88
+ wrapErrors?: boolean;
89
+ }): void;
88
90
  restoreConsole(): void;
89
91
  flush(): Promise<void>;
90
92
  private doFlush;
package/dist/index.d.ts CHANGED
@@ -84,7 +84,9 @@ declare class Logger {
84
84
  withGroup(group: string): GroupLogger;
85
85
  setTraceId(traceId: string | undefined): void;
86
86
  setGroup(group: string | undefined): void;
87
- interceptConsole(group?: string): void;
87
+ interceptConsole(group?: string, options?: {
88
+ wrapErrors?: boolean;
89
+ }): void;
88
90
  restoreConsole(): void;
89
91
  flush(): Promise<void>;
90
92
  private doFlush;
package/dist/index.js CHANGED
@@ -3,6 +3,9 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __typeError = (msg) => {
7
+ throw TypeError(msg);
8
+ };
6
9
  var __export = (target, all) => {
7
10
  for (var name in all)
8
11
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -16,6 +19,9 @@ var __copyProps = (to, from, except, desc) => {
16
19
  return to;
17
20
  };
18
21
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
23
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
24
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
19
25
 
20
26
  // src/index.ts
21
27
  var index_exports = {};
@@ -25,6 +31,314 @@ __export(index_exports, {
25
31
  default: () => index_default
26
32
  });
27
33
  module.exports = __toCommonJS(index_exports);
34
+
35
+ // ../node_modules/non-error/index.js
36
+ var isNonErrorSymbol = /* @__PURE__ */ Symbol("isNonError");
37
+ function defineProperty(object, key, value) {
38
+ Object.defineProperty(object, key, {
39
+ value,
40
+ writable: false,
41
+ enumerable: false,
42
+ configurable: false
43
+ });
44
+ }
45
+ function stringify(value) {
46
+ if (value === void 0) {
47
+ return "undefined";
48
+ }
49
+ if (value === null) {
50
+ return "null";
51
+ }
52
+ if (typeof value === "string") {
53
+ return value;
54
+ }
55
+ if (typeof value === "number" || typeof value === "boolean") {
56
+ return String(value);
57
+ }
58
+ if (typeof value === "bigint") {
59
+ return `${value}n`;
60
+ }
61
+ if (typeof value === "symbol") {
62
+ return value.toString();
63
+ }
64
+ if (typeof value === "function") {
65
+ return `[Function${value.name ? ` ${value.name}` : " (anonymous)"}]`;
66
+ }
67
+ if (value instanceof Error) {
68
+ try {
69
+ return String(value);
70
+ } catch {
71
+ return "<Unserializable error>";
72
+ }
73
+ }
74
+ try {
75
+ return JSON.stringify(value);
76
+ } catch {
77
+ try {
78
+ return String(value);
79
+ } catch {
80
+ return "<Unserializable value>";
81
+ }
82
+ }
83
+ }
84
+ var _NonError_static, handleCallback_fn;
85
+ var _NonError = class _NonError extends Error {
86
+ constructor(value, { superclass: Superclass = Error } = {}) {
87
+ if (_NonError.isNonError(value)) {
88
+ return value;
89
+ }
90
+ if (value instanceof Error) {
91
+ throw new TypeError("Do not pass Error instances to NonError. Throw the error directly instead.");
92
+ }
93
+ super(`Non-error value: ${stringify(value)}`);
94
+ if (Superclass !== Error) {
95
+ Object.setPrototypeOf(this, Superclass.prototype);
96
+ }
97
+ defineProperty(this, "name", "NonError");
98
+ defineProperty(this, isNonErrorSymbol, true);
99
+ defineProperty(this, "isNonError", true);
100
+ defineProperty(this, "value", value);
101
+ }
102
+ static isNonError(value) {
103
+ return value?.[isNonErrorSymbol] === true;
104
+ }
105
+ static try(callback) {
106
+ var _a;
107
+ return __privateMethod(_a = _NonError, _NonError_static, handleCallback_fn).call(_a, callback, []);
108
+ }
109
+ static wrap(callback) {
110
+ return (...arguments_) => {
111
+ var _a;
112
+ return __privateMethod(_a = _NonError, _NonError_static, handleCallback_fn).call(_a, callback, arguments_);
113
+ };
114
+ }
115
+ // This makes instanceof work even when using the `superclass` option
116
+ static [Symbol.hasInstance](instance) {
117
+ return _NonError.isNonError(instance);
118
+ }
119
+ };
120
+ _NonError_static = new WeakSet();
121
+ handleCallback_fn = function(callback, arguments_) {
122
+ try {
123
+ const result = callback(...arguments_);
124
+ if (result && typeof result.then === "function") {
125
+ return (async () => {
126
+ try {
127
+ return await result;
128
+ } catch (error) {
129
+ if (error instanceof Error) {
130
+ throw error;
131
+ }
132
+ throw new _NonError(error);
133
+ }
134
+ })();
135
+ }
136
+ return result;
137
+ } catch (error) {
138
+ if (error instanceof Error) {
139
+ throw error;
140
+ }
141
+ throw new _NonError(error);
142
+ }
143
+ };
144
+ __privateAdd(_NonError, _NonError_static);
145
+ var NonError = _NonError;
146
+
147
+ // ../node_modules/serialize-error/error-constructors.js
148
+ var list = [
149
+ // Native ES errors https://262.ecma-international.org/12.0/#sec-well-known-intrinsic-objects
150
+ Error,
151
+ EvalError,
152
+ RangeError,
153
+ ReferenceError,
154
+ SyntaxError,
155
+ TypeError,
156
+ URIError,
157
+ AggregateError,
158
+ // Built-in errors
159
+ globalThis.DOMException,
160
+ // Node-specific errors
161
+ // https://nodejs.org/api/errors.html
162
+ globalThis.AssertionError,
163
+ globalThis.SystemError
164
+ ].filter(Boolean).map((constructor) => [constructor.name, constructor]);
165
+ var errorConstructors = new Map(list);
166
+ var errorFactories = /* @__PURE__ */ new Map();
167
+
168
+ // ../node_modules/serialize-error/index.js
169
+ var errorProperties = [
170
+ {
171
+ property: "name",
172
+ enumerable: false
173
+ },
174
+ {
175
+ property: "message",
176
+ enumerable: false
177
+ },
178
+ {
179
+ property: "stack",
180
+ enumerable: false
181
+ },
182
+ {
183
+ property: "code",
184
+ enumerable: true
185
+ },
186
+ {
187
+ property: "cause",
188
+ enumerable: false
189
+ },
190
+ {
191
+ property: "errors",
192
+ enumerable: false
193
+ }
194
+ ];
195
+ var toJsonWasCalled = /* @__PURE__ */ new WeakSet();
196
+ var toJSON = (from) => {
197
+ toJsonWasCalled.add(from);
198
+ const json = from.toJSON();
199
+ toJsonWasCalled.delete(from);
200
+ return json;
201
+ };
202
+ var newError = (name) => {
203
+ if (name === "NonError") {
204
+ return new NonError();
205
+ }
206
+ const factory = errorFactories.get(name);
207
+ if (factory) {
208
+ return factory();
209
+ }
210
+ const ErrorConstructor = errorConstructors.get(name) ?? Error;
211
+ return ErrorConstructor === AggregateError ? new ErrorConstructor([]) : new ErrorConstructor();
212
+ };
213
+ var destroyCircular = ({
214
+ from,
215
+ seen,
216
+ to,
217
+ forceEnumerable,
218
+ maxDepth,
219
+ depth,
220
+ useToJSON,
221
+ serialize
222
+ }) => {
223
+ if (!to) {
224
+ if (Array.isArray(from)) {
225
+ to = [];
226
+ } else if (!serialize && isErrorLike(from)) {
227
+ to = newError(from.name);
228
+ } else {
229
+ to = {};
230
+ }
231
+ }
232
+ seen.add(from);
233
+ if (depth >= maxDepth) {
234
+ seen.delete(from);
235
+ return to;
236
+ }
237
+ if (useToJSON && typeof from.toJSON === "function" && !toJsonWasCalled.has(from)) {
238
+ seen.delete(from);
239
+ return toJSON(from);
240
+ }
241
+ const continueDestroyCircular = (value) => destroyCircular({
242
+ from: value,
243
+ seen,
244
+ forceEnumerable,
245
+ maxDepth,
246
+ depth: depth + 1,
247
+ useToJSON,
248
+ serialize
249
+ });
250
+ for (const key of Object.keys(from)) {
251
+ const value = from[key];
252
+ if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
253
+ to[key] = serialize ? "[object Buffer]" : value;
254
+ continue;
255
+ }
256
+ if (value !== null && typeof value === "object" && typeof value.pipe === "function") {
257
+ to[key] = serialize ? "[object Stream]" : value;
258
+ continue;
259
+ }
260
+ if (typeof value === "function") {
261
+ if (!serialize) {
262
+ to[key] = value;
263
+ }
264
+ continue;
265
+ }
266
+ if (serialize && typeof value === "bigint") {
267
+ to[key] = `${value}n`;
268
+ continue;
269
+ }
270
+ if (!value || typeof value !== "object") {
271
+ try {
272
+ to[key] = value;
273
+ } catch {
274
+ }
275
+ continue;
276
+ }
277
+ if (!seen.has(value)) {
278
+ to[key] = continueDestroyCircular(value);
279
+ continue;
280
+ }
281
+ to[key] = "[Circular]";
282
+ }
283
+ if (serialize || to instanceof Error) {
284
+ for (const { property, enumerable } of errorProperties) {
285
+ const value = from[property];
286
+ if (value === void 0 || value === null) {
287
+ continue;
288
+ }
289
+ const descriptor = Object.getOwnPropertyDescriptor(to, property);
290
+ if (descriptor?.configurable === false) {
291
+ continue;
292
+ }
293
+ let processedValue = value;
294
+ if (typeof value === "object") {
295
+ processedValue = seen.has(value) ? "[Circular]" : continueDestroyCircular(value);
296
+ }
297
+ Object.defineProperty(to, property, {
298
+ value: processedValue,
299
+ enumerable: forceEnumerable || enumerable,
300
+ configurable: true,
301
+ writable: true
302
+ });
303
+ }
304
+ }
305
+ seen.delete(from);
306
+ return to;
307
+ };
308
+ function serializeError(value, options = {}) {
309
+ const {
310
+ maxDepth = Number.POSITIVE_INFINITY,
311
+ useToJSON = true
312
+ } = options;
313
+ if (typeof value === "object" && value !== null) {
314
+ return destroyCircular({
315
+ from: value,
316
+ seen: /* @__PURE__ */ new Set(),
317
+ forceEnumerable: true,
318
+ maxDepth,
319
+ depth: 0,
320
+ useToJSON,
321
+ serialize: true
322
+ });
323
+ }
324
+ if (typeof value === "function") {
325
+ value = "<Function>";
326
+ }
327
+ return destroyCircular({
328
+ from: new NonError(value),
329
+ seen: /* @__PURE__ */ new Set(),
330
+ forceEnumerable: true,
331
+ maxDepth,
332
+ depth: 0,
333
+ useToJSON,
334
+ serialize: true
335
+ });
336
+ }
337
+ function isErrorLike(value) {
338
+ return Boolean(value) && typeof value === "object" && typeof value.name === "string" && typeof value.message === "string" && typeof value.stack === "string";
339
+ }
340
+
341
+ // src/index.ts
28
342
  var LEVEL_ORDER = {
29
343
  DEBUG: 0,
30
344
  INFO: 1,
@@ -32,7 +346,7 @@ var LEVEL_ORDER = {
32
346
  ERROR: 3,
33
347
  FATAL: 4
34
348
  };
35
- var SDK_VERSION = "0.1.0";
349
+ var SDK_VERSION = "0.1.3";
36
350
  var DEFAULT_BASE_URL = "https://log.terodato.com";
37
351
  var BATCH_SIZE = 50;
38
352
  var MAX_BUFFER_SIZE = 1e3;
@@ -277,15 +591,27 @@ ${arg.stack}`;
277
591
  setGroup(group) {
278
592
  this.config.group = group;
279
593
  }
280
- interceptConsole(group) {
594
+ interceptConsole(group, options) {
281
595
  if (this.consoleIntercepted) return;
282
596
  this.consoleIntercepted = true;
283
597
  const self = this;
284
598
  const logGroup = group ?? "console";
285
599
  const wrap = (original, level) => {
286
600
  const wrapped = (...args) => {
601
+ let stack;
602
+ let errorType;
603
+ if (level === "ERROR") {
604
+ const errArg = args.find((a) => isErrorLike(a));
605
+ if (errArg) {
606
+ const serialized = serializeError(errArg);
607
+ stack = serialized.stack;
608
+ errorType = serialized.name;
609
+ } else {
610
+ stack = new Error().stack;
611
+ }
612
+ }
287
613
  queueMicrotask(() => {
288
- self.log(level, self.formatArgs(args), { group: logGroup });
614
+ self.log(level, self.formatArgs(args), { group: logGroup, stack, errorType });
289
615
  });
290
616
  return original.apply(console, args);
291
617
  };
@@ -296,6 +622,9 @@ ${arg.stack}`;
296
622
  console.log = wrap(originalConsole.log, "INFO");
297
623
  console.info = wrap(originalConsole.info, "INFO");
298
624
  console.warn = wrap(originalConsole.warn, "WARN");
625
+ if (options?.wrapErrors !== false) {
626
+ console.error = wrap(originalConsole.error, "ERROR");
627
+ }
299
628
  if (typeof window !== "undefined") {
300
629
  this._onError = (event) => {
301
630
  self.log("ERROR", event.message, {
@@ -324,6 +653,9 @@ ${arg.stack}`;
324
653
  console.log = originalConsole.log;
325
654
  console.info = originalConsole.info;
326
655
  console.warn = originalConsole.warn;
656
+ if (console.error !== originalConsole.error) {
657
+ console.error = originalConsole.error;
658
+ }
327
659
  if (typeof window !== "undefined") {
328
660
  if (this._onError) {
329
661
  window.removeEventListener("error", this._onError);
package/dist/index.mjs CHANGED
@@ -1,3 +1,316 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
6
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
7
+
8
+ // ../node_modules/non-error/index.js
9
+ var isNonErrorSymbol = /* @__PURE__ */ Symbol("isNonError");
10
+ function defineProperty(object, key, value) {
11
+ Object.defineProperty(object, key, {
12
+ value,
13
+ writable: false,
14
+ enumerable: false,
15
+ configurable: false
16
+ });
17
+ }
18
+ function stringify(value) {
19
+ if (value === void 0) {
20
+ return "undefined";
21
+ }
22
+ if (value === null) {
23
+ return "null";
24
+ }
25
+ if (typeof value === "string") {
26
+ return value;
27
+ }
28
+ if (typeof value === "number" || typeof value === "boolean") {
29
+ return String(value);
30
+ }
31
+ if (typeof value === "bigint") {
32
+ return `${value}n`;
33
+ }
34
+ if (typeof value === "symbol") {
35
+ return value.toString();
36
+ }
37
+ if (typeof value === "function") {
38
+ return `[Function${value.name ? ` ${value.name}` : " (anonymous)"}]`;
39
+ }
40
+ if (value instanceof Error) {
41
+ try {
42
+ return String(value);
43
+ } catch {
44
+ return "<Unserializable error>";
45
+ }
46
+ }
47
+ try {
48
+ return JSON.stringify(value);
49
+ } catch {
50
+ try {
51
+ return String(value);
52
+ } catch {
53
+ return "<Unserializable value>";
54
+ }
55
+ }
56
+ }
57
+ var _NonError_static, handleCallback_fn;
58
+ var _NonError = class _NonError extends Error {
59
+ constructor(value, { superclass: Superclass = Error } = {}) {
60
+ if (_NonError.isNonError(value)) {
61
+ return value;
62
+ }
63
+ if (value instanceof Error) {
64
+ throw new TypeError("Do not pass Error instances to NonError. Throw the error directly instead.");
65
+ }
66
+ super(`Non-error value: ${stringify(value)}`);
67
+ if (Superclass !== Error) {
68
+ Object.setPrototypeOf(this, Superclass.prototype);
69
+ }
70
+ defineProperty(this, "name", "NonError");
71
+ defineProperty(this, isNonErrorSymbol, true);
72
+ defineProperty(this, "isNonError", true);
73
+ defineProperty(this, "value", value);
74
+ }
75
+ static isNonError(value) {
76
+ return value?.[isNonErrorSymbol] === true;
77
+ }
78
+ static try(callback) {
79
+ var _a;
80
+ return __privateMethod(_a = _NonError, _NonError_static, handleCallback_fn).call(_a, callback, []);
81
+ }
82
+ static wrap(callback) {
83
+ return (...arguments_) => {
84
+ var _a;
85
+ return __privateMethod(_a = _NonError, _NonError_static, handleCallback_fn).call(_a, callback, arguments_);
86
+ };
87
+ }
88
+ // This makes instanceof work even when using the `superclass` option
89
+ static [Symbol.hasInstance](instance) {
90
+ return _NonError.isNonError(instance);
91
+ }
92
+ };
93
+ _NonError_static = new WeakSet();
94
+ handleCallback_fn = function(callback, arguments_) {
95
+ try {
96
+ const result = callback(...arguments_);
97
+ if (result && typeof result.then === "function") {
98
+ return (async () => {
99
+ try {
100
+ return await result;
101
+ } catch (error) {
102
+ if (error instanceof Error) {
103
+ throw error;
104
+ }
105
+ throw new _NonError(error);
106
+ }
107
+ })();
108
+ }
109
+ return result;
110
+ } catch (error) {
111
+ if (error instanceof Error) {
112
+ throw error;
113
+ }
114
+ throw new _NonError(error);
115
+ }
116
+ };
117
+ __privateAdd(_NonError, _NonError_static);
118
+ var NonError = _NonError;
119
+
120
+ // ../node_modules/serialize-error/error-constructors.js
121
+ var list = [
122
+ // Native ES errors https://262.ecma-international.org/12.0/#sec-well-known-intrinsic-objects
123
+ Error,
124
+ EvalError,
125
+ RangeError,
126
+ ReferenceError,
127
+ SyntaxError,
128
+ TypeError,
129
+ URIError,
130
+ AggregateError,
131
+ // Built-in errors
132
+ globalThis.DOMException,
133
+ // Node-specific errors
134
+ // https://nodejs.org/api/errors.html
135
+ globalThis.AssertionError,
136
+ globalThis.SystemError
137
+ ].filter(Boolean).map((constructor) => [constructor.name, constructor]);
138
+ var errorConstructors = new Map(list);
139
+ var errorFactories = /* @__PURE__ */ new Map();
140
+
141
+ // ../node_modules/serialize-error/index.js
142
+ var errorProperties = [
143
+ {
144
+ property: "name",
145
+ enumerable: false
146
+ },
147
+ {
148
+ property: "message",
149
+ enumerable: false
150
+ },
151
+ {
152
+ property: "stack",
153
+ enumerable: false
154
+ },
155
+ {
156
+ property: "code",
157
+ enumerable: true
158
+ },
159
+ {
160
+ property: "cause",
161
+ enumerable: false
162
+ },
163
+ {
164
+ property: "errors",
165
+ enumerable: false
166
+ }
167
+ ];
168
+ var toJsonWasCalled = /* @__PURE__ */ new WeakSet();
169
+ var toJSON = (from) => {
170
+ toJsonWasCalled.add(from);
171
+ const json = from.toJSON();
172
+ toJsonWasCalled.delete(from);
173
+ return json;
174
+ };
175
+ var newError = (name) => {
176
+ if (name === "NonError") {
177
+ return new NonError();
178
+ }
179
+ const factory = errorFactories.get(name);
180
+ if (factory) {
181
+ return factory();
182
+ }
183
+ const ErrorConstructor = errorConstructors.get(name) ?? Error;
184
+ return ErrorConstructor === AggregateError ? new ErrorConstructor([]) : new ErrorConstructor();
185
+ };
186
+ var destroyCircular = ({
187
+ from,
188
+ seen,
189
+ to,
190
+ forceEnumerable,
191
+ maxDepth,
192
+ depth,
193
+ useToJSON,
194
+ serialize
195
+ }) => {
196
+ if (!to) {
197
+ if (Array.isArray(from)) {
198
+ to = [];
199
+ } else if (!serialize && isErrorLike(from)) {
200
+ to = newError(from.name);
201
+ } else {
202
+ to = {};
203
+ }
204
+ }
205
+ seen.add(from);
206
+ if (depth >= maxDepth) {
207
+ seen.delete(from);
208
+ return to;
209
+ }
210
+ if (useToJSON && typeof from.toJSON === "function" && !toJsonWasCalled.has(from)) {
211
+ seen.delete(from);
212
+ return toJSON(from);
213
+ }
214
+ const continueDestroyCircular = (value) => destroyCircular({
215
+ from: value,
216
+ seen,
217
+ forceEnumerable,
218
+ maxDepth,
219
+ depth: depth + 1,
220
+ useToJSON,
221
+ serialize
222
+ });
223
+ for (const key of Object.keys(from)) {
224
+ const value = from[key];
225
+ if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
226
+ to[key] = serialize ? "[object Buffer]" : value;
227
+ continue;
228
+ }
229
+ if (value !== null && typeof value === "object" && typeof value.pipe === "function") {
230
+ to[key] = serialize ? "[object Stream]" : value;
231
+ continue;
232
+ }
233
+ if (typeof value === "function") {
234
+ if (!serialize) {
235
+ to[key] = value;
236
+ }
237
+ continue;
238
+ }
239
+ if (serialize && typeof value === "bigint") {
240
+ to[key] = `${value}n`;
241
+ continue;
242
+ }
243
+ if (!value || typeof value !== "object") {
244
+ try {
245
+ to[key] = value;
246
+ } catch {
247
+ }
248
+ continue;
249
+ }
250
+ if (!seen.has(value)) {
251
+ to[key] = continueDestroyCircular(value);
252
+ continue;
253
+ }
254
+ to[key] = "[Circular]";
255
+ }
256
+ if (serialize || to instanceof Error) {
257
+ for (const { property, enumerable } of errorProperties) {
258
+ const value = from[property];
259
+ if (value === void 0 || value === null) {
260
+ continue;
261
+ }
262
+ const descriptor = Object.getOwnPropertyDescriptor(to, property);
263
+ if (descriptor?.configurable === false) {
264
+ continue;
265
+ }
266
+ let processedValue = value;
267
+ if (typeof value === "object") {
268
+ processedValue = seen.has(value) ? "[Circular]" : continueDestroyCircular(value);
269
+ }
270
+ Object.defineProperty(to, property, {
271
+ value: processedValue,
272
+ enumerable: forceEnumerable || enumerable,
273
+ configurable: true,
274
+ writable: true
275
+ });
276
+ }
277
+ }
278
+ seen.delete(from);
279
+ return to;
280
+ };
281
+ function serializeError(value, options = {}) {
282
+ const {
283
+ maxDepth = Number.POSITIVE_INFINITY,
284
+ useToJSON = true
285
+ } = options;
286
+ if (typeof value === "object" && value !== null) {
287
+ return destroyCircular({
288
+ from: value,
289
+ seen: /* @__PURE__ */ new Set(),
290
+ forceEnumerable: true,
291
+ maxDepth,
292
+ depth: 0,
293
+ useToJSON,
294
+ serialize: true
295
+ });
296
+ }
297
+ if (typeof value === "function") {
298
+ value = "<Function>";
299
+ }
300
+ return destroyCircular({
301
+ from: new NonError(value),
302
+ seen: /* @__PURE__ */ new Set(),
303
+ forceEnumerable: true,
304
+ maxDepth,
305
+ depth: 0,
306
+ useToJSON,
307
+ serialize: true
308
+ });
309
+ }
310
+ function isErrorLike(value) {
311
+ return Boolean(value) && typeof value === "object" && typeof value.name === "string" && typeof value.message === "string" && typeof value.stack === "string";
312
+ }
313
+
1
314
  // src/index.ts
2
315
  var LEVEL_ORDER = {
3
316
  DEBUG: 0,
@@ -6,7 +319,7 @@ var LEVEL_ORDER = {
6
319
  ERROR: 3,
7
320
  FATAL: 4
8
321
  };
9
- var SDK_VERSION = "0.1.0";
322
+ var SDK_VERSION = "0.1.3";
10
323
  var DEFAULT_BASE_URL = "https://log.terodato.com";
11
324
  var BATCH_SIZE = 50;
12
325
  var MAX_BUFFER_SIZE = 1e3;
@@ -251,15 +564,27 @@ ${arg.stack}`;
251
564
  setGroup(group) {
252
565
  this.config.group = group;
253
566
  }
254
- interceptConsole(group) {
567
+ interceptConsole(group, options) {
255
568
  if (this.consoleIntercepted) return;
256
569
  this.consoleIntercepted = true;
257
570
  const self = this;
258
571
  const logGroup = group ?? "console";
259
572
  const wrap = (original, level) => {
260
573
  const wrapped = (...args) => {
574
+ let stack;
575
+ let errorType;
576
+ if (level === "ERROR") {
577
+ const errArg = args.find((a) => isErrorLike(a));
578
+ if (errArg) {
579
+ const serialized = serializeError(errArg);
580
+ stack = serialized.stack;
581
+ errorType = serialized.name;
582
+ } else {
583
+ stack = new Error().stack;
584
+ }
585
+ }
261
586
  queueMicrotask(() => {
262
- self.log(level, self.formatArgs(args), { group: logGroup });
587
+ self.log(level, self.formatArgs(args), { group: logGroup, stack, errorType });
263
588
  });
264
589
  return original.apply(console, args);
265
590
  };
@@ -270,6 +595,9 @@ ${arg.stack}`;
270
595
  console.log = wrap(originalConsole.log, "INFO");
271
596
  console.info = wrap(originalConsole.info, "INFO");
272
597
  console.warn = wrap(originalConsole.warn, "WARN");
598
+ if (options?.wrapErrors !== false) {
599
+ console.error = wrap(originalConsole.error, "ERROR");
600
+ }
273
601
  if (typeof window !== "undefined") {
274
602
  this._onError = (event) => {
275
603
  self.log("ERROR", event.message, {
@@ -298,6 +626,9 @@ ${arg.stack}`;
298
626
  console.log = originalConsole.log;
299
627
  console.info = originalConsole.info;
300
628
  console.warn = originalConsole.warn;
629
+ if (console.error !== originalConsole.error) {
630
+ console.error = originalConsole.error;
631
+ }
301
632
  if (typeof window !== "undefined") {
302
633
  if (this._onError) {
303
634
  window.removeEventListener("error", this._onError);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remote-logger/sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "JavaScript SDK for Remote Logger",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -25,6 +25,7 @@
25
25
  "test": "tsx test/test.ts"
26
26
  },
27
27
  "devDependencies": {
28
+ "serialize-error": "^13.0.1",
28
29
  "tsup": "^8.0.0",
29
30
  "tsx": "^4.7.0",
30
31
  "typescript": "^5.3.0"