@logtape/logtape 1.0.0-dev.207 → 1.0.0-dev.209
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/deno.json +1 -1
- package/dist/mod.cjs +2 -0
- package/dist/mod.d.cts +2 -2
- package/dist/mod.d.ts +2 -2
- package/dist/mod.js +2 -2
- package/dist/sink.cjs +94 -0
- package/dist/sink.d.cts +70 -1
- package/dist/sink.d.cts.map +1 -1
- package/dist/sink.d.ts +70 -1
- package/dist/sink.d.ts.map +1 -1
- package/dist/sink.js +93 -1
- package/dist/sink.js.map +1 -1
- package/mod.ts +4 -0
- package/package.json +1 -1
- package/sink.test.ts +591 -1
- package/sink.ts +155 -0
package/deno.json
CHANGED
package/dist/mod.cjs
CHANGED
|
@@ -15,6 +15,7 @@ exports.defaultConsoleFormatter = require_formatter.defaultConsoleFormatter;
|
|
|
15
15
|
exports.defaultTextFormatter = require_formatter.defaultTextFormatter;
|
|
16
16
|
exports.dispose = require_config.dispose;
|
|
17
17
|
exports.disposeSync = require_config.disposeSync;
|
|
18
|
+
exports.fromAsyncSink = require_sink.fromAsyncSink;
|
|
18
19
|
exports.getAnsiColorFormatter = require_formatter.getAnsiColorFormatter;
|
|
19
20
|
exports.getConfig = require_config.getConfig;
|
|
20
21
|
exports.getConsoleSink = require_sink.getConsoleSink;
|
|
@@ -30,5 +31,6 @@ exports.parseLogLevel = require_level.parseLogLevel;
|
|
|
30
31
|
exports.reset = require_config.reset;
|
|
31
32
|
exports.resetSync = require_config.resetSync;
|
|
32
33
|
exports.toFilter = require_filter.toFilter;
|
|
34
|
+
exports.withBuffer = require_sink.withBuffer;
|
|
33
35
|
exports.withContext = require_context.withContext;
|
|
34
36
|
exports.withFilter = require_sink.withFilter;
|
package/dist/mod.d.cts
CHANGED
|
@@ -3,7 +3,7 @@ import { LogLevel, compareLogLevel, getLogLevels, isLogLevel, parseLogLevel } fr
|
|
|
3
3
|
import { LogRecord } from "./record.cjs";
|
|
4
4
|
import { Filter, FilterLike, getLevelFilter, toFilter } from "./filter.cjs";
|
|
5
5
|
import { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, ConsoleFormatter, FormattedValues, JsonLinesFormatterOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, defaultConsoleFormatter, defaultTextFormatter, getAnsiColorFormatter, getJsonLinesFormatter, getTextFormatter, jsonLinesFormatter } from "./formatter.cjs";
|
|
6
|
-
import { ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withFilter } from "./sink.cjs";
|
|
6
|
+
import { AsyncSink, BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter } from "./sink.cjs";
|
|
7
7
|
import { Config, ConfigError, LoggerConfig, configure, configureSync, dispose, disposeSync, getConfig, reset, resetSync } from "./config.cjs";
|
|
8
8
|
import { LogMethod, Logger, getLogger } from "./logger.cjs";
|
|
9
|
-
export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withContext, withFilter };
|
|
9
|
+
export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, AsyncSink, BufferSinkOptions, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, fromAsyncSink, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withBuffer, withContext, withFilter };
|
package/dist/mod.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { LogLevel, compareLogLevel, getLogLevels, isLogLevel, parseLogLevel } fr
|
|
|
3
3
|
import { LogRecord } from "./record.js";
|
|
4
4
|
import { Filter, FilterLike, getLevelFilter, toFilter } from "./filter.js";
|
|
5
5
|
import { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, ConsoleFormatter, FormattedValues, JsonLinesFormatterOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, defaultConsoleFormatter, defaultTextFormatter, getAnsiColorFormatter, getJsonLinesFormatter, getTextFormatter, jsonLinesFormatter } from "./formatter.js";
|
|
6
|
-
import { ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withFilter } from "./sink.js";
|
|
6
|
+
import { AsyncSink, BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter } from "./sink.js";
|
|
7
7
|
import { Config, ConfigError, LoggerConfig, configure, configureSync, dispose, disposeSync, getConfig, reset, resetSync } from "./config.js";
|
|
8
8
|
import { LogMethod, Logger, getLogger } from "./logger.js";
|
|
9
|
-
export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withContext, withFilter };
|
|
9
|
+
export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, AsyncSink, BufferSinkOptions, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, fromAsyncSink, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withBuffer, withContext, withFilter };
|
package/dist/mod.js
CHANGED
|
@@ -2,8 +2,8 @@ import { getLevelFilter, toFilter } from "./filter.js";
|
|
|
2
2
|
import { compareLogLevel, getLogLevels, isLogLevel, parseLogLevel } from "./level.js";
|
|
3
3
|
import { getLogger } from "./logger.js";
|
|
4
4
|
import { ansiColorFormatter, defaultConsoleFormatter, defaultTextFormatter, getAnsiColorFormatter, getJsonLinesFormatter, getTextFormatter, jsonLinesFormatter } from "./formatter.js";
|
|
5
|
-
import { getConsoleSink, getStreamSink, withFilter } from "./sink.js";
|
|
5
|
+
import { fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter } from "./sink.js";
|
|
6
6
|
import { ConfigError, configure, configureSync, dispose, disposeSync, getConfig, reset, resetSync } from "./config.js";
|
|
7
7
|
import { withContext } from "./context.js";
|
|
8
8
|
|
|
9
|
-
export { ConfigError, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withContext, withFilter };
|
|
9
|
+
export { ConfigError, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, fromAsyncSink, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withBuffer, withContext, withFilter };
|
package/dist/sink.cjs
CHANGED
|
@@ -23,6 +23,68 @@ function withFilter(sink, filter) {
|
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
+
* Turns a sink into a buffered sink. The returned sink buffers log records
|
|
27
|
+
* in memory and flushes them to the underlying sink when the buffer is full
|
|
28
|
+
* or after a specified time interval.
|
|
29
|
+
*
|
|
30
|
+
* @example Buffer a console sink with custom options
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const sink = withBuffer(getConsoleSink(), {
|
|
33
|
+
* bufferSize: 5,
|
|
34
|
+
* flushInterval: 1000
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @param sink A sink to be buffered.
|
|
39
|
+
* @param options Options for the buffered sink.
|
|
40
|
+
* @returns A buffered sink that flushes records periodically.
|
|
41
|
+
* @since 1.0.0
|
|
42
|
+
*/
|
|
43
|
+
function withBuffer(sink, options = {}) {
|
|
44
|
+
const bufferSize = options.bufferSize ?? 10;
|
|
45
|
+
const flushInterval = options.flushInterval ?? 5e3;
|
|
46
|
+
const buffer = [];
|
|
47
|
+
let flushTimer = null;
|
|
48
|
+
let disposed = false;
|
|
49
|
+
function flush() {
|
|
50
|
+
if (buffer.length === 0) return;
|
|
51
|
+
const records = buffer.splice(0);
|
|
52
|
+
for (const record of records) try {
|
|
53
|
+
sink(record);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
if (flushTimer !== null) {
|
|
58
|
+
clearTimeout(flushTimer);
|
|
59
|
+
flushTimer = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function scheduleFlush() {
|
|
63
|
+
if (flushInterval <= 0 || flushTimer !== null || disposed) return;
|
|
64
|
+
flushTimer = setTimeout(() => {
|
|
65
|
+
flushTimer = null;
|
|
66
|
+
flush();
|
|
67
|
+
}, flushInterval);
|
|
68
|
+
}
|
|
69
|
+
const bufferedSink = (record) => {
|
|
70
|
+
if (disposed) return;
|
|
71
|
+
buffer.push(record);
|
|
72
|
+
if (buffer.length >= bufferSize) flush();
|
|
73
|
+
else scheduleFlush();
|
|
74
|
+
};
|
|
75
|
+
bufferedSink[Symbol.asyncDispose] = async () => {
|
|
76
|
+
disposed = true;
|
|
77
|
+
if (flushTimer !== null) {
|
|
78
|
+
clearTimeout(flushTimer);
|
|
79
|
+
flushTimer = null;
|
|
80
|
+
}
|
|
81
|
+
flush();
|
|
82
|
+
if (Symbol.asyncDispose in sink) await sink[Symbol.asyncDispose]();
|
|
83
|
+
else if (Symbol.dispose in sink) sink[Symbol.dispose]();
|
|
84
|
+
};
|
|
85
|
+
return bufferedSink;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
26
88
|
* A factory that returns a sink that writes to a {@link WritableStream}.
|
|
27
89
|
*
|
|
28
90
|
* Note that the `stream` is of Web Streams API, which is different from
|
|
@@ -89,8 +151,40 @@ function getConsoleSink(options = {}) {
|
|
|
89
151
|
} else console[method](...args);
|
|
90
152
|
};
|
|
91
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Converts an async sink into a regular sink with proper async handling.
|
|
156
|
+
* The returned sink chains async operations to ensure proper ordering and
|
|
157
|
+
* implements AsyncDisposable to wait for all pending operations on disposal.
|
|
158
|
+
*
|
|
159
|
+
* @example Create a sink that asynchronously posts to a webhook
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const asyncSink: AsyncSink = async (record) => {
|
|
162
|
+
* await fetch("https://example.com/logs", {
|
|
163
|
+
* method: "POST",
|
|
164
|
+
* body: JSON.stringify(record),
|
|
165
|
+
* });
|
|
166
|
+
* };
|
|
167
|
+
* const sink = fromAsyncSink(asyncSink);
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @param asyncSink The async sink function to convert.
|
|
171
|
+
* @returns A sink that properly handles async operations and disposal.
|
|
172
|
+
* @since 1.0.0
|
|
173
|
+
*/
|
|
174
|
+
function fromAsyncSink(asyncSink) {
|
|
175
|
+
let lastPromise = Promise.resolve();
|
|
176
|
+
const sink = (record) => {
|
|
177
|
+
lastPromise = lastPromise.then(() => asyncSink(record)).catch(() => {});
|
|
178
|
+
};
|
|
179
|
+
sink[Symbol.asyncDispose] = async () => {
|
|
180
|
+
await lastPromise;
|
|
181
|
+
};
|
|
182
|
+
return sink;
|
|
183
|
+
}
|
|
92
184
|
|
|
93
185
|
//#endregion
|
|
186
|
+
exports.fromAsyncSink = fromAsyncSink;
|
|
94
187
|
exports.getConsoleSink = getConsoleSink;
|
|
95
188
|
exports.getStreamSink = getStreamSink;
|
|
189
|
+
exports.withBuffer = withBuffer;
|
|
96
190
|
exports.withFilter = withFilter;
|
package/dist/sink.d.cts
CHANGED
|
@@ -15,6 +15,16 @@ import { ConsoleFormatter, TextFormatter } from "./formatter.cjs";
|
|
|
15
15
|
* @param record The log record to sink.
|
|
16
16
|
*/
|
|
17
17
|
type Sink = (record: LogRecord) => void;
|
|
18
|
+
/**
|
|
19
|
+
* An async sink is a function that accepts a log record and asynchronously
|
|
20
|
+
* processes it. This type is used with {@link fromAsyncSink} to create
|
|
21
|
+
* a regular sink that properly handles asynchronous operations.
|
|
22
|
+
*
|
|
23
|
+
* @param record The log record to process asynchronously.
|
|
24
|
+
* @returns A promise that resolves when the record has been processed.
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
type AsyncSink = (record: LogRecord) => Promise<void>;
|
|
18
28
|
/**
|
|
19
29
|
* Turns a sink into a filtered sink. The returned sink only logs records that
|
|
20
30
|
* pass the filter.
|
|
@@ -30,6 +40,44 @@ type Sink = (record: LogRecord) => void;
|
|
|
30
40
|
* @returns A sink that only logs records that pass the filter.
|
|
31
41
|
*/
|
|
32
42
|
declare function withFilter(sink: Sink, filter: FilterLike): Sink;
|
|
43
|
+
/**
|
|
44
|
+
* Options for the {@link withBuffer} function.
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
*/
|
|
47
|
+
interface BufferSinkOptions {
|
|
48
|
+
/**
|
|
49
|
+
* The maximum number of log records to buffer before flushing to the
|
|
50
|
+
* underlying sink.
|
|
51
|
+
* @default `10`
|
|
52
|
+
*/
|
|
53
|
+
bufferSize?: number;
|
|
54
|
+
/**
|
|
55
|
+
* The maximum time in milliseconds to wait before flushing buffered records
|
|
56
|
+
* to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
|
|
57
|
+
* negative to disable time-based flushing.
|
|
58
|
+
* @default `5000`
|
|
59
|
+
*/
|
|
60
|
+
flushInterval?: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Turns a sink into a buffered sink. The returned sink buffers log records
|
|
64
|
+
* in memory and flushes them to the underlying sink when the buffer is full
|
|
65
|
+
* or after a specified time interval.
|
|
66
|
+
*
|
|
67
|
+
* @example Buffer a console sink with custom options
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const sink = withBuffer(getConsoleSink(), {
|
|
70
|
+
* bufferSize: 5,
|
|
71
|
+
* flushInterval: 1000
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @param sink A sink to be buffered.
|
|
76
|
+
* @param options Options for the buffered sink.
|
|
77
|
+
* @returns A buffered sink that flushes records periodically.
|
|
78
|
+
* @since 1.0.0
|
|
79
|
+
*/
|
|
80
|
+
declare function withBuffer(sink: Sink, options?: BufferSinkOptions): Sink & AsyncDisposable;
|
|
33
81
|
/**
|
|
34
82
|
* Options for the {@link getStreamSink} function.
|
|
35
83
|
*/
|
|
@@ -108,6 +156,27 @@ interface ConsoleSinkOptions {
|
|
|
108
156
|
* @returns A sink that logs to the console.
|
|
109
157
|
*/
|
|
110
158
|
declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
|
|
159
|
+
/**
|
|
160
|
+
* Converts an async sink into a regular sink with proper async handling.
|
|
161
|
+
* The returned sink chains async operations to ensure proper ordering and
|
|
162
|
+
* implements AsyncDisposable to wait for all pending operations on disposal.
|
|
163
|
+
*
|
|
164
|
+
* @example Create a sink that asynchronously posts to a webhook
|
|
165
|
+
* ```typescript
|
|
166
|
+
* const asyncSink: AsyncSink = async (record) => {
|
|
167
|
+
* await fetch("https://example.com/logs", {
|
|
168
|
+
* method: "POST",
|
|
169
|
+
* body: JSON.stringify(record),
|
|
170
|
+
* });
|
|
171
|
+
* };
|
|
172
|
+
* const sink = fromAsyncSink(asyncSink);
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @param asyncSink The async sink function to convert.
|
|
176
|
+
* @returns A sink that properly handles async operations and disposal.
|
|
177
|
+
* @since 1.0.0
|
|
178
|
+
*/
|
|
179
|
+
declare function fromAsyncSink(asyncSink: AsyncSink): Sink & AsyncDisposable;
|
|
111
180
|
//#endregion
|
|
112
|
-
export { ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withFilter };
|
|
181
|
+
export { AsyncSink, BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
113
182
|
//# sourceMappingURL=sink.d.cts.map
|
package/dist/sink.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sink.d.cts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;
|
|
1
|
+
{"version":3,"file":"sink.d.cts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAWA;;;;AAAsD;AAgBtD;AAA0B,KA3Bd,IAAA,GA2Bc,CAAA,MAAA,EA3BE,SA2BF,EAAA,GAAA,IAAA;;;;AAAsC;AAWhE;AAmCA;;;;AAGG,KAjES,SAAA,GAiET,CAAA,MAAA,EAjE8B,SAiE9B,EAAA,GAjE4C,OAiE5C,CAAA,IAAA,CAAA;;AAAsB;AAsEzB;;;;AAS8C;AA2B9C;;;;;;AAGyB;AAkBpB,iBAhLW,UAAA,CAgLE,IAAA,EAhLe,IAgLf,EAAA,MAAA,EAhL6B,UAgL7B,CAAA,EAhL0C,IAgL1C;AAKlB;;;;AAsBoB,UAhMH,iBAAA,CAgMG;EAAQ;;;AAKT;AASnB;EAA8B,UAAA,CAAA,EAAA,MAAA;EAAA;;AAAwC;AA+CtE;;;EAAkD,aAAG,CAAA,EAAA,MAAA;;AAAsB;;;;;;;;;;;;;;;;;;iBA1N3D,UAAA,OACR,gBACG,oBACR,OAAO;;;;UAsEO,iBAAA;;;;cAIH;;;;;0BAKsB;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2BpB,aAAA,SACN,0BACC,oBACR,OAAO;KAkBL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;iBASI,cAAA,WAAwB,qBAA0B;;;;;;;;;;;;;;;;;;;;;iBA+ClD,aAAA,YAAyB,YAAY,OAAO"}
|
package/dist/sink.d.ts
CHANGED
|
@@ -15,6 +15,16 @@ import { ConsoleFormatter, TextFormatter } from "./formatter.js";
|
|
|
15
15
|
* @param record The log record to sink.
|
|
16
16
|
*/
|
|
17
17
|
type Sink = (record: LogRecord) => void;
|
|
18
|
+
/**
|
|
19
|
+
* An async sink is a function that accepts a log record and asynchronously
|
|
20
|
+
* processes it. This type is used with {@link fromAsyncSink} to create
|
|
21
|
+
* a regular sink that properly handles asynchronous operations.
|
|
22
|
+
*
|
|
23
|
+
* @param record The log record to process asynchronously.
|
|
24
|
+
* @returns A promise that resolves when the record has been processed.
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
type AsyncSink = (record: LogRecord) => Promise<void>;
|
|
18
28
|
/**
|
|
19
29
|
* Turns a sink into a filtered sink. The returned sink only logs records that
|
|
20
30
|
* pass the filter.
|
|
@@ -30,6 +40,44 @@ type Sink = (record: LogRecord) => void;
|
|
|
30
40
|
* @returns A sink that only logs records that pass the filter.
|
|
31
41
|
*/
|
|
32
42
|
declare function withFilter(sink: Sink, filter: FilterLike): Sink;
|
|
43
|
+
/**
|
|
44
|
+
* Options for the {@link withBuffer} function.
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
*/
|
|
47
|
+
interface BufferSinkOptions {
|
|
48
|
+
/**
|
|
49
|
+
* The maximum number of log records to buffer before flushing to the
|
|
50
|
+
* underlying sink.
|
|
51
|
+
* @default `10`
|
|
52
|
+
*/
|
|
53
|
+
bufferSize?: number;
|
|
54
|
+
/**
|
|
55
|
+
* The maximum time in milliseconds to wait before flushing buffered records
|
|
56
|
+
* to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
|
|
57
|
+
* negative to disable time-based flushing.
|
|
58
|
+
* @default `5000`
|
|
59
|
+
*/
|
|
60
|
+
flushInterval?: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Turns a sink into a buffered sink. The returned sink buffers log records
|
|
64
|
+
* in memory and flushes them to the underlying sink when the buffer is full
|
|
65
|
+
* or after a specified time interval.
|
|
66
|
+
*
|
|
67
|
+
* @example Buffer a console sink with custom options
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const sink = withBuffer(getConsoleSink(), {
|
|
70
|
+
* bufferSize: 5,
|
|
71
|
+
* flushInterval: 1000
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @param sink A sink to be buffered.
|
|
76
|
+
* @param options Options for the buffered sink.
|
|
77
|
+
* @returns A buffered sink that flushes records periodically.
|
|
78
|
+
* @since 1.0.0
|
|
79
|
+
*/
|
|
80
|
+
declare function withBuffer(sink: Sink, options?: BufferSinkOptions): Sink & AsyncDisposable;
|
|
33
81
|
/**
|
|
34
82
|
* Options for the {@link getStreamSink} function.
|
|
35
83
|
*/
|
|
@@ -108,6 +156,27 @@ interface ConsoleSinkOptions {
|
|
|
108
156
|
* @returns A sink that logs to the console.
|
|
109
157
|
*/
|
|
110
158
|
declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
|
|
159
|
+
/**
|
|
160
|
+
* Converts an async sink into a regular sink with proper async handling.
|
|
161
|
+
* The returned sink chains async operations to ensure proper ordering and
|
|
162
|
+
* implements AsyncDisposable to wait for all pending operations on disposal.
|
|
163
|
+
*
|
|
164
|
+
* @example Create a sink that asynchronously posts to a webhook
|
|
165
|
+
* ```typescript
|
|
166
|
+
* const asyncSink: AsyncSink = async (record) => {
|
|
167
|
+
* await fetch("https://example.com/logs", {
|
|
168
|
+
* method: "POST",
|
|
169
|
+
* body: JSON.stringify(record),
|
|
170
|
+
* });
|
|
171
|
+
* };
|
|
172
|
+
* const sink = fromAsyncSink(asyncSink);
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @param asyncSink The async sink function to convert.
|
|
176
|
+
* @returns A sink that properly handles async operations and disposal.
|
|
177
|
+
* @since 1.0.0
|
|
178
|
+
*/
|
|
179
|
+
declare function fromAsyncSink(asyncSink: AsyncSink): Sink & AsyncDisposable;
|
|
111
180
|
//#endregion
|
|
112
|
-
export { ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withFilter };
|
|
181
|
+
export { AsyncSink, BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
113
182
|
//# sourceMappingURL=sink.d.ts.map
|
package/dist/sink.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sink.d.ts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;
|
|
1
|
+
{"version":3,"file":"sink.d.ts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAWA;;;;AAAsD;AAgBtD;AAA0B,KA3Bd,IAAA,GA2Bc,CAAA,MAAA,EA3BE,SA2BF,EAAA,GAAA,IAAA;;;;AAAsC;AAWhE;AAmCA;;;;AAGG,KAjES,SAAA,GAiET,CAAA,MAAA,EAjE8B,SAiE9B,EAAA,GAjE4C,OAiE5C,CAAA,IAAA,CAAA;;AAAsB;AAsEzB;;;;AAS8C;AA2B9C;;;;;;AAGyB;AAkBpB,iBAhLW,UAAA,CAgLE,IAAA,EAhLe,IAgLf,EAAA,MAAA,EAhL6B,UAgL7B,CAAA,EAhL0C,IAgL1C;AAKlB;;;;AAsBoB,UAhMH,iBAAA,CAgMG;EAAQ;;;AAKT;AASnB;EAA8B,UAAA,CAAA,EAAA,MAAA;EAAA;;AAAwC;AA+CtE;;;EAAkD,aAAG,CAAA,EAAA,MAAA;;AAAsB;;;;;;;;;;;;;;;;;;iBA1N3D,UAAA,OACR,gBACG,oBACR,OAAO;;;;UAsEO,iBAAA;;;;cAIH;;;;;0BAKsB;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2BpB,aAAA,SACN,0BACC,oBACR,OAAO;KAkBL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;iBASI,cAAA,WAAwB,qBAA0B;;;;;;;;;;;;;;;;;;;;;iBA+ClD,aAAA,YAAyB,YAAY,OAAO"}
|
package/dist/sink.js
CHANGED
|
@@ -23,6 +23,68 @@ function withFilter(sink, filter) {
|
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
+
* Turns a sink into a buffered sink. The returned sink buffers log records
|
|
27
|
+
* in memory and flushes them to the underlying sink when the buffer is full
|
|
28
|
+
* or after a specified time interval.
|
|
29
|
+
*
|
|
30
|
+
* @example Buffer a console sink with custom options
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const sink = withBuffer(getConsoleSink(), {
|
|
33
|
+
* bufferSize: 5,
|
|
34
|
+
* flushInterval: 1000
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @param sink A sink to be buffered.
|
|
39
|
+
* @param options Options for the buffered sink.
|
|
40
|
+
* @returns A buffered sink that flushes records periodically.
|
|
41
|
+
* @since 1.0.0
|
|
42
|
+
*/
|
|
43
|
+
function withBuffer(sink, options = {}) {
|
|
44
|
+
const bufferSize = options.bufferSize ?? 10;
|
|
45
|
+
const flushInterval = options.flushInterval ?? 5e3;
|
|
46
|
+
const buffer = [];
|
|
47
|
+
let flushTimer = null;
|
|
48
|
+
let disposed = false;
|
|
49
|
+
function flush() {
|
|
50
|
+
if (buffer.length === 0) return;
|
|
51
|
+
const records = buffer.splice(0);
|
|
52
|
+
for (const record of records) try {
|
|
53
|
+
sink(record);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
if (flushTimer !== null) {
|
|
58
|
+
clearTimeout(flushTimer);
|
|
59
|
+
flushTimer = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function scheduleFlush() {
|
|
63
|
+
if (flushInterval <= 0 || flushTimer !== null || disposed) return;
|
|
64
|
+
flushTimer = setTimeout(() => {
|
|
65
|
+
flushTimer = null;
|
|
66
|
+
flush();
|
|
67
|
+
}, flushInterval);
|
|
68
|
+
}
|
|
69
|
+
const bufferedSink = (record) => {
|
|
70
|
+
if (disposed) return;
|
|
71
|
+
buffer.push(record);
|
|
72
|
+
if (buffer.length >= bufferSize) flush();
|
|
73
|
+
else scheduleFlush();
|
|
74
|
+
};
|
|
75
|
+
bufferedSink[Symbol.asyncDispose] = async () => {
|
|
76
|
+
disposed = true;
|
|
77
|
+
if (flushTimer !== null) {
|
|
78
|
+
clearTimeout(flushTimer);
|
|
79
|
+
flushTimer = null;
|
|
80
|
+
}
|
|
81
|
+
flush();
|
|
82
|
+
if (Symbol.asyncDispose in sink) await sink[Symbol.asyncDispose]();
|
|
83
|
+
else if (Symbol.dispose in sink) sink[Symbol.dispose]();
|
|
84
|
+
};
|
|
85
|
+
return bufferedSink;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
26
88
|
* A factory that returns a sink that writes to a {@link WritableStream}.
|
|
27
89
|
*
|
|
28
90
|
* Note that the `stream` is of Web Streams API, which is different from
|
|
@@ -89,7 +151,37 @@ function getConsoleSink(options = {}) {
|
|
|
89
151
|
} else console[method](...args);
|
|
90
152
|
};
|
|
91
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Converts an async sink into a regular sink with proper async handling.
|
|
156
|
+
* The returned sink chains async operations to ensure proper ordering and
|
|
157
|
+
* implements AsyncDisposable to wait for all pending operations on disposal.
|
|
158
|
+
*
|
|
159
|
+
* @example Create a sink that asynchronously posts to a webhook
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const asyncSink: AsyncSink = async (record) => {
|
|
162
|
+
* await fetch("https://example.com/logs", {
|
|
163
|
+
* method: "POST",
|
|
164
|
+
* body: JSON.stringify(record),
|
|
165
|
+
* });
|
|
166
|
+
* };
|
|
167
|
+
* const sink = fromAsyncSink(asyncSink);
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @param asyncSink The async sink function to convert.
|
|
171
|
+
* @returns A sink that properly handles async operations and disposal.
|
|
172
|
+
* @since 1.0.0
|
|
173
|
+
*/
|
|
174
|
+
function fromAsyncSink(asyncSink) {
|
|
175
|
+
let lastPromise = Promise.resolve();
|
|
176
|
+
const sink = (record) => {
|
|
177
|
+
lastPromise = lastPromise.then(() => asyncSink(record)).catch(() => {});
|
|
178
|
+
};
|
|
179
|
+
sink[Symbol.asyncDispose] = async () => {
|
|
180
|
+
await lastPromise;
|
|
181
|
+
};
|
|
182
|
+
return sink;
|
|
183
|
+
}
|
|
92
184
|
|
|
93
185
|
//#endregion
|
|
94
|
-
export { getConsoleSink, getStreamSink, withFilter };
|
|
186
|
+
export { fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
95
187
|
//# sourceMappingURL=sink.js.map
|
package/dist/sink.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sink.js","names":["sink: Sink","filter: FilterLike","record: LogRecord","stream: WritableStream","options: StreamSinkOptions","sink: Sink & AsyncDisposable","options: ConsoleSinkOptions","levelMap: Record<LogLevel, ConsoleMethod>"],"sources":["../sink.ts"],"sourcesContent":["import { type FilterLike, toFilter } from \"./filter.ts\";\nimport {\n type ConsoleFormatter,\n defaultConsoleFormatter,\n defaultTextFormatter,\n type TextFormatter,\n} from \"./formatter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport type { LogRecord } from \"./record.ts\";\n\n/**\n * A sink is a function that accepts a log record and prints it somewhere.\n * Thrown exceptions will be suppressed and then logged to the meta logger,\n * a {@link Logger} with the category `[\"logtape\", \"meta\"]`. (In that case,\n * the meta log record will not be passed to the sink to avoid infinite\n * recursion.)\n *\n * @param record The log record to sink.\n */\nexport type Sink = (record: LogRecord) => void;\n\n/**\n * Turns a sink into a filtered sink. The returned sink only logs records that\n * pass the filter.\n *\n * @example Filter a console sink to only log records with the info level\n * ```typescript\n * const sink = withFilter(getConsoleSink(), \"info\");\n * ```\n *\n * @param sink A sink to be filtered.\n * @param filter A filter to apply to the sink. It can be either a filter\n * function or a {@link LogLevel} string.\n * @returns A sink that only logs records that pass the filter.\n */\nexport function withFilter(sink: Sink, filter: FilterLike): Sink {\n const filterFunc = toFilter(filter);\n return (record: LogRecord) => {\n if (filterFunc(record)) sink(record);\n };\n}\n\n/**\n * Options for the {@link getStreamSink} function.\n */\nexport interface StreamSinkOptions {\n /**\n * The text formatter to use. Defaults to {@link defaultTextFormatter}.\n */\n formatter?: TextFormatter;\n\n /**\n * The text encoder to use. Defaults to an instance of {@link TextEncoder}.\n */\n encoder?: { encode(text: string): Uint8Array };\n}\n\n/**\n * A factory that returns a sink that writes to a {@link WritableStream}.\n *\n * Note that the `stream` is of Web Streams API, which is different from\n * Node.js streams. You can convert a Node.js stream to a Web Streams API\n * stream using [`stream.Writable.toWeb()`] method.\n *\n * [`stream.Writable.toWeb()`]: https://nodejs.org/api/stream.html#streamwritabletowebstreamwritable\n *\n * @example Sink to the standard error in Deno\n * ```typescript\n * const stderrSink = getStreamSink(Deno.stderr.writable);\n * ```\n *\n * @example Sink to the standard error in Node.js\n * ```typescript\n * import stream from \"node:stream\";\n * const stderrSink = getStreamSink(stream.Writable.toWeb(process.stderr));\n * ```\n *\n * @param stream The stream to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the stream.\n */\nexport function getStreamSink(\n stream: WritableStream,\n options: StreamSinkOptions = {},\n): Sink & AsyncDisposable {\n const formatter = options.formatter ?? defaultTextFormatter;\n const encoder = options.encoder ?? new TextEncoder();\n const writer = stream.getWriter();\n let lastPromise = Promise.resolve();\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n const bytes = encoder.encode(formatter(record));\n lastPromise = lastPromise\n .then(() => writer.ready)\n .then(() => writer.write(bytes));\n };\n sink[Symbol.asyncDispose] = async () => {\n await lastPromise;\n await writer.close();\n };\n return sink;\n}\n\ntype ConsoleMethod = \"debug\" | \"info\" | \"log\" | \"warn\" | \"error\";\n\n/**\n * Options for the {@link getConsoleSink} function.\n */\nexport interface ConsoleSinkOptions {\n /**\n * The console formatter or text formatter to use.\n * Defaults to {@link defaultConsoleFormatter}.\n */\n formatter?: ConsoleFormatter | TextFormatter;\n\n /**\n * The mapping from log levels to console methods. Defaults to:\n *\n * ```typescript\n * {\n * trace: \"trace\",\n * debug: \"debug\",\n * info: \"info\",\n * warning: \"warn\",\n * error: \"error\",\n * fatal: \"error\",\n * }\n * ```\n * @since 0.9.0\n */\n levelMap?: Record<LogLevel, ConsoleMethod>;\n\n /**\n * The console to log to. Defaults to {@link console}.\n */\n console?: Console;\n}\n\n/**\n * A console sink factory that returns a sink that logs to the console.\n *\n * @param options The options for the sink.\n * @returns A sink that logs to the console.\n */\nexport function getConsoleSink(options: ConsoleSinkOptions = {}): Sink {\n const formatter = options.formatter ?? defaultConsoleFormatter;\n const levelMap: Record<LogLevel, ConsoleMethod> = {\n trace: \"debug\",\n debug: \"debug\",\n info: \"info\",\n warning: \"warn\",\n error: \"error\",\n fatal: \"error\",\n ...(options.levelMap ?? {}),\n };\n const console = options.console ?? globalThis.console;\n return (record: LogRecord) => {\n const args = formatter(record);\n const method = levelMap[record.level];\n if (method === undefined) {\n throw new TypeError(`Invalid log level: ${record.level}.`);\n }\n if (typeof args === \"string\") {\n const msg = args.replace(/\\r?\\n$/, \"\");\n console[method](msg);\n } else {\n console[method](...args);\n }\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmCA,SAAgB,WAAWA,MAAYC,QAA0B;CAC/D,MAAM,aAAa,SAAS,OAAO;AACnC,QAAO,CAACC,WAAsB;AAC5B,MAAI,WAAW,OAAO,CAAE,MAAK,OAAO;CACrC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;AAyCD,SAAgB,cACdC,QACAC,UAA6B,CAAE,GACP;CACxB,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,UAAU,QAAQ,WAAW,IAAI;CACvC,MAAM,SAAS,OAAO,WAAW;CACjC,IAAI,cAAc,QAAQ,SAAS;CACnC,MAAMC,OAA+B,CAACH,WAAsB;EAC1D,MAAM,QAAQ,QAAQ,OAAO,UAAU,OAAO,CAAC;AAC/C,gBAAc,YACX,KAAK,MAAM,OAAO,MAAM,CACxB,KAAK,MAAM,OAAO,MAAM,MAAM,CAAC;CACnC;AACD,MAAK,OAAO,gBAAgB,YAAY;AACtC,QAAM;AACN,QAAM,OAAO,OAAO;CACrB;AACD,QAAO;AACR;;;;;;;AA2CD,SAAgB,eAAeI,UAA8B,CAAE,GAAQ;CACrE,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAMC,WAA4C;EAChD,OAAO;EACP,OAAO;EACP,MAAM;EACN,SAAS;EACT,OAAO;EACP,OAAO;EACP,GAAI,QAAQ,YAAY,CAAE;CAC3B;CACD,MAAM,UAAU,QAAQ,WAAW,WAAW;AAC9C,QAAO,CAACL,WAAsB;EAC5B,MAAM,OAAO,UAAU,OAAO;EAC9B,MAAM,SAAS,SAAS,OAAO;AAC/B,MAAI,kBACF,OAAM,IAAI,WAAW,qBAAqB,OAAO,MAAM;AAEzD,aAAW,SAAS,UAAU;GAC5B,MAAM,MAAM,KAAK,QAAQ,UAAU,GAAG;AACtC,WAAQ,QAAQ,IAAI;EACrB,MACC,SAAQ,QAAQ,GAAG,KAAK;CAE3B;AACF"}
|
|
1
|
+
{"version":3,"file":"sink.js","names":["sink: Sink","filter: FilterLike","record: LogRecord","options: BufferSinkOptions","buffer: LogRecord[]","flushTimer: ReturnType<typeof setTimeout> | null","bufferedSink: Sink & AsyncDisposable","stream: WritableStream","options: StreamSinkOptions","sink: Sink & AsyncDisposable","options: ConsoleSinkOptions","levelMap: Record<LogLevel, ConsoleMethod>","asyncSink: AsyncSink"],"sources":["../sink.ts"],"sourcesContent":["import { type FilterLike, toFilter } from \"./filter.ts\";\nimport {\n type ConsoleFormatter,\n defaultConsoleFormatter,\n defaultTextFormatter,\n type TextFormatter,\n} from \"./formatter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport type { LogRecord } from \"./record.ts\";\n\n/**\n * A sink is a function that accepts a log record and prints it somewhere.\n * Thrown exceptions will be suppressed and then logged to the meta logger,\n * a {@link Logger} with the category `[\"logtape\", \"meta\"]`. (In that case,\n * the meta log record will not be passed to the sink to avoid infinite\n * recursion.)\n *\n * @param record The log record to sink.\n */\nexport type Sink = (record: LogRecord) => void;\n\n/**\n * An async sink is a function that accepts a log record and asynchronously\n * processes it. This type is used with {@link fromAsyncSink} to create\n * a regular sink that properly handles asynchronous operations.\n *\n * @param record The log record to process asynchronously.\n * @returns A promise that resolves when the record has been processed.\n * @since 1.0.0\n */\nexport type AsyncSink = (record: LogRecord) => Promise<void>;\n\n/**\n * Turns a sink into a filtered sink. The returned sink only logs records that\n * pass the filter.\n *\n * @example Filter a console sink to only log records with the info level\n * ```typescript\n * const sink = withFilter(getConsoleSink(), \"info\");\n * ```\n *\n * @param sink A sink to be filtered.\n * @param filter A filter to apply to the sink. It can be either a filter\n * function or a {@link LogLevel} string.\n * @returns A sink that only logs records that pass the filter.\n */\nexport function withFilter(sink: Sink, filter: FilterLike): Sink {\n const filterFunc = toFilter(filter);\n return (record: LogRecord) => {\n if (filterFunc(record)) sink(record);\n };\n}\n\n/**\n * Options for the {@link withBuffer} function.\n * @since 1.0.0\n */\nexport interface BufferSinkOptions {\n /**\n * The maximum number of log records to buffer before flushing to the\n * underlying sink.\n * @default `10`\n */\n bufferSize?: number;\n\n /**\n * The maximum time in milliseconds to wait before flushing buffered records\n * to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or\n * negative to disable time-based flushing.\n * @default `5000`\n */\n flushInterval?: number;\n}\n\n/**\n * Turns a sink into a buffered sink. The returned sink buffers log records\n * in memory and flushes them to the underlying sink when the buffer is full\n * or after a specified time interval.\n *\n * @example Buffer a console sink with custom options\n * ```typescript\n * const sink = withBuffer(getConsoleSink(), {\n * bufferSize: 5,\n * flushInterval: 1000\n * });\n * ```\n *\n * @param sink A sink to be buffered.\n * @param options Options for the buffered sink.\n * @returns A buffered sink that flushes records periodically.\n * @since 1.0.0\n */\nexport function withBuffer(\n sink: Sink,\n options: BufferSinkOptions = {},\n): Sink & AsyncDisposable {\n const bufferSize = options.bufferSize ?? 10;\n const flushInterval = options.flushInterval ?? 5000;\n\n const buffer: LogRecord[] = [];\n let flushTimer: ReturnType<typeof setTimeout> | null = null;\n let disposed = false;\n\n function flush(): void {\n if (buffer.length === 0) return;\n\n const records = buffer.splice(0);\n for (const record of records) {\n try {\n sink(record);\n } catch (error) {\n // Errors are handled by the sink infrastructure\n throw error;\n }\n }\n\n if (flushTimer !== null) {\n clearTimeout(flushTimer);\n flushTimer = null;\n }\n }\n\n function scheduleFlush(): void {\n if (flushInterval <= 0 || flushTimer !== null || disposed) return;\n\n flushTimer = setTimeout(() => {\n flushTimer = null;\n flush();\n }, flushInterval);\n }\n\n const bufferedSink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n\n buffer.push(record);\n\n if (buffer.length >= bufferSize) {\n flush();\n } else {\n scheduleFlush();\n }\n };\n\n bufferedSink[Symbol.asyncDispose] = async () => {\n disposed = true;\n if (flushTimer !== null) {\n clearTimeout(flushTimer);\n flushTimer = null;\n }\n flush();\n\n // Dispose the underlying sink if it's disposable\n if (Symbol.asyncDispose in sink) {\n await (sink as AsyncDisposable)[Symbol.asyncDispose]();\n } else if (Symbol.dispose in sink) {\n (sink as Disposable)[Symbol.dispose]();\n }\n };\n\n return bufferedSink;\n}\n\n/**\n * Options for the {@link getStreamSink} function.\n */\nexport interface StreamSinkOptions {\n /**\n * The text formatter to use. Defaults to {@link defaultTextFormatter}.\n */\n formatter?: TextFormatter;\n\n /**\n * The text encoder to use. Defaults to an instance of {@link TextEncoder}.\n */\n encoder?: { encode(text: string): Uint8Array };\n}\n\n/**\n * A factory that returns a sink that writes to a {@link WritableStream}.\n *\n * Note that the `stream` is of Web Streams API, which is different from\n * Node.js streams. You can convert a Node.js stream to a Web Streams API\n * stream using [`stream.Writable.toWeb()`] method.\n *\n * [`stream.Writable.toWeb()`]: https://nodejs.org/api/stream.html#streamwritabletowebstreamwritable\n *\n * @example Sink to the standard error in Deno\n * ```typescript\n * const stderrSink = getStreamSink(Deno.stderr.writable);\n * ```\n *\n * @example Sink to the standard error in Node.js\n * ```typescript\n * import stream from \"node:stream\";\n * const stderrSink = getStreamSink(stream.Writable.toWeb(process.stderr));\n * ```\n *\n * @param stream The stream to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the stream.\n */\nexport function getStreamSink(\n stream: WritableStream,\n options: StreamSinkOptions = {},\n): Sink & AsyncDisposable {\n const formatter = options.formatter ?? defaultTextFormatter;\n const encoder = options.encoder ?? new TextEncoder();\n const writer = stream.getWriter();\n let lastPromise = Promise.resolve();\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n const bytes = encoder.encode(formatter(record));\n lastPromise = lastPromise\n .then(() => writer.ready)\n .then(() => writer.write(bytes));\n };\n sink[Symbol.asyncDispose] = async () => {\n await lastPromise;\n await writer.close();\n };\n return sink;\n}\n\ntype ConsoleMethod = \"debug\" | \"info\" | \"log\" | \"warn\" | \"error\";\n\n/**\n * Options for the {@link getConsoleSink} function.\n */\nexport interface ConsoleSinkOptions {\n /**\n * The console formatter or text formatter to use.\n * Defaults to {@link defaultConsoleFormatter}.\n */\n formatter?: ConsoleFormatter | TextFormatter;\n\n /**\n * The mapping from log levels to console methods. Defaults to:\n *\n * ```typescript\n * {\n * trace: \"trace\",\n * debug: \"debug\",\n * info: \"info\",\n * warning: \"warn\",\n * error: \"error\",\n * fatal: \"error\",\n * }\n * ```\n * @since 0.9.0\n */\n levelMap?: Record<LogLevel, ConsoleMethod>;\n\n /**\n * The console to log to. Defaults to {@link console}.\n */\n console?: Console;\n}\n\n/**\n * A console sink factory that returns a sink that logs to the console.\n *\n * @param options The options for the sink.\n * @returns A sink that logs to the console.\n */\nexport function getConsoleSink(options: ConsoleSinkOptions = {}): Sink {\n const formatter = options.formatter ?? defaultConsoleFormatter;\n const levelMap: Record<LogLevel, ConsoleMethod> = {\n trace: \"debug\",\n debug: \"debug\",\n info: \"info\",\n warning: \"warn\",\n error: \"error\",\n fatal: \"error\",\n ...(options.levelMap ?? {}),\n };\n const console = options.console ?? globalThis.console;\n return (record: LogRecord) => {\n const args = formatter(record);\n const method = levelMap[record.level];\n if (method === undefined) {\n throw new TypeError(`Invalid log level: ${record.level}.`);\n }\n if (typeof args === \"string\") {\n const msg = args.replace(/\\r?\\n$/, \"\");\n console[method](msg);\n } else {\n console[method](...args);\n }\n };\n}\n\n/**\n * Converts an async sink into a regular sink with proper async handling.\n * The returned sink chains async operations to ensure proper ordering and\n * implements AsyncDisposable to wait for all pending operations on disposal.\n *\n * @example Create a sink that asynchronously posts to a webhook\n * ```typescript\n * const asyncSink: AsyncSink = async (record) => {\n * await fetch(\"https://example.com/logs\", {\n * method: \"POST\",\n * body: JSON.stringify(record),\n * });\n * };\n * const sink = fromAsyncSink(asyncSink);\n * ```\n *\n * @param asyncSink The async sink function to convert.\n * @returns A sink that properly handles async operations and disposal.\n * @since 1.0.0\n */\nexport function fromAsyncSink(asyncSink: AsyncSink): Sink & AsyncDisposable {\n let lastPromise = Promise.resolve();\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n lastPromise = lastPromise\n .then(() => asyncSink(record))\n .catch(() => {\n // Errors are handled by the sink infrastructure\n });\n };\n sink[Symbol.asyncDispose] = async () => {\n await lastPromise;\n };\n return sink;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA8CA,SAAgB,WAAWA,MAAYC,QAA0B;CAC/D,MAAM,aAAa,SAAS,OAAO;AACnC,QAAO,CAACC,WAAsB;AAC5B,MAAI,WAAW,OAAO,CAAE,MAAK,OAAO;CACrC;AACF;;;;;;;;;;;;;;;;;;;AAyCD,SAAgB,WACdF,MACAG,UAA6B,CAAE,GACP;CACxB,MAAM,aAAa,QAAQ,cAAc;CACzC,MAAM,gBAAgB,QAAQ,iBAAiB;CAE/C,MAAMC,SAAsB,CAAE;CAC9B,IAAIC,aAAmD;CACvD,IAAI,WAAW;CAEf,SAAS,QAAc;AACrB,MAAI,OAAO,WAAW,EAAG;EAEzB,MAAM,UAAU,OAAO,OAAO,EAAE;AAChC,OAAK,MAAM,UAAU,QACnB,KAAI;AACF,QAAK,OAAO;EACb,SAAQ,OAAO;AAEd,SAAM;EACP;AAGH,MAAI,eAAe,MAAM;AACvB,gBAAa,WAAW;AACxB,gBAAa;EACd;CACF;CAED,SAAS,gBAAsB;AAC7B,MAAI,iBAAiB,KAAK,eAAe,QAAQ,SAAU;AAE3D,eAAa,WAAW,MAAM;AAC5B,gBAAa;AACb,UAAO;EACR,GAAE,cAAc;CAClB;CAED,MAAMC,eAAuC,CAACJ,WAAsB;AAClE,MAAI,SAAU;AAEd,SAAO,KAAK,OAAO;AAEnB,MAAI,OAAO,UAAU,WACnB,QAAO;MAEP,gBAAe;CAElB;AAED,cAAa,OAAO,gBAAgB,YAAY;AAC9C,aAAW;AACX,MAAI,eAAe,MAAM;AACvB,gBAAa,WAAW;AACxB,gBAAa;EACd;AACD,SAAO;AAGP,MAAI,OAAO,gBAAgB,KACzB,OAAM,AAAC,KAAyB,OAAO,eAAe;WAC7C,OAAO,WAAW,KAC3B,CAAC,KAAoB,OAAO,UAAU;CAEzC;AAED,QAAO;AACR;;;;;;;;;;;;;;;;;;;;;;;;;AAyCD,SAAgB,cACdK,QACAC,UAA6B,CAAE,GACP;CACxB,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,UAAU,QAAQ,WAAW,IAAI;CACvC,MAAM,SAAS,OAAO,WAAW;CACjC,IAAI,cAAc,QAAQ,SAAS;CACnC,MAAMC,OAA+B,CAACP,WAAsB;EAC1D,MAAM,QAAQ,QAAQ,OAAO,UAAU,OAAO,CAAC;AAC/C,gBAAc,YACX,KAAK,MAAM,OAAO,MAAM,CACxB,KAAK,MAAM,OAAO,MAAM,MAAM,CAAC;CACnC;AACD,MAAK,OAAO,gBAAgB,YAAY;AACtC,QAAM;AACN,QAAM,OAAO,OAAO;CACrB;AACD,QAAO;AACR;;;;;;;AA2CD,SAAgB,eAAeQ,UAA8B,CAAE,GAAQ;CACrE,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAMC,WAA4C;EAChD,OAAO;EACP,OAAO;EACP,MAAM;EACN,SAAS;EACT,OAAO;EACP,OAAO;EACP,GAAI,QAAQ,YAAY,CAAE;CAC3B;CACD,MAAM,UAAU,QAAQ,WAAW,WAAW;AAC9C,QAAO,CAACT,WAAsB;EAC5B,MAAM,OAAO,UAAU,OAAO;EAC9B,MAAM,SAAS,SAAS,OAAO;AAC/B,MAAI,kBACF,OAAM,IAAI,WAAW,qBAAqB,OAAO,MAAM;AAEzD,aAAW,SAAS,UAAU;GAC5B,MAAM,MAAM,KAAK,QAAQ,UAAU,GAAG;AACtC,WAAQ,QAAQ,IAAI;EACrB,MACC,SAAQ,QAAQ,GAAG,KAAK;CAE3B;AACF;;;;;;;;;;;;;;;;;;;;;AAsBD,SAAgB,cAAcU,WAA8C;CAC1E,IAAI,cAAc,QAAQ,SAAS;CACnC,MAAMH,OAA+B,CAACP,WAAsB;AAC1D,gBAAc,YACX,KAAK,MAAM,UAAU,OAAO,CAAC,CAC7B,MAAM,MAAM,CAEZ,EAAC;CACL;AACD,MAAK,OAAO,gBAAgB,YAAY;AACtC,QAAM;CACP;AACD,QAAO;AACR"}
|
package/mod.ts
CHANGED
|
@@ -44,11 +44,15 @@ export {
|
|
|
44
44
|
export { getLogger, type Logger, type LogMethod } from "./logger.ts";
|
|
45
45
|
export type { LogRecord } from "./record.ts";
|
|
46
46
|
export {
|
|
47
|
+
type AsyncSink,
|
|
48
|
+
type BufferSinkOptions,
|
|
47
49
|
type ConsoleSinkOptions,
|
|
50
|
+
fromAsyncSink,
|
|
48
51
|
getConsoleSink,
|
|
49
52
|
getStreamSink,
|
|
50
53
|
type Sink,
|
|
51
54
|
type StreamSinkOptions,
|
|
55
|
+
withBuffer,
|
|
52
56
|
withFilter,
|
|
53
57
|
} from "./sink.ts";
|
|
54
58
|
|
package/package.json
CHANGED
package/sink.test.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import { suite } from "@alinea/suite";
|
|
2
2
|
import { assertEquals } from "@std/assert/equals";
|
|
3
|
+
import { assertInstanceOf } from "@std/assert/instance-of";
|
|
3
4
|
import { assertThrows } from "@std/assert/throws";
|
|
5
|
+
import { delay } from "@std/async/delay";
|
|
4
6
|
import makeConsoleMock from "consolemock";
|
|
5
7
|
import { debug, error, fatal, info, trace, warning } from "./fixtures.ts";
|
|
6
8
|
import { defaultTextFormatter } from "./formatter.ts";
|
|
7
9
|
import type { LogLevel } from "./level.ts";
|
|
8
10
|
import type { LogRecord } from "./record.ts";
|
|
9
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
type AsyncSink,
|
|
13
|
+
fromAsyncSink,
|
|
14
|
+
getConsoleSink,
|
|
15
|
+
getStreamSink,
|
|
16
|
+
type Sink,
|
|
17
|
+
withBuffer,
|
|
18
|
+
withFilter,
|
|
19
|
+
} from "./sink.ts";
|
|
10
20
|
|
|
11
21
|
const test = suite(import.meta);
|
|
12
22
|
|
|
@@ -246,3 +256,583 @@ test("getConsoleSink()", () => {
|
|
|
246
256
|
},
|
|
247
257
|
]);
|
|
248
258
|
});
|
|
259
|
+
|
|
260
|
+
test("withBuffer() - buffer size limit", async () => {
|
|
261
|
+
const buffer: LogRecord[] = [];
|
|
262
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
|
|
263
|
+
|
|
264
|
+
// Add records one by one
|
|
265
|
+
sink(trace);
|
|
266
|
+
assertEquals(buffer.length, 0); // Not flushed yet
|
|
267
|
+
|
|
268
|
+
sink(debug);
|
|
269
|
+
assertEquals(buffer.length, 0); // Not flushed yet
|
|
270
|
+
|
|
271
|
+
sink(info);
|
|
272
|
+
assertEquals(buffer.length, 3); // Flushed when buffer is full
|
|
273
|
+
assertEquals(buffer, [trace, debug, info]);
|
|
274
|
+
|
|
275
|
+
// Add more records
|
|
276
|
+
sink(warning);
|
|
277
|
+
assertEquals(buffer.length, 3); // Previous records remain
|
|
278
|
+
|
|
279
|
+
sink(error);
|
|
280
|
+
assertEquals(buffer.length, 3); // Still not flushed
|
|
281
|
+
|
|
282
|
+
sink(fatal);
|
|
283
|
+
assertEquals(buffer.length, 6); // Flushed again
|
|
284
|
+
assertEquals(buffer, [trace, debug, info, warning, error, fatal]);
|
|
285
|
+
|
|
286
|
+
await sink[Symbol.asyncDispose]();
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test("withBuffer() - flush interval", async () => {
|
|
290
|
+
const buffer: LogRecord[] = [];
|
|
291
|
+
const sink = withBuffer(buffer.push.bind(buffer), {
|
|
292
|
+
bufferSize: 10,
|
|
293
|
+
flushInterval: 100,
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
sink(trace);
|
|
297
|
+
assertEquals(buffer.length, 0); // Not flushed immediately
|
|
298
|
+
|
|
299
|
+
// Wait for flush interval
|
|
300
|
+
await delay(150);
|
|
301
|
+
assertEquals(buffer.length, 1); // Flushed after interval
|
|
302
|
+
assertEquals(buffer, [trace]);
|
|
303
|
+
|
|
304
|
+
await sink[Symbol.asyncDispose]();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test("withBuffer() - dispose flushes remaining records", async () => {
|
|
308
|
+
const buffer: LogRecord[] = [];
|
|
309
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
|
|
310
|
+
|
|
311
|
+
sink(trace);
|
|
312
|
+
sink(debug);
|
|
313
|
+
assertEquals(buffer.length, 0); // Not flushed yet
|
|
314
|
+
|
|
315
|
+
await sink[Symbol.asyncDispose]();
|
|
316
|
+
assertEquals(buffer.length, 2); // Flushed on dispose
|
|
317
|
+
assertEquals(buffer, [trace, debug]);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test("withBuffer() - disabled flush interval", async () => {
|
|
321
|
+
const buffer: LogRecord[] = [];
|
|
322
|
+
const sink = withBuffer(buffer.push.bind(buffer), {
|
|
323
|
+
bufferSize: 10,
|
|
324
|
+
flushInterval: 0,
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
sink(trace);
|
|
328
|
+
assertEquals(buffer.length, 0); // Not flushed immediately
|
|
329
|
+
|
|
330
|
+
// Wait longer than normal flush interval
|
|
331
|
+
await delay(200);
|
|
332
|
+
assertEquals(buffer.length, 0); // Still not flushed due to disabled interval
|
|
333
|
+
|
|
334
|
+
await sink[Symbol.asyncDispose]();
|
|
335
|
+
assertEquals(buffer.length, 1); // Flushed only on dispose
|
|
336
|
+
assertEquals(buffer, [trace]);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
test("withBuffer() - default options", async () => {
|
|
340
|
+
const buffer: LogRecord[] = [];
|
|
341
|
+
const sink = withBuffer(buffer.push.bind(buffer));
|
|
342
|
+
|
|
343
|
+
// Add 9 records (less than default buffer size of 10)
|
|
344
|
+
for (let i = 0; i < 9; i++) {
|
|
345
|
+
sink(trace);
|
|
346
|
+
}
|
|
347
|
+
assertEquals(buffer.length, 0); // Not flushed yet
|
|
348
|
+
|
|
349
|
+
// Add 10th record to trigger flush
|
|
350
|
+
sink(trace);
|
|
351
|
+
assertEquals(buffer.length, 10); // Flushed when buffer is full
|
|
352
|
+
|
|
353
|
+
await sink[Symbol.asyncDispose]();
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test("withBuffer() - no operation after dispose", async () => {
|
|
357
|
+
const buffer: LogRecord[] = [];
|
|
358
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
|
|
359
|
+
|
|
360
|
+
await sink[Symbol.asyncDispose]();
|
|
361
|
+
|
|
362
|
+
// Try to add records after dispose
|
|
363
|
+
sink(trace);
|
|
364
|
+
sink(debug);
|
|
365
|
+
assertEquals(buffer.length, 0); // No records added after dispose
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
test("withBuffer() - disposes underlying AsyncDisposable sink", async () => {
|
|
369
|
+
const buffer: LogRecord[] = [];
|
|
370
|
+
let disposed = false;
|
|
371
|
+
|
|
372
|
+
const disposableSink: Sink & AsyncDisposable = (record: LogRecord) => {
|
|
373
|
+
buffer.push(record);
|
|
374
|
+
};
|
|
375
|
+
disposableSink[Symbol.asyncDispose] = () => {
|
|
376
|
+
disposed = true;
|
|
377
|
+
return Promise.resolve();
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const bufferedSink = withBuffer(disposableSink);
|
|
381
|
+
|
|
382
|
+
await bufferedSink[Symbol.asyncDispose]();
|
|
383
|
+
|
|
384
|
+
assertEquals(disposed, true); // Underlying sink should be disposed
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
test("withBuffer() - disposes underlying Disposable sink", async () => {
|
|
388
|
+
const buffer: LogRecord[] = [];
|
|
389
|
+
let disposed = false;
|
|
390
|
+
|
|
391
|
+
const disposableSink: Sink & Disposable = (record: LogRecord) => {
|
|
392
|
+
buffer.push(record);
|
|
393
|
+
};
|
|
394
|
+
disposableSink[Symbol.dispose] = () => {
|
|
395
|
+
disposed = true;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const bufferedSink = withBuffer(disposableSink);
|
|
399
|
+
|
|
400
|
+
await bufferedSink[Symbol.asyncDispose]();
|
|
401
|
+
|
|
402
|
+
assertEquals(disposed, true); // Underlying sink should be disposed
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
test("withBuffer() - handles non-disposable sink gracefully", async () => {
|
|
406
|
+
const buffer: LogRecord[] = [];
|
|
407
|
+
const regularSink: Sink = (record: LogRecord) => {
|
|
408
|
+
buffer.push(record);
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
const bufferedSink = withBuffer(regularSink);
|
|
412
|
+
|
|
413
|
+
// Should not throw when disposing non-disposable sink
|
|
414
|
+
await bufferedSink[Symbol.asyncDispose]();
|
|
415
|
+
|
|
416
|
+
// This test passes if no error is thrown
|
|
417
|
+
assertEquals(true, true);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
test("withBuffer() - edge case: bufferSize 1", async () => {
|
|
421
|
+
const buffer: LogRecord[] = [];
|
|
422
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 1 });
|
|
423
|
+
|
|
424
|
+
// Every record should flush immediately
|
|
425
|
+
sink(trace);
|
|
426
|
+
assertEquals(buffer.length, 1);
|
|
427
|
+
assertEquals(buffer, [trace]);
|
|
428
|
+
|
|
429
|
+
sink(debug);
|
|
430
|
+
assertEquals(buffer.length, 2);
|
|
431
|
+
assertEquals(buffer, [trace, debug]);
|
|
432
|
+
|
|
433
|
+
await sink[Symbol.asyncDispose]();
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
test("withBuffer() - edge case: bufferSize 0 or negative", async () => {
|
|
437
|
+
const buffer: LogRecord[] = [];
|
|
438
|
+
const sink1 = withBuffer(buffer.push.bind(buffer), { bufferSize: 0 });
|
|
439
|
+
const sink2 = withBuffer(buffer.push.bind(buffer), { bufferSize: -5 });
|
|
440
|
+
|
|
441
|
+
// Should still work, but behavior may vary
|
|
442
|
+
sink1(trace);
|
|
443
|
+
sink2(debug);
|
|
444
|
+
|
|
445
|
+
await sink1[Symbol.asyncDispose]();
|
|
446
|
+
await sink2[Symbol.asyncDispose]();
|
|
447
|
+
|
|
448
|
+
assertEquals(buffer.length, 2);
|
|
449
|
+
assertEquals(buffer, [trace, debug]);
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
test("withBuffer() - edge case: very large bufferSize", async () => {
|
|
453
|
+
const buffer: LogRecord[] = [];
|
|
454
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10000 });
|
|
455
|
+
|
|
456
|
+
// Add many records without hitting buffer limit
|
|
457
|
+
for (let i = 0; i < 100; i++) {
|
|
458
|
+
sink(trace);
|
|
459
|
+
}
|
|
460
|
+
assertEquals(buffer.length, 0); // Not flushed yet
|
|
461
|
+
|
|
462
|
+
await sink[Symbol.asyncDispose]();
|
|
463
|
+
assertEquals(buffer.length, 100); // All flushed on dispose
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
test("withBuffer() - edge case: rapid successive calls", async () => {
|
|
467
|
+
const buffer: LogRecord[] = [];
|
|
468
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
|
|
469
|
+
|
|
470
|
+
// Add records in rapid succession
|
|
471
|
+
sink(trace);
|
|
472
|
+
sink(debug);
|
|
473
|
+
sink(info); // This should trigger flush
|
|
474
|
+
sink(warning);
|
|
475
|
+
sink(error);
|
|
476
|
+
sink(fatal); // This should trigger another flush
|
|
477
|
+
|
|
478
|
+
assertEquals(buffer.length, 6);
|
|
479
|
+
assertEquals(buffer, [trace, debug, info, warning, error, fatal]);
|
|
480
|
+
|
|
481
|
+
await sink[Symbol.asyncDispose]();
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
test("withBuffer() - edge case: multiple timer flushes", async () => {
|
|
485
|
+
const buffer: LogRecord[] = [];
|
|
486
|
+
const sink = withBuffer(buffer.push.bind(buffer), {
|
|
487
|
+
bufferSize: 10,
|
|
488
|
+
flushInterval: 50,
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
sink(trace);
|
|
492
|
+
await delay(60);
|
|
493
|
+
assertEquals(buffer.length, 1); // First flush
|
|
494
|
+
|
|
495
|
+
sink(debug);
|
|
496
|
+
await delay(60);
|
|
497
|
+
assertEquals(buffer.length, 2); // Second flush
|
|
498
|
+
|
|
499
|
+
sink(info);
|
|
500
|
+
await delay(60);
|
|
501
|
+
assertEquals(buffer.length, 3); // Third flush
|
|
502
|
+
|
|
503
|
+
await sink[Symbol.asyncDispose]();
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
test("withBuffer() - edge case: timer and buffer size interaction", async () => {
|
|
507
|
+
const buffer: LogRecord[] = [];
|
|
508
|
+
const sink = withBuffer(buffer.push.bind(buffer), {
|
|
509
|
+
bufferSize: 3,
|
|
510
|
+
flushInterval: 100,
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
// Add 2 records (less than bufferSize)
|
|
514
|
+
sink(trace);
|
|
515
|
+
sink(debug);
|
|
516
|
+
assertEquals(buffer.length, 0);
|
|
517
|
+
|
|
518
|
+
// Wait for timer flush
|
|
519
|
+
await delay(120);
|
|
520
|
+
assertEquals(buffer.length, 2); // Timer flush
|
|
521
|
+
|
|
522
|
+
// Add 3 more records to trigger buffer flush
|
|
523
|
+
sink(info);
|
|
524
|
+
sink(warning);
|
|
525
|
+
sink(error); // Should trigger immediate flush
|
|
526
|
+
assertEquals(buffer.length, 5);
|
|
527
|
+
|
|
528
|
+
await sink[Symbol.asyncDispose]();
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
test("withBuffer() - edge case: dispose called multiple times", async () => {
|
|
532
|
+
const buffer: LogRecord[] = [];
|
|
533
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
|
|
534
|
+
|
|
535
|
+
sink(trace);
|
|
536
|
+
sink(debug);
|
|
537
|
+
|
|
538
|
+
// First dispose
|
|
539
|
+
await sink[Symbol.asyncDispose]();
|
|
540
|
+
assertEquals(buffer.length, 2);
|
|
541
|
+
|
|
542
|
+
// Second dispose - should not cause errors or duplicate records
|
|
543
|
+
await sink[Symbol.asyncDispose]();
|
|
544
|
+
assertEquals(buffer.length, 2); // Should remain the same
|
|
545
|
+
|
|
546
|
+
// Third dispose
|
|
547
|
+
await sink[Symbol.asyncDispose]();
|
|
548
|
+
assertEquals(buffer.length, 2); // Should remain the same
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
test("withBuffer() - edge case: underlying sink throws error", async () => {
|
|
552
|
+
let errorCount = 0;
|
|
553
|
+
const errorSink: Sink = () => {
|
|
554
|
+
errorCount++;
|
|
555
|
+
throw new Error("Sink error");
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
const bufferedSink = withBuffer(errorSink, { bufferSize: 2 });
|
|
559
|
+
|
|
560
|
+
// First record goes to buffer
|
|
561
|
+
bufferedSink(trace);
|
|
562
|
+
|
|
563
|
+
// Second record should trigger flush and throw error
|
|
564
|
+
try {
|
|
565
|
+
bufferedSink(debug);
|
|
566
|
+
// Should not reach here
|
|
567
|
+
assertEquals(true, false, "Expected error to be thrown");
|
|
568
|
+
} catch (error) {
|
|
569
|
+
assertInstanceOf(error, Error);
|
|
570
|
+
assertEquals(error.message, "Sink error");
|
|
571
|
+
assertEquals(errorCount, 1); // Only first record processed before error
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
await bufferedSink[Symbol.asyncDispose]();
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
test("withBuffer() - edge case: underlying AsyncDisposable throws error", async () => {
|
|
578
|
+
const buffer: LogRecord[] = [];
|
|
579
|
+
let disposed = false;
|
|
580
|
+
|
|
581
|
+
const errorDisposableSink: Sink & AsyncDisposable = (record: LogRecord) => {
|
|
582
|
+
buffer.push(record);
|
|
583
|
+
};
|
|
584
|
+
errorDisposableSink[Symbol.asyncDispose] = () => {
|
|
585
|
+
disposed = true;
|
|
586
|
+
throw new Error("Dispose error");
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
const bufferedSink = withBuffer(errorDisposableSink);
|
|
590
|
+
|
|
591
|
+
bufferedSink(trace);
|
|
592
|
+
|
|
593
|
+
try {
|
|
594
|
+
await bufferedSink[Symbol.asyncDispose]();
|
|
595
|
+
// Should not reach here
|
|
596
|
+
assertEquals(true, false, "Expected dispose error to be thrown");
|
|
597
|
+
} catch (error) {
|
|
598
|
+
assertInstanceOf(error, Error);
|
|
599
|
+
assertEquals(error.message, "Dispose error");
|
|
600
|
+
assertEquals(disposed, true); // Should still be disposed
|
|
601
|
+
assertEquals(buffer.length, 1); // Buffer should have been flushed before dispose error
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
test("withBuffer() - edge case: negative flushInterval", async () => {
|
|
606
|
+
const buffer: LogRecord[] = [];
|
|
607
|
+
const sink = withBuffer(buffer.push.bind(buffer), {
|
|
608
|
+
bufferSize: 10,
|
|
609
|
+
flushInterval: -1000,
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
sink(trace);
|
|
613
|
+
assertEquals(buffer.length, 0);
|
|
614
|
+
|
|
615
|
+
// Wait longer than a normal flush interval
|
|
616
|
+
await delay(200);
|
|
617
|
+
assertEquals(buffer.length, 0); // Should not flush due to negative interval
|
|
618
|
+
|
|
619
|
+
await sink[Symbol.asyncDispose]();
|
|
620
|
+
assertEquals(buffer.length, 1); // Should only flush on dispose
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
test("withBuffer() - edge case: concurrent dispose and log calls", async () => {
|
|
624
|
+
const buffer: LogRecord[] = [];
|
|
625
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
|
|
626
|
+
|
|
627
|
+
sink(trace);
|
|
628
|
+
|
|
629
|
+
// Start dispose and immediately try to log more
|
|
630
|
+
const disposePromise = sink[Symbol.asyncDispose]();
|
|
631
|
+
sink(debug); // This should be ignored since dispose is in progress
|
|
632
|
+
sink(info); // This should be ignored since dispose is in progress
|
|
633
|
+
|
|
634
|
+
await disposePromise;
|
|
635
|
+
|
|
636
|
+
// Only the first record should be in buffer
|
|
637
|
+
assertEquals(buffer.length, 1);
|
|
638
|
+
assertEquals(buffer, [trace]);
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
test("fromAsyncSink() - basic functionality", async () => {
|
|
642
|
+
const buffer: LogRecord[] = [];
|
|
643
|
+
const asyncSink: AsyncSink = async (record) => {
|
|
644
|
+
await delay(10);
|
|
645
|
+
buffer.push(record);
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
const sink = fromAsyncSink(asyncSink);
|
|
649
|
+
|
|
650
|
+
sink(trace);
|
|
651
|
+
sink(debug);
|
|
652
|
+
sink(info);
|
|
653
|
+
|
|
654
|
+
// Records should not be in buffer immediately
|
|
655
|
+
assertEquals(buffer.length, 0);
|
|
656
|
+
|
|
657
|
+
// Wait for async operations to complete
|
|
658
|
+
await sink[Symbol.asyncDispose]();
|
|
659
|
+
|
|
660
|
+
// All records should be in buffer in order
|
|
661
|
+
assertEquals(buffer.length, 3);
|
|
662
|
+
assertEquals(buffer, [trace, debug, info]);
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
test("fromAsyncSink() - promise chaining preserves order", async () => {
|
|
666
|
+
const buffer: LogRecord[] = [];
|
|
667
|
+
const delays = [50, 10, 30]; // Different delays for each call
|
|
668
|
+
let callIndex = 0;
|
|
669
|
+
|
|
670
|
+
const asyncSink: AsyncSink = async (record) => {
|
|
671
|
+
const delayTime = delays[callIndex % delays.length];
|
|
672
|
+
callIndex++;
|
|
673
|
+
await delay(delayTime);
|
|
674
|
+
buffer.push(record);
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
const sink = fromAsyncSink(asyncSink);
|
|
678
|
+
|
|
679
|
+
sink(trace);
|
|
680
|
+
sink(debug);
|
|
681
|
+
sink(info);
|
|
682
|
+
|
|
683
|
+
await sink[Symbol.asyncDispose]();
|
|
684
|
+
|
|
685
|
+
// Despite different delays, order should be preserved
|
|
686
|
+
assertEquals(buffer.length, 3);
|
|
687
|
+
assertEquals(buffer, [trace, debug, info]);
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
test("fromAsyncSink() - error handling", async () => {
|
|
691
|
+
const buffer: LogRecord[] = [];
|
|
692
|
+
let errorCount = 0;
|
|
693
|
+
|
|
694
|
+
const asyncSink: AsyncSink = async (record) => {
|
|
695
|
+
if (record.level === "error") {
|
|
696
|
+
errorCount++;
|
|
697
|
+
throw new Error("Async sink error");
|
|
698
|
+
}
|
|
699
|
+
await Promise.resolve(); // Ensure it's async
|
|
700
|
+
buffer.push(record);
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
const sink = fromAsyncSink(asyncSink);
|
|
704
|
+
|
|
705
|
+
sink(trace);
|
|
706
|
+
sink(error); // This will throw in async sink
|
|
707
|
+
sink(info);
|
|
708
|
+
|
|
709
|
+
await sink[Symbol.asyncDispose]();
|
|
710
|
+
|
|
711
|
+
// Error should be caught and not break the chain
|
|
712
|
+
assertEquals(errorCount, 1);
|
|
713
|
+
assertEquals(buffer.length, 2);
|
|
714
|
+
assertEquals(buffer, [trace, info]);
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
test("fromAsyncSink() - multiple dispose calls", async () => {
|
|
718
|
+
const buffer: LogRecord[] = [];
|
|
719
|
+
const asyncSink: AsyncSink = async (record) => {
|
|
720
|
+
await delay(10);
|
|
721
|
+
buffer.push(record);
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
const sink = fromAsyncSink(asyncSink);
|
|
725
|
+
|
|
726
|
+
sink(trace);
|
|
727
|
+
sink(debug);
|
|
728
|
+
|
|
729
|
+
// First dispose
|
|
730
|
+
await sink[Symbol.asyncDispose]();
|
|
731
|
+
assertEquals(buffer.length, 2);
|
|
732
|
+
|
|
733
|
+
// Second dispose should be safe
|
|
734
|
+
await sink[Symbol.asyncDispose]();
|
|
735
|
+
assertEquals(buffer.length, 2);
|
|
736
|
+
|
|
737
|
+
// Third dispose should be safe
|
|
738
|
+
await sink[Symbol.asyncDispose]();
|
|
739
|
+
assertEquals(buffer.length, 2);
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
test("fromAsyncSink() - concurrent calls", async () => {
|
|
743
|
+
const buffer: LogRecord[] = [];
|
|
744
|
+
let concurrentCalls = 0;
|
|
745
|
+
let maxConcurrentCalls = 0;
|
|
746
|
+
|
|
747
|
+
const asyncSink: AsyncSink = async (record) => {
|
|
748
|
+
concurrentCalls++;
|
|
749
|
+
maxConcurrentCalls = Math.max(maxConcurrentCalls, concurrentCalls);
|
|
750
|
+
await delay(20);
|
|
751
|
+
buffer.push(record);
|
|
752
|
+
concurrentCalls--;
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
const sink = fromAsyncSink(asyncSink);
|
|
756
|
+
|
|
757
|
+
// Fire multiple calls rapidly
|
|
758
|
+
for (let i = 0; i < 5; i++) {
|
|
759
|
+
sink(trace);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
await sink[Symbol.asyncDispose]();
|
|
763
|
+
|
|
764
|
+
// Due to promise chaining, max concurrent calls should be 1
|
|
765
|
+
assertEquals(maxConcurrentCalls, 1);
|
|
766
|
+
assertEquals(buffer.length, 5);
|
|
767
|
+
});
|
|
768
|
+
|
|
769
|
+
test("fromAsyncSink() - works with synchronous exceptions", async () => {
|
|
770
|
+
const buffer: LogRecord[] = [];
|
|
771
|
+
let errorCount = 0;
|
|
772
|
+
|
|
773
|
+
const asyncSink: AsyncSink = async (record) => {
|
|
774
|
+
if (record.level === "fatal") {
|
|
775
|
+
errorCount++;
|
|
776
|
+
// Synchronous throw before any await
|
|
777
|
+
throw new Error("Sync error in async sink");
|
|
778
|
+
}
|
|
779
|
+
await delay(10);
|
|
780
|
+
buffer.push(record);
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
const sink = fromAsyncSink(asyncSink);
|
|
784
|
+
|
|
785
|
+
sink(trace);
|
|
786
|
+
sink(fatal); // This will throw synchronously in async sink
|
|
787
|
+
sink(info);
|
|
788
|
+
|
|
789
|
+
await sink[Symbol.asyncDispose]();
|
|
790
|
+
|
|
791
|
+
// Error should still be caught
|
|
792
|
+
assertEquals(errorCount, 1);
|
|
793
|
+
assertEquals(buffer.length, 2);
|
|
794
|
+
assertEquals(buffer, [trace, info]);
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
test("fromAsyncSink() - very long async operations", async () => {
|
|
798
|
+
const buffer: LogRecord[] = [];
|
|
799
|
+
const asyncSink: AsyncSink = async (record) => {
|
|
800
|
+
await delay(100); // Longer delay
|
|
801
|
+
buffer.push(record);
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
const sink = fromAsyncSink(asyncSink);
|
|
805
|
+
|
|
806
|
+
sink(trace);
|
|
807
|
+
sink(debug);
|
|
808
|
+
|
|
809
|
+
// Don't wait, just dispose immediately
|
|
810
|
+
const disposePromise = sink[Symbol.asyncDispose]();
|
|
811
|
+
|
|
812
|
+
// Buffer should still be empty
|
|
813
|
+
assertEquals(buffer.length, 0);
|
|
814
|
+
|
|
815
|
+
// Wait for dispose to complete
|
|
816
|
+
await disposePromise;
|
|
817
|
+
|
|
818
|
+
// Now all records should be processed
|
|
819
|
+
assertEquals(buffer.length, 2);
|
|
820
|
+
assertEquals(buffer, [trace, debug]);
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
test("fromAsyncSink() - empty async sink", async () => {
|
|
824
|
+
const asyncSink: AsyncSink = async () => {
|
|
825
|
+
// Do nothing
|
|
826
|
+
};
|
|
827
|
+
|
|
828
|
+
const sink = fromAsyncSink(asyncSink);
|
|
829
|
+
|
|
830
|
+
// Should not throw
|
|
831
|
+
sink(trace);
|
|
832
|
+
sink(debug);
|
|
833
|
+
|
|
834
|
+
await sink[Symbol.asyncDispose]();
|
|
835
|
+
|
|
836
|
+
// Test passes if no errors thrown
|
|
837
|
+
assertEquals(true, true);
|
|
838
|
+
});
|
package/sink.ts
CHANGED
|
@@ -19,6 +19,17 @@ import type { LogRecord } from "./record.ts";
|
|
|
19
19
|
*/
|
|
20
20
|
export type Sink = (record: LogRecord) => void;
|
|
21
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
|
+
|
|
22
33
|
/**
|
|
23
34
|
* Turns a sink into a filtered sink. The returned sink only logs records that
|
|
24
35
|
* pass the filter.
|
|
@@ -40,6 +51,115 @@ export function withFilter(sink: Sink, filter: FilterLike): Sink {
|
|
|
40
51
|
};
|
|
41
52
|
}
|
|
42
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Options for the {@link withBuffer} function.
|
|
56
|
+
* @since 1.0.0
|
|
57
|
+
*/
|
|
58
|
+
export interface BufferSinkOptions {
|
|
59
|
+
/**
|
|
60
|
+
* The maximum number of log records to buffer before flushing to the
|
|
61
|
+
* underlying sink.
|
|
62
|
+
* @default `10`
|
|
63
|
+
*/
|
|
64
|
+
bufferSize?: number;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The maximum time in milliseconds to wait before flushing buffered records
|
|
68
|
+
* to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
|
|
69
|
+
* negative to disable time-based flushing.
|
|
70
|
+
* @default `5000`
|
|
71
|
+
*/
|
|
72
|
+
flushInterval?: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Turns a sink into a buffered sink. The returned sink buffers log records
|
|
77
|
+
* in memory and flushes them to the underlying sink when the buffer is full
|
|
78
|
+
* or after a specified time interval.
|
|
79
|
+
*
|
|
80
|
+
* @example Buffer a console sink with custom options
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const sink = withBuffer(getConsoleSink(), {
|
|
83
|
+
* bufferSize: 5,
|
|
84
|
+
* flushInterval: 1000
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @param sink A sink to be buffered.
|
|
89
|
+
* @param options Options for the buffered sink.
|
|
90
|
+
* @returns A buffered sink that flushes records periodically.
|
|
91
|
+
* @since 1.0.0
|
|
92
|
+
*/
|
|
93
|
+
export function withBuffer(
|
|
94
|
+
sink: Sink,
|
|
95
|
+
options: BufferSinkOptions = {},
|
|
96
|
+
): Sink & AsyncDisposable {
|
|
97
|
+
const bufferSize = options.bufferSize ?? 10;
|
|
98
|
+
const flushInterval = options.flushInterval ?? 5000;
|
|
99
|
+
|
|
100
|
+
const buffer: LogRecord[] = [];
|
|
101
|
+
let flushTimer: ReturnType<typeof setTimeout> | null = null;
|
|
102
|
+
let disposed = false;
|
|
103
|
+
|
|
104
|
+
function flush(): void {
|
|
105
|
+
if (buffer.length === 0) return;
|
|
106
|
+
|
|
107
|
+
const records = buffer.splice(0);
|
|
108
|
+
for (const record of records) {
|
|
109
|
+
try {
|
|
110
|
+
sink(record);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
// Errors are handled by the sink infrastructure
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (flushTimer !== null) {
|
|
118
|
+
clearTimeout(flushTimer);
|
|
119
|
+
flushTimer = null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function scheduleFlush(): void {
|
|
124
|
+
if (flushInterval <= 0 || flushTimer !== null || disposed) return;
|
|
125
|
+
|
|
126
|
+
flushTimer = setTimeout(() => {
|
|
127
|
+
flushTimer = null;
|
|
128
|
+
flush();
|
|
129
|
+
}, flushInterval);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const bufferedSink: Sink & AsyncDisposable = (record: LogRecord) => {
|
|
133
|
+
if (disposed) return;
|
|
134
|
+
|
|
135
|
+
buffer.push(record);
|
|
136
|
+
|
|
137
|
+
if (buffer.length >= bufferSize) {
|
|
138
|
+
flush();
|
|
139
|
+
} else {
|
|
140
|
+
scheduleFlush();
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
bufferedSink[Symbol.asyncDispose] = async () => {
|
|
145
|
+
disposed = true;
|
|
146
|
+
if (flushTimer !== null) {
|
|
147
|
+
clearTimeout(flushTimer);
|
|
148
|
+
flushTimer = null;
|
|
149
|
+
}
|
|
150
|
+
flush();
|
|
151
|
+
|
|
152
|
+
// Dispose the underlying sink if it's disposable
|
|
153
|
+
if (Symbol.asyncDispose in sink) {
|
|
154
|
+
await (sink as AsyncDisposable)[Symbol.asyncDispose]();
|
|
155
|
+
} else if (Symbol.dispose in sink) {
|
|
156
|
+
(sink as Disposable)[Symbol.dispose]();
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
return bufferedSink;
|
|
161
|
+
}
|
|
162
|
+
|
|
43
163
|
/**
|
|
44
164
|
* Options for the {@link getStreamSink} function.
|
|
45
165
|
*/
|
|
@@ -167,3 +287,38 @@ export function getConsoleSink(options: ConsoleSinkOptions = {}): Sink {
|
|
|
167
287
|
}
|
|
168
288
|
};
|
|
169
289
|
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Converts an async sink into a regular sink with proper async handling.
|
|
293
|
+
* The returned sink chains async operations to ensure proper ordering and
|
|
294
|
+
* implements AsyncDisposable to wait for all pending operations on disposal.
|
|
295
|
+
*
|
|
296
|
+
* @example Create a sink that asynchronously posts to a webhook
|
|
297
|
+
* ```typescript
|
|
298
|
+
* const asyncSink: AsyncSink = async (record) => {
|
|
299
|
+
* await fetch("https://example.com/logs", {
|
|
300
|
+
* method: "POST",
|
|
301
|
+
* body: JSON.stringify(record),
|
|
302
|
+
* });
|
|
303
|
+
* };
|
|
304
|
+
* const sink = fromAsyncSink(asyncSink);
|
|
305
|
+
* ```
|
|
306
|
+
*
|
|
307
|
+
* @param asyncSink The async sink function to convert.
|
|
308
|
+
* @returns A sink that properly handles async operations and disposal.
|
|
309
|
+
* @since 1.0.0
|
|
310
|
+
*/
|
|
311
|
+
export function fromAsyncSink(asyncSink: AsyncSink): Sink & AsyncDisposable {
|
|
312
|
+
let lastPromise = Promise.resolve();
|
|
313
|
+
const sink: Sink & AsyncDisposable = (record: LogRecord) => {
|
|
314
|
+
lastPromise = lastPromise
|
|
315
|
+
.then(() => asyncSink(record))
|
|
316
|
+
.catch(() => {
|
|
317
|
+
// Errors are handled by the sink infrastructure
|
|
318
|
+
});
|
|
319
|
+
};
|
|
320
|
+
sink[Symbol.asyncDispose] = async () => {
|
|
321
|
+
await lastPromise;
|
|
322
|
+
};
|
|
323
|
+
return sink;
|
|
324
|
+
}
|