@contractspec/lib.logger 1.56.1 → 1.58.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.
Files changed (108) hide show
  1. package/dist/context.browser.d.ts +44 -0
  2. package/dist/context.browser.d.ts.map +1 -0
  3. package/dist/context.browser.js +77 -0
  4. package/dist/context.d.ts +22 -0
  5. package/dist/context.d.ts.map +1 -0
  6. package/dist/context.js +1 -0
  7. package/dist/context.node.d.ts +43 -0
  8. package/dist/context.node.d.ts.map +1 -0
  9. package/dist/context.node.js +62 -0
  10. package/dist/elysia-plugin.d.ts +58 -0
  11. package/dist/elysia-plugin.d.ts.map +1 -0
  12. package/dist/elysia-plugin.js +793 -0
  13. package/dist/formatters.d.ts +25 -0
  14. package/dist/formatters.d.ts.map +1 -0
  15. package/dist/formatters.js +232 -0
  16. package/dist/index.browser.d.ts +8 -0
  17. package/dist/index.browser.d.ts.map +1 -0
  18. package/dist/index.browser.js +728 -0
  19. package/dist/index.d.ts +9 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +800 -0
  22. package/dist/logger.browser.d.ts +48 -0
  23. package/dist/logger.browser.d.ts.map +1 -0
  24. package/dist/logger.browser.js +722 -0
  25. package/dist/logger.d.ts +2 -0
  26. package/dist/logger.d.ts.map +1 -0
  27. package/dist/logger.js +707 -0
  28. package/dist/logger.node.d.ts +48 -0
  29. package/dist/logger.node.d.ts.map +1 -0
  30. package/dist/logger.node.js +707 -0
  31. package/dist/node/context.js +0 -0
  32. package/dist/node/context.node.js +61 -0
  33. package/dist/node/elysia-plugin.js +792 -0
  34. package/dist/node/formatters.js +231 -0
  35. package/dist/node/index.js +799 -0
  36. package/dist/node/logger.js +706 -0
  37. package/dist/node/logger.node.js +706 -0
  38. package/dist/node/timer.js +122 -0
  39. package/dist/node/tracer.js +274 -0
  40. package/dist/node/tracer.node.js +274 -0
  41. package/dist/node/types.js +13 -0
  42. package/dist/timer.d.ts +99 -0
  43. package/dist/timer.d.ts.map +1 -0
  44. package/dist/timer.js +123 -0
  45. package/dist/tracer.browser.d.ts +47 -0
  46. package/dist/tracer.browser.d.ts.map +1 -0
  47. package/dist/tracer.browser.js +290 -0
  48. package/dist/tracer.d.ts +2 -0
  49. package/dist/tracer.d.ts.map +1 -0
  50. package/dist/tracer.js +275 -0
  51. package/dist/tracer.node.d.ts +47 -0
  52. package/dist/tracer.node.d.ts.map +1 -0
  53. package/dist/tracer.node.js +275 -0
  54. package/dist/types.d.ts +68 -0
  55. package/dist/types.d.ts.map +1 -0
  56. package/dist/types.js +14 -0
  57. package/package.json +112 -42
  58. package/dist/context.browser.d.mts +0 -49
  59. package/dist/context.browser.d.mts.map +0 -1
  60. package/dist/context.browser.mjs +0 -89
  61. package/dist/context.browser.mjs.map +0 -1
  62. package/dist/context.d.mts +0 -17
  63. package/dist/context.d.mts.map +0 -1
  64. package/dist/context.mjs +0 -1
  65. package/dist/context.node.d.mts +0 -48
  66. package/dist/context.node.d.mts.map +0 -1
  67. package/dist/context.node.mjs +0 -79
  68. package/dist/context.node.mjs.map +0 -1
  69. package/dist/elysia-plugin.d.mts +0 -63
  70. package/dist/elysia-plugin.d.mts.map +0 -1
  71. package/dist/elysia-plugin.mjs +0 -84
  72. package/dist/elysia-plugin.mjs.map +0 -1
  73. package/dist/formatters.d.mts +0 -29
  74. package/dist/formatters.d.mts.map +0 -1
  75. package/dist/formatters.mjs +0 -180
  76. package/dist/formatters.mjs.map +0 -1
  77. package/dist/index.browser.d.mts +0 -7
  78. package/dist/index.browser.mjs +0 -8
  79. package/dist/index.d.mts +0 -8
  80. package/dist/index.mjs +0 -9
  81. package/dist/logger.browser.d.mts +0 -51
  82. package/dist/logger.browser.d.mts.map +0 -1
  83. package/dist/logger.browser.mjs +0 -190
  84. package/dist/logger.browser.mjs.map +0 -1
  85. package/dist/logger.d.mts +0 -2
  86. package/dist/logger.mjs +0 -3
  87. package/dist/logger.node.d.mts +0 -51
  88. package/dist/logger.node.d.mts.map +0 -1
  89. package/dist/logger.node.mjs +0 -190
  90. package/dist/logger.node.mjs.map +0 -1
  91. package/dist/timer.d.mts +0 -103
  92. package/dist/timer.d.mts.map +0 -1
  93. package/dist/timer.mjs +0 -165
  94. package/dist/timer.mjs.map +0 -1
  95. package/dist/tracer.browser.d.mts +0 -51
  96. package/dist/tracer.browser.d.mts.map +0 -1
  97. package/dist/tracer.browser.mjs +0 -116
  98. package/dist/tracer.browser.mjs.map +0 -1
  99. package/dist/tracer.d.mts +0 -2
  100. package/dist/tracer.mjs +0 -3
  101. package/dist/tracer.node.d.mts +0 -51
  102. package/dist/tracer.node.d.mts.map +0 -1
  103. package/dist/tracer.node.mjs +0 -116
  104. package/dist/tracer.node.mjs.map +0 -1
  105. package/dist/types.d.mts +0 -71
  106. package/dist/types.d.mts.map +0 -1
  107. package/dist/types.mjs +0 -14
  108. package/dist/types.mjs.map +0 -1
package/dist/tracer.js ADDED
@@ -0,0 +1,275 @@
1
+ // @bun
2
+ // src/context.node.ts
3
+ import { AsyncLocalStorage } from "async_hooks";
4
+
5
+ class LogContext {
6
+ static instance;
7
+ storage;
8
+ constructor() {
9
+ this.storage = new AsyncLocalStorage;
10
+ }
11
+ static getInstance() {
12
+ if (!LogContext.instance) {
13
+ LogContext.instance = new LogContext;
14
+ }
15
+ return LogContext.instance;
16
+ }
17
+ run(context, fn) {
18
+ const contextData = {
19
+ context: { ...context },
20
+ trace: this.getCurrentTrace()
21
+ };
22
+ return this.storage.run(contextData, fn);
23
+ }
24
+ extend(additionalContext, fn) {
25
+ const currentContext = this.getContext();
26
+ const mergedContext = {
27
+ ...currentContext,
28
+ ...additionalContext
29
+ };
30
+ return this.run(mergedContext, fn);
31
+ }
32
+ set(key, value) {
33
+ const current = this.storage.getStore();
34
+ if (current) {
35
+ current.context[key] = value;
36
+ }
37
+ }
38
+ get(key) {
39
+ const current = this.storage.getStore();
40
+ return current?.context?.[key];
41
+ }
42
+ getContext() {
43
+ const current = this.storage.getStore();
44
+ return current?.context || {};
45
+ }
46
+ setTrace(trace) {
47
+ const current = this.storage.getStore();
48
+ if (current) {
49
+ current.trace = trace;
50
+ }
51
+ }
52
+ getCurrentTrace() {
53
+ const current = this.storage.getStore();
54
+ return current?.trace;
55
+ }
56
+ generateId() {
57
+ return crypto.randomUUID();
58
+ }
59
+ }
60
+
61
+ // src/timer.ts
62
+ class Timer {
63
+ id;
64
+ startTime;
65
+ laps = [];
66
+ stopped = false;
67
+ stopTime;
68
+ constructor(id) {
69
+ this.id = id || crypto.randomUUID();
70
+ this.startTime = performance.now();
71
+ }
72
+ stop() {
73
+ if (this.stopped) {
74
+ return this.getElapsed();
75
+ }
76
+ this.stopTime = performance.now();
77
+ this.stopped = true;
78
+ const elapsed = this.stopTime - this.startTime;
79
+ this.laps.push({
80
+ label: "stop",
81
+ time: this.stopTime,
82
+ elapsed
83
+ });
84
+ return elapsed;
85
+ }
86
+ lap(label) {
87
+ if (this.stopped) {
88
+ return this.getElapsed();
89
+ }
90
+ const now = performance.now();
91
+ const elapsed = now - this.startTime;
92
+ this.laps.push({
93
+ label: label || `lap-${this.laps.length + 1}`,
94
+ time: now,
95
+ elapsed
96
+ });
97
+ return elapsed;
98
+ }
99
+ getElapsed() {
100
+ if (this.stopped && this.stopTime) {
101
+ return this.stopTime - this.startTime;
102
+ }
103
+ return performance.now() - this.startTime;
104
+ }
105
+ getLaps() {
106
+ return [...this.laps];
107
+ }
108
+ getSummary() {
109
+ return {
110
+ id: this.id,
111
+ totalTime: this.getElapsed(),
112
+ isRunning: !this.stopped,
113
+ laps: this.getLaps()
114
+ };
115
+ }
116
+ reset() {
117
+ this.startTime = performance.now();
118
+ this.laps = [];
119
+ this.stopped = false;
120
+ this.stopTime = undefined;
121
+ }
122
+ }
123
+
124
+ class TimerManager {
125
+ timers = new Map;
126
+ start(id) {
127
+ const timer = new Timer(id);
128
+ this.timers.set(timer.id, timer);
129
+ return timer;
130
+ }
131
+ get(id) {
132
+ return this.timers.get(id);
133
+ }
134
+ stop(id) {
135
+ const timer = this.timers.get(id);
136
+ if (timer) {
137
+ const elapsed = timer.stop();
138
+ this.timers.delete(id);
139
+ return elapsed;
140
+ }
141
+ return;
142
+ }
143
+ getActive() {
144
+ return Array.from(this.timers.values()).filter((timer) => !timer.getSummary().isRunning === false);
145
+ }
146
+ clear() {
147
+ this.timers.clear();
148
+ }
149
+ getSummary() {
150
+ return Array.from(this.timers.values()).map((timer) => timer.getSummary());
151
+ }
152
+ }
153
+ async function timed(operation, options) {
154
+ const timer = new Timer(options?.id);
155
+ try {
156
+ const result = await operation();
157
+ const elapsed = timer.stop();
158
+ options?.onComplete?.(elapsed);
159
+ return { result, elapsed, timer };
160
+ } catch (error) {
161
+ timer.stop();
162
+ throw error;
163
+ }
164
+ }
165
+ function timedSync(operation, options) {
166
+ const timer = new Timer(options?.id);
167
+ try {
168
+ const result = operation();
169
+ const elapsed = timer.stop();
170
+ options?.onComplete?.(elapsed);
171
+ return { result, elapsed, timer };
172
+ } catch (error) {
173
+ timer.stop();
174
+ throw error;
175
+ }
176
+ }
177
+
178
+ // src/tracer.node.ts
179
+ class Tracer {
180
+ context;
181
+ activeSpans = new Map;
182
+ constructor() {
183
+ this.context = LogContext.getInstance();
184
+ }
185
+ startSpan(options) {
186
+ const parentTrace = this.context.getCurrentTrace();
187
+ const traceId = parentTrace?.traceId || this.generateTraceId();
188
+ const span = {
189
+ traceId,
190
+ parentId: parentTrace?.spanId,
191
+ spanId: this.generateSpanId(),
192
+ operationType: options.operationType,
193
+ operationName: options.operationName,
194
+ startTime: performance.now(),
195
+ metadata: { ...options.metadata },
196
+ tags: [...options.tags || []]
197
+ };
198
+ this.activeSpans.set(span.spanId, span);
199
+ this.context.setTrace(span);
200
+ return span;
201
+ }
202
+ finishSpan(spanId) {
203
+ const span = this.activeSpans.get(spanId);
204
+ if (!span) {
205
+ return;
206
+ }
207
+ const duration = performance.now() - span.startTime;
208
+ this.activeSpans.delete(spanId);
209
+ const currentTrace = this.context.getCurrentTrace();
210
+ if (currentTrace?.spanId === spanId && span.parentId) {
211
+ const parentSpan = this.findSpanById(span.parentId);
212
+ if (parentSpan) {
213
+ this.context.setTrace(parentSpan);
214
+ }
215
+ }
216
+ return duration;
217
+ }
218
+ async trace(options, fn) {
219
+ const span = this.startSpan(options);
220
+ const timer = options.autoTiming !== false ? new Timer(`trace-${span.spanId}`) : undefined;
221
+ try {
222
+ const result = await fn();
223
+ const duration = this.finishSpan(span.spanId);
224
+ if (timer) {
225
+ timer.stop();
226
+ }
227
+ if (duration !== undefined) {
228
+ span.metadata.duration = duration;
229
+ }
230
+ return result;
231
+ } catch (error) {
232
+ span.metadata.error = {
233
+ name: error.name || "Unknown",
234
+ message: error.message || "Unknown error",
235
+ stack: error.stack
236
+ };
237
+ const duration = this.finishSpan(span.spanId);
238
+ span.metadata.duration = duration;
239
+ if (timer) {
240
+ timer.stop();
241
+ }
242
+ throw error;
243
+ }
244
+ }
245
+ addMetadata(key, value) {
246
+ const currentTrace = this.context.getCurrentTrace();
247
+ if (currentTrace) {
248
+ currentTrace.metadata[key] = value;
249
+ }
250
+ }
251
+ addTags(...tags) {
252
+ const currentTrace = this.context.getCurrentTrace();
253
+ if (currentTrace) {
254
+ currentTrace.tags.push(...tags);
255
+ }
256
+ }
257
+ getCurrentTrace() {
258
+ return this.context.getCurrentTrace();
259
+ }
260
+ getActiveSpans() {
261
+ return Array.from(this.activeSpans.values());
262
+ }
263
+ findSpanById(spanId) {
264
+ return this.activeSpans.get(spanId);
265
+ }
266
+ generateTraceId() {
267
+ return crypto.randomUUID().replace(/-/g, "");
268
+ }
269
+ generateSpanId() {
270
+ return crypto.randomUUID().replace(/-/g, "").substring(0, 16);
271
+ }
272
+ }
273
+ export {
274
+ Tracer
275
+ };
@@ -0,0 +1,47 @@
1
+ import type { TraceContext, TracingOptions } from './types';
2
+ export declare class Tracer {
3
+ private context;
4
+ private activeSpans;
5
+ constructor();
6
+ /**
7
+ * Start a new trace span
8
+ */
9
+ startSpan(options: TracingOptions): TraceContext;
10
+ /**
11
+ * Finish a trace span
12
+ */
13
+ finishSpan(spanId: string): number | undefined;
14
+ /**
15
+ * Execute a function within a trace span
16
+ */
17
+ trace<T>(options: TracingOptions, fn: () => T | Promise<T>): Promise<T>;
18
+ /**
19
+ * Add metadata to current span
20
+ */
21
+ addMetadata(key: string, value: unknown): void;
22
+ /**
23
+ * Add tags to current span
24
+ */
25
+ addTags(...tags: string[]): void;
26
+ /**
27
+ * Get current trace context
28
+ */
29
+ getCurrentTrace(): TraceContext | undefined;
30
+ /**
31
+ * Get all active spans
32
+ */
33
+ getActiveSpans(): TraceContext[];
34
+ /**
35
+ * Find a span by ID
36
+ */
37
+ findSpanById(spanId: string): TraceContext | undefined;
38
+ /**
39
+ * Generate a unique trace ID
40
+ */
41
+ private generateTraceId;
42
+ /**
43
+ * Generate a unique span ID
44
+ */
45
+ private generateSpanId;
46
+ }
47
+ //# sourceMappingURL=tracer.node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracer.node.d.ts","sourceRoot":"","sources":["../src/tracer.node.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAI5D,qBAAa,MAAM;IACjB,OAAO,CAAC,OAAO,CAAa;IAC5B,OAAO,CAAC,WAAW,CAAmC;;IAMtD;;OAEG;IACH,SAAS,CAAC,OAAO,EAAE,cAAc,GAAG,YAAY;IAqBhD;;OAEG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAqB9C;;OAEG;IACG,KAAK,CAAC,CAAC,EACX,OAAO,EAAE,cAAc,EACvB,EAAE,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GACvB,OAAO,CAAC,CAAC,CAAC;IAuCb;;OAEG;IACH,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI;IAO9C;;OAEG;IACH,OAAO,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI;IAOhC;;OAEG;IACH,eAAe,IAAI,YAAY,GAAG,SAAS;IAI3C;;OAEG;IACH,cAAc,IAAI,YAAY,EAAE;IAIhC;;OAEG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;IAItD;;OAEG;IACH,OAAO,CAAC,eAAe;IAIvB;;OAEG;IACH,OAAO,CAAC,cAAc;CAGvB"}
@@ -0,0 +1,275 @@
1
+ // @bun
2
+ // src/context.node.ts
3
+ import { AsyncLocalStorage } from "async_hooks";
4
+
5
+ class LogContext {
6
+ static instance;
7
+ storage;
8
+ constructor() {
9
+ this.storage = new AsyncLocalStorage;
10
+ }
11
+ static getInstance() {
12
+ if (!LogContext.instance) {
13
+ LogContext.instance = new LogContext;
14
+ }
15
+ return LogContext.instance;
16
+ }
17
+ run(context, fn) {
18
+ const contextData = {
19
+ context: { ...context },
20
+ trace: this.getCurrentTrace()
21
+ };
22
+ return this.storage.run(contextData, fn);
23
+ }
24
+ extend(additionalContext, fn) {
25
+ const currentContext = this.getContext();
26
+ const mergedContext = {
27
+ ...currentContext,
28
+ ...additionalContext
29
+ };
30
+ return this.run(mergedContext, fn);
31
+ }
32
+ set(key, value) {
33
+ const current = this.storage.getStore();
34
+ if (current) {
35
+ current.context[key] = value;
36
+ }
37
+ }
38
+ get(key) {
39
+ const current = this.storage.getStore();
40
+ return current?.context?.[key];
41
+ }
42
+ getContext() {
43
+ const current = this.storage.getStore();
44
+ return current?.context || {};
45
+ }
46
+ setTrace(trace) {
47
+ const current = this.storage.getStore();
48
+ if (current) {
49
+ current.trace = trace;
50
+ }
51
+ }
52
+ getCurrentTrace() {
53
+ const current = this.storage.getStore();
54
+ return current?.trace;
55
+ }
56
+ generateId() {
57
+ return crypto.randomUUID();
58
+ }
59
+ }
60
+
61
+ // src/timer.ts
62
+ class Timer {
63
+ id;
64
+ startTime;
65
+ laps = [];
66
+ stopped = false;
67
+ stopTime;
68
+ constructor(id) {
69
+ this.id = id || crypto.randomUUID();
70
+ this.startTime = performance.now();
71
+ }
72
+ stop() {
73
+ if (this.stopped) {
74
+ return this.getElapsed();
75
+ }
76
+ this.stopTime = performance.now();
77
+ this.stopped = true;
78
+ const elapsed = this.stopTime - this.startTime;
79
+ this.laps.push({
80
+ label: "stop",
81
+ time: this.stopTime,
82
+ elapsed
83
+ });
84
+ return elapsed;
85
+ }
86
+ lap(label) {
87
+ if (this.stopped) {
88
+ return this.getElapsed();
89
+ }
90
+ const now = performance.now();
91
+ const elapsed = now - this.startTime;
92
+ this.laps.push({
93
+ label: label || `lap-${this.laps.length + 1}`,
94
+ time: now,
95
+ elapsed
96
+ });
97
+ return elapsed;
98
+ }
99
+ getElapsed() {
100
+ if (this.stopped && this.stopTime) {
101
+ return this.stopTime - this.startTime;
102
+ }
103
+ return performance.now() - this.startTime;
104
+ }
105
+ getLaps() {
106
+ return [...this.laps];
107
+ }
108
+ getSummary() {
109
+ return {
110
+ id: this.id,
111
+ totalTime: this.getElapsed(),
112
+ isRunning: !this.stopped,
113
+ laps: this.getLaps()
114
+ };
115
+ }
116
+ reset() {
117
+ this.startTime = performance.now();
118
+ this.laps = [];
119
+ this.stopped = false;
120
+ this.stopTime = undefined;
121
+ }
122
+ }
123
+
124
+ class TimerManager {
125
+ timers = new Map;
126
+ start(id) {
127
+ const timer = new Timer(id);
128
+ this.timers.set(timer.id, timer);
129
+ return timer;
130
+ }
131
+ get(id) {
132
+ return this.timers.get(id);
133
+ }
134
+ stop(id) {
135
+ const timer = this.timers.get(id);
136
+ if (timer) {
137
+ const elapsed = timer.stop();
138
+ this.timers.delete(id);
139
+ return elapsed;
140
+ }
141
+ return;
142
+ }
143
+ getActive() {
144
+ return Array.from(this.timers.values()).filter((timer) => !timer.getSummary().isRunning === false);
145
+ }
146
+ clear() {
147
+ this.timers.clear();
148
+ }
149
+ getSummary() {
150
+ return Array.from(this.timers.values()).map((timer) => timer.getSummary());
151
+ }
152
+ }
153
+ async function timed(operation, options) {
154
+ const timer = new Timer(options?.id);
155
+ try {
156
+ const result = await operation();
157
+ const elapsed = timer.stop();
158
+ options?.onComplete?.(elapsed);
159
+ return { result, elapsed, timer };
160
+ } catch (error) {
161
+ timer.stop();
162
+ throw error;
163
+ }
164
+ }
165
+ function timedSync(operation, options) {
166
+ const timer = new Timer(options?.id);
167
+ try {
168
+ const result = operation();
169
+ const elapsed = timer.stop();
170
+ options?.onComplete?.(elapsed);
171
+ return { result, elapsed, timer };
172
+ } catch (error) {
173
+ timer.stop();
174
+ throw error;
175
+ }
176
+ }
177
+
178
+ // src/tracer.node.ts
179
+ class Tracer {
180
+ context;
181
+ activeSpans = new Map;
182
+ constructor() {
183
+ this.context = LogContext.getInstance();
184
+ }
185
+ startSpan(options) {
186
+ const parentTrace = this.context.getCurrentTrace();
187
+ const traceId = parentTrace?.traceId || this.generateTraceId();
188
+ const span = {
189
+ traceId,
190
+ parentId: parentTrace?.spanId,
191
+ spanId: this.generateSpanId(),
192
+ operationType: options.operationType,
193
+ operationName: options.operationName,
194
+ startTime: performance.now(),
195
+ metadata: { ...options.metadata },
196
+ tags: [...options.tags || []]
197
+ };
198
+ this.activeSpans.set(span.spanId, span);
199
+ this.context.setTrace(span);
200
+ return span;
201
+ }
202
+ finishSpan(spanId) {
203
+ const span = this.activeSpans.get(spanId);
204
+ if (!span) {
205
+ return;
206
+ }
207
+ const duration = performance.now() - span.startTime;
208
+ this.activeSpans.delete(spanId);
209
+ const currentTrace = this.context.getCurrentTrace();
210
+ if (currentTrace?.spanId === spanId && span.parentId) {
211
+ const parentSpan = this.findSpanById(span.parentId);
212
+ if (parentSpan) {
213
+ this.context.setTrace(parentSpan);
214
+ }
215
+ }
216
+ return duration;
217
+ }
218
+ async trace(options, fn) {
219
+ const span = this.startSpan(options);
220
+ const timer = options.autoTiming !== false ? new Timer(`trace-${span.spanId}`) : undefined;
221
+ try {
222
+ const result = await fn();
223
+ const duration = this.finishSpan(span.spanId);
224
+ if (timer) {
225
+ timer.stop();
226
+ }
227
+ if (duration !== undefined) {
228
+ span.metadata.duration = duration;
229
+ }
230
+ return result;
231
+ } catch (error) {
232
+ span.metadata.error = {
233
+ name: error.name || "Unknown",
234
+ message: error.message || "Unknown error",
235
+ stack: error.stack
236
+ };
237
+ const duration = this.finishSpan(span.spanId);
238
+ span.metadata.duration = duration;
239
+ if (timer) {
240
+ timer.stop();
241
+ }
242
+ throw error;
243
+ }
244
+ }
245
+ addMetadata(key, value) {
246
+ const currentTrace = this.context.getCurrentTrace();
247
+ if (currentTrace) {
248
+ currentTrace.metadata[key] = value;
249
+ }
250
+ }
251
+ addTags(...tags) {
252
+ const currentTrace = this.context.getCurrentTrace();
253
+ if (currentTrace) {
254
+ currentTrace.tags.push(...tags);
255
+ }
256
+ }
257
+ getCurrentTrace() {
258
+ return this.context.getCurrentTrace();
259
+ }
260
+ getActiveSpans() {
261
+ return Array.from(this.activeSpans.values());
262
+ }
263
+ findSpanById(spanId) {
264
+ return this.activeSpans.get(spanId);
265
+ }
266
+ generateTraceId() {
267
+ return crypto.randomUUID().replace(/-/g, "");
268
+ }
269
+ generateSpanId() {
270
+ return crypto.randomUUID().replace(/-/g, "").substring(0, 16);
271
+ }
272
+ }
273
+ export {
274
+ Tracer
275
+ };
@@ -0,0 +1,68 @@
1
+ export declare enum LogLevel {
2
+ TRACE = 0,
3
+ DEBUG = 1,
4
+ INFO = 2,
5
+ WARN = 3,
6
+ ERROR = 4,
7
+ FATAL = 5
8
+ }
9
+ export interface LogEntry {
10
+ level: LogLevel;
11
+ message: string;
12
+ timestamp: Date;
13
+ traceId?: string;
14
+ parentId?: string;
15
+ spanId?: string;
16
+ context?: Record<string, any>;
17
+ metadata?: Record<string, any>;
18
+ duration?: number;
19
+ error?: Error;
20
+ tags?: string[];
21
+ }
22
+ export interface TraceContext {
23
+ traceId: string;
24
+ parentId?: string;
25
+ spanId: string;
26
+ operationType: string;
27
+ operationName: string;
28
+ startTime: number;
29
+ metadata: Record<string, any>;
30
+ tags: string[];
31
+ }
32
+ export interface TracingOptions {
33
+ operationType: 'http' | 'websocket' | 'cron' | 'queue' | 'database' | 'custom';
34
+ operationName: string;
35
+ metadata?: Record<string, any>;
36
+ tags?: string[];
37
+ autoTiming?: boolean;
38
+ }
39
+ export interface LoggerConfig {
40
+ level: LogLevel;
41
+ environment: 'development' | 'production' | 'test';
42
+ enableTracing: boolean;
43
+ enableTiming: boolean;
44
+ enableContext: boolean;
45
+ enableColors: boolean;
46
+ maxContextDepth: number;
47
+ timestampFormat: 'iso' | 'epoch' | 'relative';
48
+ }
49
+ export interface Formatter {
50
+ format(entry: LogEntry): string;
51
+ }
52
+ export interface Timer {
53
+ readonly id: string;
54
+ stop(): number;
55
+ lap(label?: string): number;
56
+ getElapsed(): number;
57
+ }
58
+ export interface ContextData {
59
+ [key: string]: any;
60
+ userId?: string;
61
+ requestId?: string;
62
+ sessionId?: string;
63
+ ip?: string;
64
+ userAgent?: string;
65
+ }
66
+ export type LogMethod = (message: string, metadata?: Record<string, any>) => void;
67
+ export type TraceMethod = <T>(options: TracingOptions, fn: () => T | Promise<T>) => Promise<T>;
68
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"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"}
package/dist/types.js ADDED
@@ -0,0 +1,14 @@
1
+ // @bun
2
+ // src/types.ts
3
+ var LogLevel;
4
+ ((LogLevel2) => {
5
+ LogLevel2[LogLevel2["TRACE"] = 0] = "TRACE";
6
+ LogLevel2[LogLevel2["DEBUG"] = 1] = "DEBUG";
7
+ LogLevel2[LogLevel2["INFO"] = 2] = "INFO";
8
+ LogLevel2[LogLevel2["WARN"] = 3] = "WARN";
9
+ LogLevel2[LogLevel2["ERROR"] = 4] = "ERROR";
10
+ LogLevel2[LogLevel2["FATAL"] = 5] = "FATAL";
11
+ })(LogLevel ||= {});
12
+ export {
13
+ LogLevel
14
+ };