@logtape/logtape 1.2.2 → 1.2.4

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/src/sink.ts DELETED
@@ -1,976 +0,0 @@
1
- import { type FilterLike, toFilter } from "./filter.ts";
2
- import {
3
- type ConsoleFormatter,
4
- defaultConsoleFormatter,
5
- defaultTextFormatter,
6
- type TextFormatter,
7
- } from "./formatter.ts";
8
- import { compareLogLevel, type LogLevel } from "./level.ts";
9
- import type { LogRecord } from "./record.ts";
10
-
11
- /**
12
- * A sink is a function that accepts a log record and prints it somewhere.
13
- * Thrown exceptions will be suppressed and then logged to the meta logger,
14
- * a {@link Logger} with the category `["logtape", "meta"]`. (In that case,
15
- * the meta log record will not be passed to the sink to avoid infinite
16
- * recursion.)
17
- *
18
- * @param record The log record to sink.
19
- */
20
- export type Sink = (record: LogRecord) => void;
21
-
22
- /**
23
- * An async sink is a function that accepts a log record and asynchronously
24
- * processes it. This type is used with {@link fromAsyncSink} to create
25
- * a regular sink that properly handles asynchronous operations.
26
- *
27
- * @param record The log record to process asynchronously.
28
- * @returns A promise that resolves when the record has been processed.
29
- * @since 1.0.0
30
- */
31
- export type AsyncSink = (record: LogRecord) => Promise<void>;
32
-
33
- /**
34
- * Turns a sink into a filtered sink. The returned sink only logs records that
35
- * pass the filter.
36
- *
37
- * @example Filter a console sink to only log records with the info level
38
- * ```typescript
39
- * const sink = withFilter(getConsoleSink(), "info");
40
- * ```
41
- *
42
- * @param sink A sink to be filtered.
43
- * @param filter A filter to apply to the sink. It can be either a filter
44
- * function or a {@link LogLevel} string.
45
- * @returns A sink that only logs records that pass the filter.
46
- */
47
- export function withFilter(sink: Sink, filter: FilterLike): Sink {
48
- const filterFunc = toFilter(filter);
49
- return (record: LogRecord) => {
50
- if (filterFunc(record)) sink(record);
51
- };
52
- }
53
-
54
- /**
55
- * Options for the {@link getStreamSink} function.
56
- */
57
- export interface StreamSinkOptions {
58
- /**
59
- * The text formatter to use. Defaults to {@link defaultTextFormatter}.
60
- */
61
- formatter?: TextFormatter;
62
-
63
- /**
64
- * The text encoder to use. Defaults to an instance of {@link TextEncoder}.
65
- */
66
- encoder?: { encode(text: string): Uint8Array };
67
-
68
- /**
69
- * Enable non-blocking mode with optional buffer configuration.
70
- * When enabled, log records are buffered and flushed in the background.
71
- *
72
- * @example Simple non-blocking mode
73
- * ```typescript
74
- * getStreamSink(stream, { nonBlocking: true });
75
- * ```
76
- *
77
- * @example Custom buffer configuration
78
- * ```typescript
79
- * getStreamSink(stream, {
80
- * nonBlocking: {
81
- * bufferSize: 1000,
82
- * flushInterval: 50
83
- * }
84
- * });
85
- * ```
86
- *
87
- * @default `false`
88
- * @since 1.0.0
89
- */
90
- nonBlocking?: boolean | {
91
- /**
92
- * Maximum number of records to buffer before flushing.
93
- * @default `100`
94
- */
95
- bufferSize?: number;
96
-
97
- /**
98
- * Interval in milliseconds between automatic flushes.
99
- * @default `100`
100
- */
101
- flushInterval?: number;
102
- };
103
- }
104
-
105
- /**
106
- * A factory that returns a sink that writes to a {@link WritableStream}.
107
- *
108
- * Note that the `stream` is of Web Streams API, which is different from
109
- * Node.js streams. You can convert a Node.js stream to a Web Streams API
110
- * stream using [`stream.Writable.toWeb()`] method.
111
- *
112
- * [`stream.Writable.toWeb()`]: https://nodejs.org/api/stream.html#streamwritabletowebstreamwritable
113
- *
114
- * @example Sink to the standard error in Deno
115
- * ```typescript
116
- * const stderrSink = getStreamSink(Deno.stderr.writable);
117
- * ```
118
- *
119
- * @example Sink to the standard error in Node.js
120
- * ```typescript
121
- * import stream from "node:stream";
122
- * const stderrSink = getStreamSink(stream.Writable.toWeb(process.stderr));
123
- * ```
124
- *
125
- * @param stream The stream to write to.
126
- * @param options The options for the sink.
127
- * @returns A sink that writes to the stream.
128
- */
129
- export function getStreamSink(
130
- stream: WritableStream,
131
- options: StreamSinkOptions = {},
132
- ): Sink & AsyncDisposable {
133
- const formatter = options.formatter ?? defaultTextFormatter;
134
- const encoder = options.encoder ?? new TextEncoder();
135
- const writer = stream.getWriter();
136
-
137
- if (!options.nonBlocking) {
138
- let lastPromise = Promise.resolve();
139
- const sink: Sink & AsyncDisposable = (record: LogRecord) => {
140
- const bytes = encoder.encode(formatter(record));
141
- lastPromise = lastPromise
142
- .then(() => writer.ready)
143
- .then(() => writer.write(bytes));
144
- };
145
- sink[Symbol.asyncDispose] = async () => {
146
- await lastPromise;
147
- await writer.close();
148
- };
149
- return sink;
150
- }
151
-
152
- // Non-blocking mode implementation
153
- const nonBlockingConfig = options.nonBlocking === true
154
- ? {}
155
- : options.nonBlocking;
156
- const bufferSize = nonBlockingConfig.bufferSize ?? 100;
157
- const flushInterval = nonBlockingConfig.flushInterval ?? 100;
158
-
159
- const buffer: LogRecord[] = [];
160
- let flushTimer: ReturnType<typeof setInterval> | null = null;
161
- let disposed = false;
162
- let activeFlush: Promise<void> | null = null;
163
- const maxBufferSize = bufferSize * 2; // Overflow protection
164
-
165
- async function flush(): Promise<void> {
166
- if (buffer.length === 0) return;
167
-
168
- const records = buffer.splice(0);
169
- for (const record of records) {
170
- try {
171
- const bytes = encoder.encode(formatter(record));
172
- await writer.ready;
173
- await writer.write(bytes);
174
- } catch {
175
- // Silently ignore errors in non-blocking mode to avoid disrupting the application
176
- }
177
- }
178
- }
179
-
180
- function scheduleFlush(): void {
181
- if (activeFlush) return;
182
-
183
- activeFlush = flush().finally(() => {
184
- activeFlush = null;
185
- });
186
- }
187
-
188
- function startFlushTimer(): void {
189
- if (flushTimer !== null || disposed) return;
190
-
191
- flushTimer = setInterval(() => {
192
- scheduleFlush();
193
- }, flushInterval);
194
- }
195
-
196
- const nonBlockingSink: Sink & AsyncDisposable = (record: LogRecord) => {
197
- if (disposed) return;
198
-
199
- // Buffer overflow protection: drop oldest records if buffer is too large
200
- if (buffer.length >= maxBufferSize) {
201
- buffer.shift(); // Remove oldest record
202
- }
203
-
204
- buffer.push(record);
205
-
206
- if (buffer.length >= bufferSize) {
207
- scheduleFlush();
208
- } else if (flushTimer === null) {
209
- startFlushTimer();
210
- }
211
- };
212
-
213
- nonBlockingSink[Symbol.asyncDispose] = async () => {
214
- disposed = true;
215
- if (flushTimer !== null) {
216
- clearInterval(flushTimer);
217
- flushTimer = null;
218
- }
219
- await flush();
220
- try {
221
- await writer.close();
222
- } catch {
223
- // Writer might already be closed or errored
224
- }
225
- };
226
-
227
- return nonBlockingSink;
228
- }
229
-
230
- type ConsoleMethod = "debug" | "info" | "log" | "warn" | "error";
231
-
232
- /**
233
- * Options for the {@link getConsoleSink} function.
234
- */
235
- export interface ConsoleSinkOptions {
236
- /**
237
- * The console formatter or text formatter to use.
238
- * Defaults to {@link defaultConsoleFormatter}.
239
- */
240
- formatter?: ConsoleFormatter | TextFormatter;
241
-
242
- /**
243
- * The mapping from log levels to console methods. Defaults to:
244
- *
245
- * ```typescript
246
- * {
247
- * trace: "trace",
248
- * debug: "debug",
249
- * info: "info",
250
- * warning: "warn",
251
- * error: "error",
252
- * fatal: "error",
253
- * }
254
- * ```
255
- * @since 0.9.0
256
- */
257
- levelMap?: Record<LogLevel, ConsoleMethod>;
258
-
259
- /**
260
- * The console to log to. Defaults to {@link console}.
261
- */
262
- console?: Console;
263
-
264
- /**
265
- * Enable non-blocking mode with optional buffer configuration.
266
- * When enabled, log records are buffered and flushed in the background.
267
- *
268
- * @example Simple non-blocking mode
269
- * ```typescript
270
- * getConsoleSink({ nonBlocking: true });
271
- * ```
272
- *
273
- * @example Custom buffer configuration
274
- * ```typescript
275
- * getConsoleSink({
276
- * nonBlocking: {
277
- * bufferSize: 1000,
278
- * flushInterval: 50
279
- * }
280
- * });
281
- * ```
282
- *
283
- * @default `false`
284
- * @since 1.0.0
285
- */
286
- nonBlocking?: boolean | {
287
- /**
288
- * Maximum number of records to buffer before flushing.
289
- * @default `100`
290
- */
291
- bufferSize?: number;
292
-
293
- /**
294
- * Interval in milliseconds between automatic flushes.
295
- * @default `100`
296
- */
297
- flushInterval?: number;
298
- };
299
- }
300
-
301
- /**
302
- * A console sink factory that returns a sink that logs to the console.
303
- *
304
- * @param options The options for the sink.
305
- * @returns A sink that logs to the console. If `nonBlocking` is enabled,
306
- * returns a sink that also implements {@link Disposable}.
307
- */
308
- export function getConsoleSink(
309
- options: ConsoleSinkOptions = {},
310
- ): Sink | (Sink & Disposable) {
311
- const formatter = options.formatter ?? defaultConsoleFormatter;
312
- const levelMap: Record<LogLevel, ConsoleMethod> = {
313
- trace: "debug",
314
- debug: "debug",
315
- info: "info",
316
- warning: "warn",
317
- error: "error",
318
- fatal: "error",
319
- ...(options.levelMap ?? {}),
320
- };
321
- const console = options.console ?? globalThis.console;
322
-
323
- const baseSink = (record: LogRecord) => {
324
- const args = formatter(record);
325
- const method = levelMap[record.level];
326
- if (method === undefined) {
327
- throw new TypeError(`Invalid log level: ${record.level}.`);
328
- }
329
- if (typeof args === "string") {
330
- const msg = args.replace(/\r?\n$/, "");
331
- console[method](msg);
332
- } else {
333
- console[method](...args);
334
- }
335
- };
336
-
337
- if (!options.nonBlocking) {
338
- return baseSink;
339
- }
340
-
341
- // Non-blocking mode implementation
342
- const nonBlockingConfig = options.nonBlocking === true
343
- ? {}
344
- : options.nonBlocking;
345
- const bufferSize = nonBlockingConfig.bufferSize ?? 100;
346
- const flushInterval = nonBlockingConfig.flushInterval ?? 100;
347
-
348
- const buffer: LogRecord[] = [];
349
- let flushTimer: ReturnType<typeof setInterval> | null = null;
350
- let disposed = false;
351
- let flushScheduled = false;
352
- const maxBufferSize = bufferSize * 2; // Overflow protection
353
-
354
- function flush(): void {
355
- if (buffer.length === 0) return;
356
-
357
- const records = buffer.splice(0);
358
- for (const record of records) {
359
- try {
360
- baseSink(record);
361
- } catch {
362
- // Silently ignore errors in non-blocking mode to avoid disrupting the application
363
- }
364
- }
365
- }
366
-
367
- function scheduleFlush(): void {
368
- if (flushScheduled) return;
369
-
370
- flushScheduled = true;
371
- setTimeout(() => {
372
- flushScheduled = false;
373
- flush();
374
- }, 0);
375
- }
376
-
377
- function startFlushTimer(): void {
378
- if (flushTimer !== null || disposed) return;
379
-
380
- flushTimer = setInterval(() => {
381
- flush();
382
- }, flushInterval);
383
- }
384
-
385
- const nonBlockingSink: Sink & Disposable = (record: LogRecord) => {
386
- if (disposed) return;
387
-
388
- // Buffer overflow protection: drop oldest records if buffer is too large
389
- if (buffer.length >= maxBufferSize) {
390
- buffer.shift(); // Remove oldest record
391
- }
392
-
393
- buffer.push(record);
394
-
395
- if (buffer.length >= bufferSize) {
396
- scheduleFlush();
397
- } else if (flushTimer === null) {
398
- startFlushTimer();
399
- }
400
- };
401
-
402
- nonBlockingSink[Symbol.dispose] = () => {
403
- disposed = true;
404
- if (flushTimer !== null) {
405
- clearInterval(flushTimer);
406
- flushTimer = null;
407
- }
408
- flush();
409
- };
410
-
411
- return nonBlockingSink;
412
- }
413
-
414
- /**
415
- * Converts an async sink into a regular sink with proper async handling.
416
- * The returned sink chains async operations to ensure proper ordering and
417
- * implements AsyncDisposable to wait for all pending operations on disposal.
418
- *
419
- * @example Create a sink that asynchronously posts to a webhook
420
- * ```typescript
421
- * const asyncSink: AsyncSink = async (record) => {
422
- * await fetch("https://example.com/logs", {
423
- * method: "POST",
424
- * body: JSON.stringify(record),
425
- * });
426
- * };
427
- * const sink = fromAsyncSink(asyncSink);
428
- * ```
429
- *
430
- * @param asyncSink The async sink function to convert.
431
- * @returns A sink that properly handles async operations and disposal.
432
- * @since 1.0.0
433
- */
434
- export function fromAsyncSink(asyncSink: AsyncSink): Sink & AsyncDisposable {
435
- let lastPromise = Promise.resolve();
436
- const sink: Sink & AsyncDisposable = (record: LogRecord) => {
437
- lastPromise = lastPromise
438
- .then(() => asyncSink(record))
439
- .catch(() => {
440
- // Errors are handled by the sink infrastructure
441
- });
442
- };
443
- sink[Symbol.asyncDispose] = async () => {
444
- await lastPromise;
445
- };
446
- return sink;
447
- }
448
-
449
- /**
450
- * Options for the {@link fingersCrossed} function.
451
- * @since 1.1.0
452
- */
453
- export interface FingersCrossedOptions {
454
- /**
455
- * Minimum log level that triggers buffer flush.
456
- * When a log record at or above this level is received, all buffered
457
- * records are flushed to the wrapped sink.
458
- * @default `"error"`
459
- */
460
- readonly triggerLevel?: LogLevel;
461
-
462
- /**
463
- * Maximum buffer size before oldest records are dropped.
464
- * When the buffer exceeds this size, the oldest records are removed
465
- * to prevent unbounded memory growth.
466
- * @default `1000`
467
- */
468
- readonly maxBufferSize?: number;
469
-
470
- /**
471
- * Category isolation mode or custom matcher function.
472
- *
473
- * When `undefined` (default), all log records share a single buffer.
474
- *
475
- * When set to a mode string:
476
- *
477
- * - `"descendant"`: Flush child category buffers when parent triggers
478
- * - `"ancestor"`: Flush parent category buffers when child triggers
479
- * - `"both"`: Flush both parent and child category buffers
480
- *
481
- * When set to a function, it receives the trigger category and buffered
482
- * category and should return true if the buffered category should be flushed.
483
- *
484
- * @default `undefined` (no isolation, single global buffer)
485
- */
486
- readonly isolateByCategory?:
487
- | "descendant"
488
- | "ancestor"
489
- | "both"
490
- | ((
491
- triggerCategory: readonly string[],
492
- bufferedCategory: readonly string[],
493
- ) => boolean);
494
-
495
- /**
496
- * Enable context-based buffer isolation.
497
- * When enabled, buffers are isolated based on specified context keys.
498
- * This is useful for scenarios like HTTP request tracing where logs
499
- * should be isolated per request.
500
- *
501
- * @example
502
- * ```typescript
503
- * fingersCrossed(sink, {
504
- * isolateByContext: { keys: ['requestId'] }
505
- * })
506
- * ```
507
- *
508
- * @example Combined with category isolation
509
- * ```typescript
510
- * fingersCrossed(sink, {
511
- * isolateByCategory: 'descendant',
512
- * isolateByContext: { keys: ['requestId', 'sessionId'] }
513
- * })
514
- * ```
515
- *
516
- * @example With TTL-based buffer cleanup
517
- * ```typescript
518
- * fingersCrossed(sink, {
519
- * isolateByContext: {
520
- * keys: ['requestId'],
521
- * bufferTtlMs: 30000, // 30 seconds
522
- * cleanupIntervalMs: 10000 // cleanup every 10 seconds
523
- * }
524
- * })
525
- * ```
526
- *
527
- * @default `undefined` (no context isolation)
528
- * @since 1.2.0
529
- */
530
- readonly isolateByContext?: {
531
- /**
532
- * Context keys to use for isolation.
533
- * Buffers will be separate for different combinations of these context values.
534
- */
535
- readonly keys: readonly string[];
536
-
537
- /**
538
- * Maximum number of context buffers to maintain simultaneously.
539
- * When this limit is exceeded, the least recently used (LRU) buffers
540
- * will be evicted to make room for new ones.
541
- *
542
- * This provides memory protection in high-concurrency scenarios where
543
- * many different context values might be active simultaneously.
544
- *
545
- * When set to 0 or undefined, no limit is enforced.
546
- *
547
- * @default `undefined` (no limit)
548
- * @since 1.2.0
549
- */
550
- readonly maxContexts?: number;
551
-
552
- /**
553
- * Time-to-live for context buffers in milliseconds.
554
- * Buffers that haven't been accessed for this duration will be automatically
555
- * cleaned up to prevent memory leaks in long-running applications.
556
- *
557
- * When set to 0 or undefined, buffers will never expire based on time.
558
- *
559
- * @default `undefined` (no TTL)
560
- * @since 1.2.0
561
- */
562
- readonly bufferTtlMs?: number;
563
-
564
- /**
565
- * Interval in milliseconds for running cleanup operations.
566
- * The cleanup process removes expired buffers based on {@link bufferTtlMs}.
567
- *
568
- * This option is ignored if {@link bufferTtlMs} is not set.
569
- *
570
- * @default `30000` (30 seconds)
571
- * @since 1.2.0
572
- */
573
- readonly cleanupIntervalMs?: number;
574
- };
575
- }
576
-
577
- /**
578
- * Metadata for context-based buffer tracking.
579
- * Used internally by {@link fingersCrossed} to manage buffer lifecycle with LRU support.
580
- * @since 1.2.0
581
- */
582
- interface BufferMetadata {
583
- /**
584
- * The actual log records buffer.
585
- */
586
- readonly buffer: LogRecord[];
587
-
588
- /**
589
- * Timestamp of the last access to this buffer (in milliseconds).
590
- * Used for LRU-based eviction when {@link FingersCrossedOptions.isolateByContext.maxContexts} is set.
591
- */
592
- lastAccess: number;
593
- }
594
-
595
- /**
596
- * Creates a sink that buffers log records until a trigger level is reached.
597
- * This pattern, known as "fingers crossed" logging, keeps detailed debug logs
598
- * in memory and only outputs them when an error or other significant event occurs.
599
- *
600
- * @example Basic usage with default settings
601
- * ```typescript
602
- * const sink = fingersCrossed(getConsoleSink());
603
- * // Debug and info logs are buffered
604
- * // When an error occurs, all buffered logs + the error are output
605
- * ```
606
- *
607
- * @example Custom trigger level and buffer size
608
- * ```typescript
609
- * const sink = fingersCrossed(getConsoleSink(), {
610
- * triggerLevel: "warning", // Trigger on warning or higher
611
- * maxBufferSize: 500 // Keep last 500 records
612
- * });
613
- * ```
614
- *
615
- * @example Category isolation
616
- * ```typescript
617
- * const sink = fingersCrossed(getConsoleSink(), {
618
- * isolateByCategory: "descendant" // Separate buffers per category
619
- * });
620
- * // Error in ["app"] triggers flush of ["app"] and ["app", "module"] buffers
621
- * // But not ["other"] buffer
622
- * ```
623
- *
624
- * @param sink The sink to wrap. Buffered records are sent to this sink when
625
- * triggered.
626
- * @param options Configuration options for the fingers crossed behavior.
627
- * @returns A sink that buffers records until the trigger level is reached.
628
- * @since 1.1.0
629
- */
630
- export function fingersCrossed(
631
- sink: Sink,
632
- options: FingersCrossedOptions = {},
633
- ): Sink | (Sink & Disposable) {
634
- const triggerLevel = options.triggerLevel ?? "error";
635
- const maxBufferSize = Math.max(0, options.maxBufferSize ?? 1000);
636
- const isolateByCategory = options.isolateByCategory;
637
- const isolateByContext = options.isolateByContext;
638
-
639
- // TTL and LRU configuration
640
- const bufferTtlMs = isolateByContext?.bufferTtlMs;
641
- const cleanupIntervalMs = isolateByContext?.cleanupIntervalMs ?? 30000;
642
- const maxContexts = isolateByContext?.maxContexts;
643
- const hasTtl = bufferTtlMs != null && bufferTtlMs > 0;
644
- const hasLru = maxContexts != null && maxContexts > 0;
645
-
646
- // Validate trigger level early
647
- try {
648
- compareLogLevel("trace", triggerLevel); // Test with any valid level
649
- } catch (error) {
650
- throw new TypeError(
651
- `Invalid triggerLevel: ${JSON.stringify(triggerLevel)}. ${
652
- error instanceof Error ? error.message : String(error)
653
- }`,
654
- );
655
- }
656
-
657
- // Helper functions for category matching
658
- function isDescendant(
659
- parent: readonly string[],
660
- child: readonly string[],
661
- ): boolean {
662
- if (parent.length === 0 || child.length === 0) return false; // Empty categories are isolated
663
- if (parent.length > child.length) return false;
664
- return parent.every((p, i) => p === child[i]);
665
- }
666
-
667
- function isAncestor(
668
- child: readonly string[],
669
- parent: readonly string[],
670
- ): boolean {
671
- if (child.length === 0 || parent.length === 0) return false; // Empty categories are isolated
672
- if (child.length < parent.length) return false;
673
- return parent.every((p, i) => p === child[i]);
674
- }
675
-
676
- // Determine matcher function based on isolation mode
677
- let shouldFlushBuffer:
678
- | ((
679
- triggerCategory: readonly string[],
680
- bufferedCategory: readonly string[],
681
- ) => boolean)
682
- | null = null;
683
-
684
- if (isolateByCategory) {
685
- if (typeof isolateByCategory === "function") {
686
- shouldFlushBuffer = isolateByCategory;
687
- } else {
688
- switch (isolateByCategory) {
689
- case "descendant":
690
- shouldFlushBuffer = (trigger, buffered) =>
691
- isDescendant(trigger, buffered);
692
- break;
693
- case "ancestor":
694
- shouldFlushBuffer = (trigger, buffered) =>
695
- isAncestor(trigger, buffered);
696
- break;
697
- case "both":
698
- shouldFlushBuffer = (trigger, buffered) =>
699
- isDescendant(trigger, buffered) || isAncestor(trigger, buffered);
700
- break;
701
- }
702
- }
703
- }
704
-
705
- // Helper functions for category serialization
706
- function getCategoryKey(category: readonly string[]): string {
707
- return JSON.stringify(category);
708
- }
709
-
710
- function parseCategoryKey(key: string): string[] {
711
- return JSON.parse(key);
712
- }
713
-
714
- // Helper function to extract context values from properties
715
- function getContextKey(properties: Record<string, unknown>): string {
716
- if (!isolateByContext || isolateByContext.keys.length === 0) {
717
- return "";
718
- }
719
- const contextValues: Record<string, unknown> = {};
720
- for (const key of isolateByContext.keys) {
721
- if (key in properties) {
722
- contextValues[key] = properties[key];
723
- }
724
- }
725
- return JSON.stringify(contextValues);
726
- }
727
-
728
- // Helper function to generate buffer key
729
- function getBufferKey(
730
- category: readonly string[],
731
- properties: Record<string, unknown>,
732
- ): string {
733
- const categoryKey = getCategoryKey(category);
734
- if (!isolateByContext) {
735
- return categoryKey;
736
- }
737
- const contextKey = getContextKey(properties);
738
- return `${categoryKey}:${contextKey}`;
739
- }
740
-
741
- // Helper function to parse buffer key
742
- function parseBufferKey(key: string): {
743
- category: string[];
744
- context: string;
745
- } {
746
- if (!isolateByContext) {
747
- return { category: parseCategoryKey(key), context: "" };
748
- }
749
- // Find the separator between category and context
750
- // The category part is JSON-encoded, so we need to find where it ends
751
- // We look for "]:" which indicates end of category array and start of context
752
- const separatorIndex = key.indexOf("]:");
753
- if (separatorIndex === -1) {
754
- // No context part, entire key is category
755
- return { category: parseCategoryKey(key), context: "" };
756
- }
757
- const categoryPart = key.substring(0, separatorIndex + 1); // Include the ]
758
- const contextPart = key.substring(separatorIndex + 2); // Skip ]:
759
- return { category: parseCategoryKey(categoryPart), context: contextPart };
760
- }
761
-
762
- // TTL-based cleanup function
763
- function cleanupExpiredBuffers(buffers: Map<string, BufferMetadata>): void {
764
- if (!hasTtl) return;
765
-
766
- const now = Date.now();
767
- const expiredKeys: string[] = [];
768
-
769
- for (const [key, metadata] of buffers) {
770
- if (metadata.buffer.length === 0) continue;
771
-
772
- // Use the timestamp of the last (most recent) record in the buffer
773
- const lastRecordTimestamp =
774
- metadata.buffer[metadata.buffer.length - 1].timestamp;
775
- if (now - lastRecordTimestamp > bufferTtlMs!) {
776
- expiredKeys.push(key);
777
- }
778
- }
779
-
780
- // Remove expired buffers
781
- for (const key of expiredKeys) {
782
- buffers.delete(key);
783
- }
784
- }
785
-
786
- // LRU-based eviction function
787
- function evictLruBuffers(
788
- buffers: Map<string, BufferMetadata>,
789
- numToEvict?: number,
790
- ): void {
791
- if (!hasLru) return;
792
-
793
- // Use provided numToEvict or calculate based on current size vs limit
794
- const toEvict = numToEvict ?? Math.max(0, buffers.size - maxContexts!);
795
- if (toEvict <= 0) return;
796
-
797
- // Sort by lastAccess timestamp (oldest first)
798
- const sortedEntries = Array.from(buffers.entries())
799
- .sort(([, a], [, b]) => a.lastAccess - b.lastAccess);
800
-
801
- // Remove the oldest buffers
802
- for (let i = 0; i < toEvict; i++) {
803
- const [key] = sortedEntries[i];
804
- buffers.delete(key);
805
- }
806
- }
807
-
808
- // Buffer management
809
- if (!isolateByCategory && !isolateByContext) {
810
- // Single global buffer
811
- const buffer: LogRecord[] = [];
812
- let triggered = false;
813
-
814
- return (record: LogRecord) => {
815
- if (triggered) {
816
- // Already triggered, pass through directly
817
- sink(record);
818
- return;
819
- }
820
-
821
- // Check if this record triggers flush
822
- if (compareLogLevel(record.level, triggerLevel) >= 0) {
823
- triggered = true;
824
-
825
- // Flush buffer
826
- for (const bufferedRecord of buffer) {
827
- sink(bufferedRecord);
828
- }
829
- buffer.length = 0;
830
-
831
- // Send trigger record
832
- sink(record);
833
- } else {
834
- // Buffer the record
835
- buffer.push(record);
836
-
837
- // Enforce max buffer size
838
- while (buffer.length > maxBufferSize) {
839
- buffer.shift();
840
- }
841
- }
842
- };
843
- } else {
844
- // Category and/or context-isolated buffers
845
- const buffers = new Map<string, BufferMetadata>();
846
- const triggered = new Set<string>();
847
-
848
- // Set up TTL cleanup timer if enabled
849
- let cleanupTimer: ReturnType<typeof setInterval> | null = null;
850
- if (hasTtl) {
851
- cleanupTimer = setInterval(() => {
852
- cleanupExpiredBuffers(buffers);
853
- }, cleanupIntervalMs);
854
- }
855
-
856
- const fingersCrossedSink = (record: LogRecord) => {
857
- const bufferKey = getBufferKey(record.category, record.properties);
858
-
859
- // Check if this buffer is already triggered
860
- if (triggered.has(bufferKey)) {
861
- sink(record);
862
- return;
863
- }
864
-
865
- // Check if this record triggers flush
866
- if (compareLogLevel(record.level, triggerLevel) >= 0) {
867
- // Find all buffers that should be flushed
868
- const keysToFlush = new Set<string>();
869
-
870
- for (const [bufferedKey] of buffers) {
871
- if (bufferedKey === bufferKey) {
872
- keysToFlush.add(bufferedKey);
873
- } else {
874
- const { category: bufferedCategory, context: bufferedContext } =
875
- parseBufferKey(bufferedKey);
876
- const { context: triggerContext } = parseBufferKey(bufferKey);
877
-
878
- // Check context match
879
- let contextMatches = true;
880
- if (isolateByContext) {
881
- contextMatches = bufferedContext === triggerContext;
882
- }
883
-
884
- // Check category match
885
- let categoryMatches = false;
886
- if (!isolateByCategory) {
887
- // No category isolation, so all categories match if context matches
888
- categoryMatches = contextMatches;
889
- } else if (shouldFlushBuffer) {
890
- try {
891
- categoryMatches = shouldFlushBuffer(
892
- record.category,
893
- bufferedCategory,
894
- );
895
- } catch {
896
- // Ignore errors from custom matcher
897
- }
898
- } else {
899
- // Same category only
900
- categoryMatches = getCategoryKey(record.category) ===
901
- getCategoryKey(bufferedCategory);
902
- }
903
-
904
- // Both must match for the buffer to be flushed
905
- if (contextMatches && categoryMatches) {
906
- keysToFlush.add(bufferedKey);
907
- }
908
- }
909
- }
910
-
911
- // Flush matching buffers
912
- const allRecordsToFlush: LogRecord[] = [];
913
- for (const key of keysToFlush) {
914
- const metadata = buffers.get(key);
915
- if (metadata) {
916
- allRecordsToFlush.push(...metadata.buffer);
917
- buffers.delete(key);
918
- triggered.add(key);
919
- }
920
- }
921
-
922
- // Sort by timestamp to maintain chronological order
923
- allRecordsToFlush.sort((a, b) => a.timestamp - b.timestamp);
924
-
925
- // Flush all records
926
- for (const bufferedRecord of allRecordsToFlush) {
927
- sink(bufferedRecord);
928
- }
929
-
930
- // Mark trigger buffer as triggered and send trigger record
931
- triggered.add(bufferKey);
932
- sink(record);
933
- } else {
934
- // Buffer the record
935
- const now = Date.now();
936
- let metadata = buffers.get(bufferKey);
937
- if (!metadata) {
938
- // Apply LRU eviction if adding new buffer would exceed capacity
939
- if (hasLru && buffers.size >= maxContexts!) {
940
- // Calculate how many buffers to evict to make room for the new one
941
- const numToEvict = buffers.size - maxContexts! + 1;
942
- evictLruBuffers(buffers, numToEvict);
943
- }
944
-
945
- metadata = {
946
- buffer: [],
947
- lastAccess: now,
948
- };
949
- buffers.set(bufferKey, metadata);
950
- } else {
951
- // Update last access time for LRU
952
- metadata.lastAccess = now;
953
- }
954
-
955
- metadata.buffer.push(record);
956
-
957
- // Enforce max buffer size per buffer
958
- while (metadata.buffer.length > maxBufferSize) {
959
- metadata.buffer.shift();
960
- }
961
- }
962
- };
963
-
964
- // Add disposal functionality to clean up timer
965
- if (cleanupTimer !== null) {
966
- (fingersCrossedSink as Sink & Disposable)[Symbol.dispose] = () => {
967
- if (cleanupTimer !== null) {
968
- clearInterval(cleanupTimer);
969
- cleanupTimer = null;
970
- }
971
- };
972
- }
973
-
974
- return fingersCrossedSink;
975
- }
976
- }