@johnboxcodes/boxlogger 0.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.
@@ -0,0 +1,242 @@
1
+ /**
2
+ * Scope Management (Sentry-compatible)
3
+ *
4
+ * Provides isolated context for error tracking, matching Sentry's scope API.
5
+ *
6
+ * @module scope
7
+ * @packageDocumentation
8
+ */
9
+ import type { UserInfo, LogMetadata } from './types.js';
10
+ /**
11
+ * Sentry-compatible severity level type
12
+ */
13
+ export type SeverityLevel = 'fatal' | 'error' | 'warning' | 'log' | 'info' | 'debug';
14
+ /**
15
+ * Breadcrumb data structure (Sentry-compatible)
16
+ */
17
+ export interface Breadcrumb {
18
+ /** Breadcrumb type (default, http, navigation, etc.) */
19
+ type?: string;
20
+ /** Category for grouping (ui.click, api, navigation, etc.) */
21
+ category?: string;
22
+ /** Human-readable message */
23
+ message?: string;
24
+ /** Severity level */
25
+ level?: SeverityLevel;
26
+ /** Timestamp (ISO 8601) */
27
+ timestamp?: number;
28
+ /** Additional data */
29
+ data?: Record<string, unknown>;
30
+ }
31
+ /**
32
+ * Capture context for exceptions and messages (Sentry-compatible)
33
+ */
34
+ export interface CaptureContext {
35
+ /** Tags for filtering and searching */
36
+ tags?: Record<string, string>;
37
+ /** Extra data attached to the event */
38
+ extra?: Record<string, unknown>;
39
+ /** User context */
40
+ user?: UserInfo;
41
+ /** Severity level */
42
+ level?: SeverityLevel;
43
+ /** Custom fingerprint for grouping */
44
+ fingerprint?: string[];
45
+ /** Named contexts (e.g., 'browser', 'os', 'device') */
46
+ contexts?: Record<string, Record<string, unknown>>;
47
+ }
48
+ /**
49
+ * Scope class for managing event context (Sentry-compatible)
50
+ *
51
+ * @remarks
52
+ * A scope holds context data that gets attached to events.
53
+ * Use `withScope()` to create isolated scopes for specific operations.
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * import { withScope, captureException } from '@nodelogger/core';
58
+ *
59
+ * withScope((scope) => {
60
+ * scope.setTag('transaction', 'payment');
61
+ * scope.setExtra('orderId', '12345');
62
+ * scope.setFingerprint(['payment', 'error']);
63
+ *
64
+ * captureException(error);
65
+ * });
66
+ * ```
67
+ */
68
+ export declare class Scope {
69
+ private _tags;
70
+ private _extra;
71
+ private _user;
72
+ private _level;
73
+ private _fingerprint;
74
+ private _breadcrumbs;
75
+ private _contexts;
76
+ private _maxBreadcrumbs;
77
+ /**
78
+ * Create a new scope, optionally cloning from another
79
+ */
80
+ constructor(scope?: Scope);
81
+ /**
82
+ * Set a single tag
83
+ * @param key - Tag key (max 32 chars recommended)
84
+ * @param value - Tag value (max 200 chars recommended)
85
+ */
86
+ setTag(key: string, value: string): this;
87
+ /**
88
+ * Set multiple tags
89
+ * @param tags - Tags object
90
+ */
91
+ setTags(tags: Record<string, string>): this;
92
+ /**
93
+ * Get all tags
94
+ */
95
+ getTags(): Record<string, string>;
96
+ /**
97
+ * Set extra data
98
+ * @param key - Extra key
99
+ * @param value - Any value
100
+ */
101
+ setExtra(key: string, value: unknown): this;
102
+ /**
103
+ * Set multiple extras
104
+ * @param extras - Extras object
105
+ */
106
+ setExtras(extras: Record<string, unknown>): this;
107
+ /**
108
+ * Get all extras
109
+ */
110
+ getExtras(): Record<string, unknown>;
111
+ /**
112
+ * Set user context
113
+ * @param user - User info or null to clear
114
+ */
115
+ setUser(user: UserInfo | null): this;
116
+ /**
117
+ * Get user context
118
+ */
119
+ getUser(): UserInfo | null;
120
+ /**
121
+ * Set severity level
122
+ * @param level - Severity level
123
+ */
124
+ setLevel(level: SeverityLevel): this;
125
+ /**
126
+ * Get severity level
127
+ */
128
+ getLevel(): SeverityLevel | null;
129
+ /**
130
+ * Set custom fingerprint for error grouping
131
+ * @param fingerprint - Array of strings for grouping
132
+ */
133
+ setFingerprint(fingerprint: string[]): this;
134
+ /**
135
+ * Get fingerprint
136
+ */
137
+ getFingerprint(): string[] | null;
138
+ /**
139
+ * Set a named context
140
+ * @param name - Context name (e.g., 'browser', 'os', 'device', 'custom')
141
+ * @param context - Context data or null to clear
142
+ */
143
+ setContext(name: string, context: Record<string, unknown> | null): this;
144
+ /**
145
+ * Get a named context
146
+ */
147
+ getContext(name: string): Record<string, unknown> | undefined;
148
+ /**
149
+ * Get all contexts
150
+ */
151
+ getContexts(): Record<string, Record<string, unknown>>;
152
+ /**
153
+ * Add a breadcrumb
154
+ * @param breadcrumb - Breadcrumb data
155
+ */
156
+ addBreadcrumb(breadcrumb: Breadcrumb): this;
157
+ /**
158
+ * Get all breadcrumbs
159
+ */
160
+ getBreadcrumbs(): Breadcrumb[];
161
+ /**
162
+ * Clear all breadcrumbs
163
+ */
164
+ clearBreadcrumbs(): this;
165
+ /**
166
+ * Clear all scope data
167
+ */
168
+ clear(): this;
169
+ /**
170
+ * Apply capture context to scope
171
+ * @param captureContext - Context to apply
172
+ */
173
+ applyContext(captureContext: CaptureContext): this;
174
+ /**
175
+ * Convert scope to LogMetadata for storage
176
+ */
177
+ toMetadata(): LogMetadata;
178
+ /**
179
+ * Clone this scope
180
+ */
181
+ clone(): Scope;
182
+ }
183
+ /**
184
+ * Get the global scope
185
+ */
186
+ export declare function getGlobalScope(): Scope;
187
+ /**
188
+ * Get the current scope
189
+ */
190
+ export declare function getCurrentScope(): Scope;
191
+ /**
192
+ * Get the isolation scope (same as current for now)
193
+ */
194
+ export declare function getIsolationScope(): Scope;
195
+ /**
196
+ * Configure the global scope (Sentry-compatible)
197
+ *
198
+ * @param callback - Function to configure the scope
199
+ *
200
+ * @example
201
+ * ```typescript
202
+ * configureScope((scope) => {
203
+ * scope.setTag('environment', 'production');
204
+ * scope.setTag('release', '1.0.0');
205
+ * });
206
+ * ```
207
+ */
208
+ export declare function configureScope(callback: (scope: Scope) => void): void;
209
+ /**
210
+ * Run code with an isolated scope (Sentry-compatible)
211
+ *
212
+ * @param callback - Function to run with isolated scope
213
+ * @returns Result of the callback
214
+ *
215
+ * @example
216
+ * ```typescript
217
+ * withScope((scope) => {
218
+ * scope.setTag('transaction', 'payment');
219
+ * scope.setExtra('orderId', orderId);
220
+ * scope.setFingerprint(['payment', orderId]);
221
+ *
222
+ * try {
223
+ * await processPayment(orderId);
224
+ * } catch (error) {
225
+ * captureException(error);
226
+ * }
227
+ * });
228
+ * ```
229
+ */
230
+ export declare function withScope<T>(callback: (scope: Scope) => T): T;
231
+ /**
232
+ * Run async code with an isolated scope
233
+ *
234
+ * @param callback - Async function to run
235
+ * @returns Promise with callback result
236
+ */
237
+ export declare function withScopeAsync<T>(callback: (scope: Scope) => Promise<T>): Promise<T>;
238
+ /**
239
+ * Reset all scopes (for testing)
240
+ */
241
+ export declare function resetScopes(): void;
242
+ //# sourceMappingURL=scope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope.d.ts","sourceRoot":"","sources":["../src/scope.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAY,QAAQ,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAElE;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,OAAO,GAAG,SAAS,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,CAAC;AAErF;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,wDAAwD;IACxD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,6BAA6B;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,qBAAqB;IACrB,KAAK,CAAC,EAAE,aAAa,CAAC;IACtB,2BAA2B;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,sBAAsB;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,uCAAuC;IACvC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,uCAAuC;IACvC,KAAK,CA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AIrE;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,CAAC,GAAG,CAAC,CAY7D;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,CAAC,EACpC,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,CAAC,CAAC,GACrC,OAAO,CAAC,CAAC,CAAC,CAUZ;AAED;;GAEG;AACH,wBAAgB,WAAW,IAAI,IAAI,CAIlC"}
package/dist/scope.js ADDED
@@ -0,0 +1,373 @@
1
+ /**
2
+ * Scope Management (Sentry-compatible)
3
+ *
4
+ * Provides isolated context for error tracking, matching Sentry's scope API.
5
+ *
6
+ * @module scope
7
+ * @packageDocumentation
8
+ */
9
+ /**
10
+ * Scope class for managing event context (Sentry-compatible)
11
+ *
12
+ * @remarks
13
+ * A scope holds context data that gets attached to events.
14
+ * Use `withScope()` to create isolated scopes for specific operations.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { withScope, captureException } from '@nodelogger/core';
19
+ *
20
+ * withScope((scope) => {
21
+ * scope.setTag('transaction', 'payment');
22
+ * scope.setExtra('orderId', '12345');
23
+ * scope.setFingerprint(['payment', 'error']);
24
+ *
25
+ * captureException(error);
26
+ * });
27
+ * ```
28
+ */
29
+ export class Scope {
30
+ _tags = {};
31
+ _extra = {};
32
+ _user = null;
33
+ _level = null;
34
+ _fingerprint = null;
35
+ _breadcrumbs = [];
36
+ _contexts = {};
37
+ _maxBreadcrumbs = 100;
38
+ /**
39
+ * Create a new scope, optionally cloning from another
40
+ */
41
+ constructor(scope) {
42
+ if (scope) {
43
+ this._tags = { ...scope._tags };
44
+ this._extra = { ...scope._extra };
45
+ this._user = scope._user ? { ...scope._user } : null;
46
+ this._level = scope._level;
47
+ this._fingerprint = scope._fingerprint ? [...scope._fingerprint] : null;
48
+ this._breadcrumbs = [...scope._breadcrumbs];
49
+ this._contexts = JSON.parse(JSON.stringify(scope._contexts));
50
+ }
51
+ }
52
+ /**
53
+ * Set a single tag
54
+ * @param key - Tag key (max 32 chars recommended)
55
+ * @param value - Tag value (max 200 chars recommended)
56
+ */
57
+ setTag(key, value) {
58
+ this._tags[key] = value;
59
+ return this;
60
+ }
61
+ /**
62
+ * Set multiple tags
63
+ * @param tags - Tags object
64
+ */
65
+ setTags(tags) {
66
+ for (const [key, value] of Object.entries(tags)) {
67
+ this._tags[key] = value;
68
+ }
69
+ return this;
70
+ }
71
+ /**
72
+ * Get all tags
73
+ */
74
+ getTags() {
75
+ return { ...this._tags };
76
+ }
77
+ /**
78
+ * Set extra data
79
+ * @param key - Extra key
80
+ * @param value - Any value
81
+ */
82
+ setExtra(key, value) {
83
+ this._extra[key] = value;
84
+ return this;
85
+ }
86
+ /**
87
+ * Set multiple extras
88
+ * @param extras - Extras object
89
+ */
90
+ setExtras(extras) {
91
+ for (const [key, value] of Object.entries(extras)) {
92
+ this._extra[key] = value;
93
+ }
94
+ return this;
95
+ }
96
+ /**
97
+ * Get all extras
98
+ */
99
+ getExtras() {
100
+ return { ...this._extra };
101
+ }
102
+ /**
103
+ * Set user context
104
+ * @param user - User info or null to clear
105
+ */
106
+ setUser(user) {
107
+ this._user = user ? { ...user } : null;
108
+ return this;
109
+ }
110
+ /**
111
+ * Get user context
112
+ */
113
+ getUser() {
114
+ return this._user ? { ...this._user } : null;
115
+ }
116
+ /**
117
+ * Set severity level
118
+ * @param level - Severity level
119
+ */
120
+ setLevel(level) {
121
+ this._level = level;
122
+ return this;
123
+ }
124
+ /**
125
+ * Get severity level
126
+ */
127
+ getLevel() {
128
+ return this._level;
129
+ }
130
+ /**
131
+ * Set custom fingerprint for error grouping
132
+ * @param fingerprint - Array of strings for grouping
133
+ */
134
+ setFingerprint(fingerprint) {
135
+ this._fingerprint = [...fingerprint];
136
+ return this;
137
+ }
138
+ /**
139
+ * Get fingerprint
140
+ */
141
+ getFingerprint() {
142
+ return this._fingerprint ? [...this._fingerprint] : null;
143
+ }
144
+ /**
145
+ * Set a named context
146
+ * @param name - Context name (e.g., 'browser', 'os', 'device', 'custom')
147
+ * @param context - Context data or null to clear
148
+ */
149
+ setContext(name, context) {
150
+ if (context === null) {
151
+ delete this._contexts[name];
152
+ }
153
+ else {
154
+ this._contexts[name] = { ...context };
155
+ }
156
+ return this;
157
+ }
158
+ /**
159
+ * Get a named context
160
+ */
161
+ getContext(name) {
162
+ return this._contexts[name] ? { ...this._contexts[name] } : undefined;
163
+ }
164
+ /**
165
+ * Get all contexts
166
+ */
167
+ getContexts() {
168
+ return JSON.parse(JSON.stringify(this._contexts));
169
+ }
170
+ /**
171
+ * Add a breadcrumb
172
+ * @param breadcrumb - Breadcrumb data
173
+ */
174
+ addBreadcrumb(breadcrumb) {
175
+ const crumb = {
176
+ ...breadcrumb,
177
+ timestamp: breadcrumb.timestamp ?? Date.now() / 1000, // Sentry uses seconds
178
+ };
179
+ this._breadcrumbs.push(crumb);
180
+ // Enforce max breadcrumbs
181
+ if (this._breadcrumbs.length > this._maxBreadcrumbs) {
182
+ this._breadcrumbs.shift();
183
+ }
184
+ return this;
185
+ }
186
+ /**
187
+ * Get all breadcrumbs
188
+ */
189
+ getBreadcrumbs() {
190
+ return [...this._breadcrumbs];
191
+ }
192
+ /**
193
+ * Clear all breadcrumbs
194
+ */
195
+ clearBreadcrumbs() {
196
+ this._breadcrumbs = [];
197
+ return this;
198
+ }
199
+ /**
200
+ * Clear all scope data
201
+ */
202
+ clear() {
203
+ this._tags = {};
204
+ this._extra = {};
205
+ this._user = null;
206
+ this._level = null;
207
+ this._fingerprint = null;
208
+ this._breadcrumbs = [];
209
+ this._contexts = {};
210
+ return this;
211
+ }
212
+ /**
213
+ * Apply capture context to scope
214
+ * @param captureContext - Context to apply
215
+ */
216
+ applyContext(captureContext) {
217
+ if (captureContext.tags) {
218
+ this.setTags(captureContext.tags);
219
+ }
220
+ if (captureContext.extra) {
221
+ this.setExtras(captureContext.extra);
222
+ }
223
+ if (captureContext.user) {
224
+ this.setUser(captureContext.user);
225
+ }
226
+ if (captureContext.level) {
227
+ this.setLevel(captureContext.level);
228
+ }
229
+ if (captureContext.fingerprint) {
230
+ this.setFingerprint(captureContext.fingerprint);
231
+ }
232
+ if (captureContext.contexts) {
233
+ for (const [name, ctx] of Object.entries(captureContext.contexts)) {
234
+ this.setContext(name, ctx);
235
+ }
236
+ }
237
+ return this;
238
+ }
239
+ /**
240
+ * Convert scope to LogMetadata for storage
241
+ */
242
+ toMetadata() {
243
+ const metadata = {};
244
+ if (Object.keys(this._tags).length > 0) {
245
+ metadata.tags = { ...this._tags };
246
+ }
247
+ const extra = { ...this._extra };
248
+ if (this._breadcrumbs.length > 0) {
249
+ extra._breadcrumbs = [...this._breadcrumbs];
250
+ }
251
+ if (this._fingerprint) {
252
+ extra._fingerprint = [...this._fingerprint];
253
+ }
254
+ if (this._contexts && Object.keys(this._contexts).length > 0) {
255
+ extra._contexts = JSON.parse(JSON.stringify(this._contexts));
256
+ }
257
+ if (Object.keys(extra).length > 0) {
258
+ metadata.extra = extra;
259
+ }
260
+ if (this._user) {
261
+ metadata.user = { ...this._user };
262
+ }
263
+ return metadata;
264
+ }
265
+ /**
266
+ * Clone this scope
267
+ */
268
+ clone() {
269
+ return new Scope(this);
270
+ }
271
+ }
272
+ // ============================================================================
273
+ // Global Scope Management
274
+ // ============================================================================
275
+ let _globalScope = new Scope();
276
+ let _currentScope = new Scope();
277
+ const _scopeStack = [];
278
+ /**
279
+ * Get the global scope
280
+ */
281
+ export function getGlobalScope() {
282
+ return _globalScope;
283
+ }
284
+ /**
285
+ * Get the current scope
286
+ */
287
+ export function getCurrentScope() {
288
+ return _currentScope;
289
+ }
290
+ /**
291
+ * Get the isolation scope (same as current for now)
292
+ */
293
+ export function getIsolationScope() {
294
+ return _currentScope;
295
+ }
296
+ /**
297
+ * Configure the global scope (Sentry-compatible)
298
+ *
299
+ * @param callback - Function to configure the scope
300
+ *
301
+ * @example
302
+ * ```typescript
303
+ * configureScope((scope) => {
304
+ * scope.setTag('environment', 'production');
305
+ * scope.setTag('release', '1.0.0');
306
+ * });
307
+ * ```
308
+ */
309
+ export function configureScope(callback) {
310
+ callback(_globalScope);
311
+ // Apply global scope to current scope
312
+ _currentScope = new Scope(_globalScope);
313
+ }
314
+ /**
315
+ * Run code with an isolated scope (Sentry-compatible)
316
+ *
317
+ * @param callback - Function to run with isolated scope
318
+ * @returns Result of the callback
319
+ *
320
+ * @example
321
+ * ```typescript
322
+ * withScope((scope) => {
323
+ * scope.setTag('transaction', 'payment');
324
+ * scope.setExtra('orderId', orderId);
325
+ * scope.setFingerprint(['payment', orderId]);
326
+ *
327
+ * try {
328
+ * await processPayment(orderId);
329
+ * } catch (error) {
330
+ * captureException(error);
331
+ * }
332
+ * });
333
+ * ```
334
+ */
335
+ export function withScope(callback) {
336
+ // Create isolated scope by cloning current
337
+ const previousScope = _currentScope;
338
+ _currentScope = new Scope(_currentScope);
339
+ _scopeStack.push(previousScope);
340
+ try {
341
+ return callback(_currentScope);
342
+ }
343
+ finally {
344
+ // Restore previous scope
345
+ _currentScope = _scopeStack.pop() || new Scope(_globalScope);
346
+ }
347
+ }
348
+ /**
349
+ * Run async code with an isolated scope
350
+ *
351
+ * @param callback - Async function to run
352
+ * @returns Promise with callback result
353
+ */
354
+ export async function withScopeAsync(callback) {
355
+ const previousScope = _currentScope;
356
+ _currentScope = new Scope(_currentScope);
357
+ _scopeStack.push(previousScope);
358
+ try {
359
+ return await callback(_currentScope);
360
+ }
361
+ finally {
362
+ _currentScope = _scopeStack.pop() || new Scope(_globalScope);
363
+ }
364
+ }
365
+ /**
366
+ * Reset all scopes (for testing)
367
+ */
368
+ export function resetScopes() {
369
+ _globalScope = new Scope();
370
+ _currentScope = new Scope();
371
+ _scopeStack.length = 0;
372
+ }
373
+ //# sourceMappingURL=scope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope.js","sourceRoot":"","sources":["../src/scope.ts"],"names":[],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