@logtape/logtape 1.0.0-dev.207 → 1.0.0-dev.208
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 +1 -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 +63 -0
- package/dist/sink.d.cts +39 -1
- package/dist/sink.d.cts.map +1 -1
- package/dist/sink.d.ts +39 -1
- package/dist/sink.d.ts.map +1 -1
- package/dist/sink.js +63 -1
- package/dist/sink.js.map +1 -1
- package/mod.ts +2 -0
- package/package.json +1 -1
- package/sink.test.ts +390 -1
- package/sink.ts +109 -0
package/deno.json
CHANGED
package/dist/mod.cjs
CHANGED
|
@@ -30,5 +30,6 @@ exports.parseLogLevel = require_level.parseLogLevel;
|
|
|
30
30
|
exports.reset = require_config.reset;
|
|
31
31
|
exports.resetSync = require_config.resetSync;
|
|
32
32
|
exports.toFilter = require_filter.toFilter;
|
|
33
|
+
exports.withBuffer = require_sink.withBuffer;
|
|
33
34
|
exports.withContext = require_context.withContext;
|
|
34
35
|
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 { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, 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, 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, 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 { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, 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, 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, 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 { 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, 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
|
|
@@ -93,4 +155,5 @@ function getConsoleSink(options = {}) {
|
|
|
93
155
|
//#endregion
|
|
94
156
|
exports.getConsoleSink = getConsoleSink;
|
|
95
157
|
exports.getStreamSink = getStreamSink;
|
|
158
|
+
exports.withBuffer = withBuffer;
|
|
96
159
|
exports.withFilter = withFilter;
|
package/dist/sink.d.cts
CHANGED
|
@@ -30,6 +30,44 @@ type Sink = (record: LogRecord) => void;
|
|
|
30
30
|
* @returns A sink that only logs records that pass the filter.
|
|
31
31
|
*/
|
|
32
32
|
declare function withFilter(sink: Sink, filter: FilterLike): Sink;
|
|
33
|
+
/**
|
|
34
|
+
* Options for the {@link withBuffer} function.
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
*/
|
|
37
|
+
interface BufferSinkOptions {
|
|
38
|
+
/**
|
|
39
|
+
* The maximum number of log records to buffer before flushing to the
|
|
40
|
+
* underlying sink.
|
|
41
|
+
* @default `10`
|
|
42
|
+
*/
|
|
43
|
+
bufferSize?: number;
|
|
44
|
+
/**
|
|
45
|
+
* The maximum time in milliseconds to wait before flushing buffered records
|
|
46
|
+
* to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
|
|
47
|
+
* negative to disable time-based flushing.
|
|
48
|
+
* @default `5000`
|
|
49
|
+
*/
|
|
50
|
+
flushInterval?: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Turns a sink into a buffered sink. The returned sink buffers log records
|
|
54
|
+
* in memory and flushes them to the underlying sink when the buffer is full
|
|
55
|
+
* or after a specified time interval.
|
|
56
|
+
*
|
|
57
|
+
* @example Buffer a console sink with custom options
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const sink = withBuffer(getConsoleSink(), {
|
|
60
|
+
* bufferSize: 5,
|
|
61
|
+
* flushInterval: 1000
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @param sink A sink to be buffered.
|
|
66
|
+
* @param options Options for the buffered sink.
|
|
67
|
+
* @returns A buffered sink that flushes records periodically.
|
|
68
|
+
* @since 1.0.0
|
|
69
|
+
*/
|
|
70
|
+
declare function withBuffer(sink: Sink, options?: BufferSinkOptions): Sink & AsyncDisposable;
|
|
33
71
|
/**
|
|
34
72
|
* Options for the {@link getStreamSink} function.
|
|
35
73
|
*/
|
|
@@ -109,5 +147,5 @@ interface ConsoleSinkOptions {
|
|
|
109
147
|
*/
|
|
110
148
|
declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
|
|
111
149
|
//#endregion
|
|
112
|
-
export { ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withFilter };
|
|
150
|
+
export { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
113
151
|
//# 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;AAgBA;;;;;AAAgE;
|
|
1
|
+
{"version":3,"file":"sink.d.cts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAgBA;;;;;AAAgE;AAW/C,KA3BL,IAAA,GA2BK,CAAA,MAAiB,EA3BN,SA2BM,EAAA,GAAA,IAAA;AAmClC;;;;;;AAGyB;AAsEzB;;;;AAS8C;AA2B9C;;AACU,iBA5JM,UAAA,CA4JN,IAAA,EA5JuB,IA4JvB,EAAA,MAAA,EA5JqC,UA4JrC,CAAA,EA5JkD,IA4JlD;;;;AAEe;AAkBpB,UArKY,iBAAA,CAqKC;EAKD;;;;;EAsBW,UAAE,CAAA,EAAA,MAAA;EAAa;;AAKxB;AASnB;;;EAA+D,aAAG,CAAA,EAAA,MAAA;AAAI;;;;;;;;;;;;;;;;;;;iBA3KtD,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"}
|
package/dist/sink.d.ts
CHANGED
|
@@ -30,6 +30,44 @@ type Sink = (record: LogRecord) => void;
|
|
|
30
30
|
* @returns A sink that only logs records that pass the filter.
|
|
31
31
|
*/
|
|
32
32
|
declare function withFilter(sink: Sink, filter: FilterLike): Sink;
|
|
33
|
+
/**
|
|
34
|
+
* Options for the {@link withBuffer} function.
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
*/
|
|
37
|
+
interface BufferSinkOptions {
|
|
38
|
+
/**
|
|
39
|
+
* The maximum number of log records to buffer before flushing to the
|
|
40
|
+
* underlying sink.
|
|
41
|
+
* @default `10`
|
|
42
|
+
*/
|
|
43
|
+
bufferSize?: number;
|
|
44
|
+
/**
|
|
45
|
+
* The maximum time in milliseconds to wait before flushing buffered records
|
|
46
|
+
* to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
|
|
47
|
+
* negative to disable time-based flushing.
|
|
48
|
+
* @default `5000`
|
|
49
|
+
*/
|
|
50
|
+
flushInterval?: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Turns a sink into a buffered sink. The returned sink buffers log records
|
|
54
|
+
* in memory and flushes them to the underlying sink when the buffer is full
|
|
55
|
+
* or after a specified time interval.
|
|
56
|
+
*
|
|
57
|
+
* @example Buffer a console sink with custom options
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const sink = withBuffer(getConsoleSink(), {
|
|
60
|
+
* bufferSize: 5,
|
|
61
|
+
* flushInterval: 1000
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @param sink A sink to be buffered.
|
|
66
|
+
* @param options Options for the buffered sink.
|
|
67
|
+
* @returns A buffered sink that flushes records periodically.
|
|
68
|
+
* @since 1.0.0
|
|
69
|
+
*/
|
|
70
|
+
declare function withBuffer(sink: Sink, options?: BufferSinkOptions): Sink & AsyncDisposable;
|
|
33
71
|
/**
|
|
34
72
|
* Options for the {@link getStreamSink} function.
|
|
35
73
|
*/
|
|
@@ -109,5 +147,5 @@ interface ConsoleSinkOptions {
|
|
|
109
147
|
*/
|
|
110
148
|
declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
|
|
111
149
|
//#endregion
|
|
112
|
-
export { ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withFilter };
|
|
150
|
+
export { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
113
151
|
//# 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;AAgBA;;;;;AAAgE;
|
|
1
|
+
{"version":3,"file":"sink.d.ts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAgBA;;;;;AAAgE;AAW/C,KA3BL,IAAA,GA2BK,CAAA,MAAiB,EA3BN,SA2BM,EAAA,GAAA,IAAA;AAmClC;;;;;;AAGyB;AAsEzB;;;;AAS8C;AA2B9C;;AACU,iBA5JM,UAAA,CA4JN,IAAA,EA5JuB,IA4JvB,EAAA,MAAA,EA5JqC,UA4JrC,CAAA,EA5JkD,IA4JlD;;;;AAEe;AAkBpB,UArKY,iBAAA,CAqKC;EAKD;;;;;EAsBW,UAAE,CAAA,EAAA,MAAA;EAAa;;AAKxB;AASnB;;;EAA+D,aAAG,CAAA,EAAA,MAAA;AAAI;;;;;;;;;;;;;;;;;;;iBA3KtD,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"}
|
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
|
|
@@ -91,5 +153,5 @@ function getConsoleSink(options = {}) {
|
|
|
91
153
|
}
|
|
92
154
|
|
|
93
155
|
//#endregion
|
|
94
|
-
export { getConsoleSink, getStreamSink, withFilter };
|
|
156
|
+
export { getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
95
157
|
//# 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>"],"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 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"],"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,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"}
|
package/mod.ts
CHANGED
|
@@ -44,11 +44,13 @@ 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 BufferSinkOptions,
|
|
47
48
|
type ConsoleSinkOptions,
|
|
48
49
|
getConsoleSink,
|
|
49
50
|
getStreamSink,
|
|
50
51
|
type Sink,
|
|
51
52
|
type StreamSinkOptions,
|
|
53
|
+
withBuffer,
|
|
52
54
|
withFilter,
|
|
53
55
|
} from "./sink.ts";
|
|
54
56
|
|
package/package.json
CHANGED
package/sink.test.ts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
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
|
+
getConsoleSink,
|
|
13
|
+
getStreamSink,
|
|
14
|
+
type Sink,
|
|
15
|
+
withBuffer,
|
|
16
|
+
withFilter,
|
|
17
|
+
} from "./sink.ts";
|
|
10
18
|
|
|
11
19
|
const test = suite(import.meta);
|
|
12
20
|
|
|
@@ -246,3 +254,384 @@ test("getConsoleSink()", () => {
|
|
|
246
254
|
},
|
|
247
255
|
]);
|
|
248
256
|
});
|
|
257
|
+
|
|
258
|
+
test("withBuffer() - buffer size limit", async () => {
|
|
259
|
+
const buffer: LogRecord[] = [];
|
|
260
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
|
|
261
|
+
|
|
262
|
+
// Add records one by one
|
|
263
|
+
sink(trace);
|
|
264
|
+
assertEquals(buffer.length, 0); // Not flushed yet
|
|
265
|
+
|
|
266
|
+
sink(debug);
|
|
267
|
+
assertEquals(buffer.length, 0); // Not flushed yet
|
|
268
|
+
|
|
269
|
+
sink(info);
|
|
270
|
+
assertEquals(buffer.length, 3); // Flushed when buffer is full
|
|
271
|
+
assertEquals(buffer, [trace, debug, info]);
|
|
272
|
+
|
|
273
|
+
// Add more records
|
|
274
|
+
sink(warning);
|
|
275
|
+
assertEquals(buffer.length, 3); // Previous records remain
|
|
276
|
+
|
|
277
|
+
sink(error);
|
|
278
|
+
assertEquals(buffer.length, 3); // Still not flushed
|
|
279
|
+
|
|
280
|
+
sink(fatal);
|
|
281
|
+
assertEquals(buffer.length, 6); // Flushed again
|
|
282
|
+
assertEquals(buffer, [trace, debug, info, warning, error, fatal]);
|
|
283
|
+
|
|
284
|
+
await sink[Symbol.asyncDispose]();
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test("withBuffer() - flush interval", async () => {
|
|
288
|
+
const buffer: LogRecord[] = [];
|
|
289
|
+
const sink = withBuffer(buffer.push.bind(buffer), {
|
|
290
|
+
bufferSize: 10,
|
|
291
|
+
flushInterval: 100,
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
sink(trace);
|
|
295
|
+
assertEquals(buffer.length, 0); // Not flushed immediately
|
|
296
|
+
|
|
297
|
+
// Wait for flush interval
|
|
298
|
+
await delay(150);
|
|
299
|
+
assertEquals(buffer.length, 1); // Flushed after interval
|
|
300
|
+
assertEquals(buffer, [trace]);
|
|
301
|
+
|
|
302
|
+
await sink[Symbol.asyncDispose]();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test("withBuffer() - dispose flushes remaining records", async () => {
|
|
306
|
+
const buffer: LogRecord[] = [];
|
|
307
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
|
|
308
|
+
|
|
309
|
+
sink(trace);
|
|
310
|
+
sink(debug);
|
|
311
|
+
assertEquals(buffer.length, 0); // Not flushed yet
|
|
312
|
+
|
|
313
|
+
await sink[Symbol.asyncDispose]();
|
|
314
|
+
assertEquals(buffer.length, 2); // Flushed on dispose
|
|
315
|
+
assertEquals(buffer, [trace, debug]);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
test("withBuffer() - disabled flush interval", async () => {
|
|
319
|
+
const buffer: LogRecord[] = [];
|
|
320
|
+
const sink = withBuffer(buffer.push.bind(buffer), {
|
|
321
|
+
bufferSize: 10,
|
|
322
|
+
flushInterval: 0,
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
sink(trace);
|
|
326
|
+
assertEquals(buffer.length, 0); // Not flushed immediately
|
|
327
|
+
|
|
328
|
+
// Wait longer than normal flush interval
|
|
329
|
+
await delay(200);
|
|
330
|
+
assertEquals(buffer.length, 0); // Still not flushed due to disabled interval
|
|
331
|
+
|
|
332
|
+
await sink[Symbol.asyncDispose]();
|
|
333
|
+
assertEquals(buffer.length, 1); // Flushed only on dispose
|
|
334
|
+
assertEquals(buffer, [trace]);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
test("withBuffer() - default options", async () => {
|
|
338
|
+
const buffer: LogRecord[] = [];
|
|
339
|
+
const sink = withBuffer(buffer.push.bind(buffer));
|
|
340
|
+
|
|
341
|
+
// Add 9 records (less than default buffer size of 10)
|
|
342
|
+
for (let i = 0; i < 9; i++) {
|
|
343
|
+
sink(trace);
|
|
344
|
+
}
|
|
345
|
+
assertEquals(buffer.length, 0); // Not flushed yet
|
|
346
|
+
|
|
347
|
+
// Add 10th record to trigger flush
|
|
348
|
+
sink(trace);
|
|
349
|
+
assertEquals(buffer.length, 10); // Flushed when buffer is full
|
|
350
|
+
|
|
351
|
+
await sink[Symbol.asyncDispose]();
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test("withBuffer() - no operation after dispose", async () => {
|
|
355
|
+
const buffer: LogRecord[] = [];
|
|
356
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
|
|
357
|
+
|
|
358
|
+
await sink[Symbol.asyncDispose]();
|
|
359
|
+
|
|
360
|
+
// Try to add records after dispose
|
|
361
|
+
sink(trace);
|
|
362
|
+
sink(debug);
|
|
363
|
+
assertEquals(buffer.length, 0); // No records added after dispose
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
test("withBuffer() - disposes underlying AsyncDisposable sink", async () => {
|
|
367
|
+
const buffer: LogRecord[] = [];
|
|
368
|
+
let disposed = false;
|
|
369
|
+
|
|
370
|
+
const disposableSink: Sink & AsyncDisposable = (record: LogRecord) => {
|
|
371
|
+
buffer.push(record);
|
|
372
|
+
};
|
|
373
|
+
disposableSink[Symbol.asyncDispose] = () => {
|
|
374
|
+
disposed = true;
|
|
375
|
+
return Promise.resolve();
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const bufferedSink = withBuffer(disposableSink);
|
|
379
|
+
|
|
380
|
+
await bufferedSink[Symbol.asyncDispose]();
|
|
381
|
+
|
|
382
|
+
assertEquals(disposed, true); // Underlying sink should be disposed
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
test("withBuffer() - disposes underlying Disposable sink", async () => {
|
|
386
|
+
const buffer: LogRecord[] = [];
|
|
387
|
+
let disposed = false;
|
|
388
|
+
|
|
389
|
+
const disposableSink: Sink & Disposable = (record: LogRecord) => {
|
|
390
|
+
buffer.push(record);
|
|
391
|
+
};
|
|
392
|
+
disposableSink[Symbol.dispose] = () => {
|
|
393
|
+
disposed = true;
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
const bufferedSink = withBuffer(disposableSink);
|
|
397
|
+
|
|
398
|
+
await bufferedSink[Symbol.asyncDispose]();
|
|
399
|
+
|
|
400
|
+
assertEquals(disposed, true); // Underlying sink should be disposed
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
test("withBuffer() - handles non-disposable sink gracefully", async () => {
|
|
404
|
+
const buffer: LogRecord[] = [];
|
|
405
|
+
const regularSink: Sink = (record: LogRecord) => {
|
|
406
|
+
buffer.push(record);
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
const bufferedSink = withBuffer(regularSink);
|
|
410
|
+
|
|
411
|
+
// Should not throw when disposing non-disposable sink
|
|
412
|
+
await bufferedSink[Symbol.asyncDispose]();
|
|
413
|
+
|
|
414
|
+
// This test passes if no error is thrown
|
|
415
|
+
assertEquals(true, true);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
test("withBuffer() - edge case: bufferSize 1", async () => {
|
|
419
|
+
const buffer: LogRecord[] = [];
|
|
420
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 1 });
|
|
421
|
+
|
|
422
|
+
// Every record should flush immediately
|
|
423
|
+
sink(trace);
|
|
424
|
+
assertEquals(buffer.length, 1);
|
|
425
|
+
assertEquals(buffer, [trace]);
|
|
426
|
+
|
|
427
|
+
sink(debug);
|
|
428
|
+
assertEquals(buffer.length, 2);
|
|
429
|
+
assertEquals(buffer, [trace, debug]);
|
|
430
|
+
|
|
431
|
+
await sink[Symbol.asyncDispose]();
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
test("withBuffer() - edge case: bufferSize 0 or negative", async () => {
|
|
435
|
+
const buffer: LogRecord[] = [];
|
|
436
|
+
const sink1 = withBuffer(buffer.push.bind(buffer), { bufferSize: 0 });
|
|
437
|
+
const sink2 = withBuffer(buffer.push.bind(buffer), { bufferSize: -5 });
|
|
438
|
+
|
|
439
|
+
// Should still work, but behavior may vary
|
|
440
|
+
sink1(trace);
|
|
441
|
+
sink2(debug);
|
|
442
|
+
|
|
443
|
+
await sink1[Symbol.asyncDispose]();
|
|
444
|
+
await sink2[Symbol.asyncDispose]();
|
|
445
|
+
|
|
446
|
+
assertEquals(buffer.length, 2);
|
|
447
|
+
assertEquals(buffer, [trace, debug]);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
test("withBuffer() - edge case: very large bufferSize", async () => {
|
|
451
|
+
const buffer: LogRecord[] = [];
|
|
452
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10000 });
|
|
453
|
+
|
|
454
|
+
// Add many records without hitting buffer limit
|
|
455
|
+
for (let i = 0; i < 100; i++) {
|
|
456
|
+
sink(trace);
|
|
457
|
+
}
|
|
458
|
+
assertEquals(buffer.length, 0); // Not flushed yet
|
|
459
|
+
|
|
460
|
+
await sink[Symbol.asyncDispose]();
|
|
461
|
+
assertEquals(buffer.length, 100); // All flushed on dispose
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
test("withBuffer() - edge case: rapid successive calls", async () => {
|
|
465
|
+
const buffer: LogRecord[] = [];
|
|
466
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
|
|
467
|
+
|
|
468
|
+
// Add records in rapid succession
|
|
469
|
+
sink(trace);
|
|
470
|
+
sink(debug);
|
|
471
|
+
sink(info); // This should trigger flush
|
|
472
|
+
sink(warning);
|
|
473
|
+
sink(error);
|
|
474
|
+
sink(fatal); // This should trigger another flush
|
|
475
|
+
|
|
476
|
+
assertEquals(buffer.length, 6);
|
|
477
|
+
assertEquals(buffer, [trace, debug, info, warning, error, fatal]);
|
|
478
|
+
|
|
479
|
+
await sink[Symbol.asyncDispose]();
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
test("withBuffer() - edge case: multiple timer flushes", async () => {
|
|
483
|
+
const buffer: LogRecord[] = [];
|
|
484
|
+
const sink = withBuffer(buffer.push.bind(buffer), {
|
|
485
|
+
bufferSize: 10,
|
|
486
|
+
flushInterval: 50,
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
sink(trace);
|
|
490
|
+
await delay(60);
|
|
491
|
+
assertEquals(buffer.length, 1); // First flush
|
|
492
|
+
|
|
493
|
+
sink(debug);
|
|
494
|
+
await delay(60);
|
|
495
|
+
assertEquals(buffer.length, 2); // Second flush
|
|
496
|
+
|
|
497
|
+
sink(info);
|
|
498
|
+
await delay(60);
|
|
499
|
+
assertEquals(buffer.length, 3); // Third flush
|
|
500
|
+
|
|
501
|
+
await sink[Symbol.asyncDispose]();
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
test("withBuffer() - edge case: timer and buffer size interaction", async () => {
|
|
505
|
+
const buffer: LogRecord[] = [];
|
|
506
|
+
const sink = withBuffer(buffer.push.bind(buffer), {
|
|
507
|
+
bufferSize: 3,
|
|
508
|
+
flushInterval: 100,
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
// Add 2 records (less than bufferSize)
|
|
512
|
+
sink(trace);
|
|
513
|
+
sink(debug);
|
|
514
|
+
assertEquals(buffer.length, 0);
|
|
515
|
+
|
|
516
|
+
// Wait for timer flush
|
|
517
|
+
await delay(120);
|
|
518
|
+
assertEquals(buffer.length, 2); // Timer flush
|
|
519
|
+
|
|
520
|
+
// Add 3 more records to trigger buffer flush
|
|
521
|
+
sink(info);
|
|
522
|
+
sink(warning);
|
|
523
|
+
sink(error); // Should trigger immediate flush
|
|
524
|
+
assertEquals(buffer.length, 5);
|
|
525
|
+
|
|
526
|
+
await sink[Symbol.asyncDispose]();
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
test("withBuffer() - edge case: dispose called multiple times", async () => {
|
|
530
|
+
const buffer: LogRecord[] = [];
|
|
531
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
|
|
532
|
+
|
|
533
|
+
sink(trace);
|
|
534
|
+
sink(debug);
|
|
535
|
+
|
|
536
|
+
// First dispose
|
|
537
|
+
await sink[Symbol.asyncDispose]();
|
|
538
|
+
assertEquals(buffer.length, 2);
|
|
539
|
+
|
|
540
|
+
// Second dispose - should not cause errors or duplicate records
|
|
541
|
+
await sink[Symbol.asyncDispose]();
|
|
542
|
+
assertEquals(buffer.length, 2); // Should remain the same
|
|
543
|
+
|
|
544
|
+
// Third dispose
|
|
545
|
+
await sink[Symbol.asyncDispose]();
|
|
546
|
+
assertEquals(buffer.length, 2); // Should remain the same
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
test("withBuffer() - edge case: underlying sink throws error", async () => {
|
|
550
|
+
let errorCount = 0;
|
|
551
|
+
const errorSink: Sink = () => {
|
|
552
|
+
errorCount++;
|
|
553
|
+
throw new Error("Sink error");
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
const bufferedSink = withBuffer(errorSink, { bufferSize: 2 });
|
|
557
|
+
|
|
558
|
+
// First record goes to buffer
|
|
559
|
+
bufferedSink(trace);
|
|
560
|
+
|
|
561
|
+
// Second record should trigger flush and throw error
|
|
562
|
+
try {
|
|
563
|
+
bufferedSink(debug);
|
|
564
|
+
// Should not reach here
|
|
565
|
+
assertEquals(true, false, "Expected error to be thrown");
|
|
566
|
+
} catch (error) {
|
|
567
|
+
assertInstanceOf(error, Error);
|
|
568
|
+
assertEquals(error.message, "Sink error");
|
|
569
|
+
assertEquals(errorCount, 1); // Only first record processed before error
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
await bufferedSink[Symbol.asyncDispose]();
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
test("withBuffer() - edge case: underlying AsyncDisposable throws error", async () => {
|
|
576
|
+
const buffer: LogRecord[] = [];
|
|
577
|
+
let disposed = false;
|
|
578
|
+
|
|
579
|
+
const errorDisposableSink: Sink & AsyncDisposable = (record: LogRecord) => {
|
|
580
|
+
buffer.push(record);
|
|
581
|
+
};
|
|
582
|
+
errorDisposableSink[Symbol.asyncDispose] = () => {
|
|
583
|
+
disposed = true;
|
|
584
|
+
throw new Error("Dispose error");
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
const bufferedSink = withBuffer(errorDisposableSink);
|
|
588
|
+
|
|
589
|
+
bufferedSink(trace);
|
|
590
|
+
|
|
591
|
+
try {
|
|
592
|
+
await bufferedSink[Symbol.asyncDispose]();
|
|
593
|
+
// Should not reach here
|
|
594
|
+
assertEquals(true, false, "Expected dispose error to be thrown");
|
|
595
|
+
} catch (error) {
|
|
596
|
+
assertInstanceOf(error, Error);
|
|
597
|
+
assertEquals(error.message, "Dispose error");
|
|
598
|
+
assertEquals(disposed, true); // Should still be disposed
|
|
599
|
+
assertEquals(buffer.length, 1); // Buffer should have been flushed before dispose error
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
test("withBuffer() - edge case: negative flushInterval", async () => {
|
|
604
|
+
const buffer: LogRecord[] = [];
|
|
605
|
+
const sink = withBuffer(buffer.push.bind(buffer), {
|
|
606
|
+
bufferSize: 10,
|
|
607
|
+
flushInterval: -1000,
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
sink(trace);
|
|
611
|
+
assertEquals(buffer.length, 0);
|
|
612
|
+
|
|
613
|
+
// Wait longer than a normal flush interval
|
|
614
|
+
await delay(200);
|
|
615
|
+
assertEquals(buffer.length, 0); // Should not flush due to negative interval
|
|
616
|
+
|
|
617
|
+
await sink[Symbol.asyncDispose]();
|
|
618
|
+
assertEquals(buffer.length, 1); // Should only flush on dispose
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
test("withBuffer() - edge case: concurrent dispose and log calls", async () => {
|
|
622
|
+
const buffer: LogRecord[] = [];
|
|
623
|
+
const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
|
|
624
|
+
|
|
625
|
+
sink(trace);
|
|
626
|
+
|
|
627
|
+
// Start dispose and immediately try to log more
|
|
628
|
+
const disposePromise = sink[Symbol.asyncDispose]();
|
|
629
|
+
sink(debug); // This should be ignored since dispose is in progress
|
|
630
|
+
sink(info); // This should be ignored since dispose is in progress
|
|
631
|
+
|
|
632
|
+
await disposePromise;
|
|
633
|
+
|
|
634
|
+
// Only the first record should be in buffer
|
|
635
|
+
assertEquals(buffer.length, 1);
|
|
636
|
+
assertEquals(buffer, [trace]);
|
|
637
|
+
});
|
package/sink.ts
CHANGED
|
@@ -40,6 +40,115 @@ export function withFilter(sink: Sink, filter: FilterLike): Sink {
|
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Options for the {@link withBuffer} function.
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
*/
|
|
47
|
+
export 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
|
+
/**
|
|
56
|
+
* The maximum time in milliseconds to wait before flushing buffered records
|
|
57
|
+
* to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
|
|
58
|
+
* negative to disable time-based flushing.
|
|
59
|
+
* @default `5000`
|
|
60
|
+
*/
|
|
61
|
+
flushInterval?: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Turns a sink into a buffered sink. The returned sink buffers log records
|
|
66
|
+
* in memory and flushes them to the underlying sink when the buffer is full
|
|
67
|
+
* or after a specified time interval.
|
|
68
|
+
*
|
|
69
|
+
* @example Buffer a console sink with custom options
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const sink = withBuffer(getConsoleSink(), {
|
|
72
|
+
* bufferSize: 5,
|
|
73
|
+
* flushInterval: 1000
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @param sink A sink to be buffered.
|
|
78
|
+
* @param options Options for the buffered sink.
|
|
79
|
+
* @returns A buffered sink that flushes records periodically.
|
|
80
|
+
* @since 1.0.0
|
|
81
|
+
*/
|
|
82
|
+
export function withBuffer(
|
|
83
|
+
sink: Sink,
|
|
84
|
+
options: BufferSinkOptions = {},
|
|
85
|
+
): Sink & AsyncDisposable {
|
|
86
|
+
const bufferSize = options.bufferSize ?? 10;
|
|
87
|
+
const flushInterval = options.flushInterval ?? 5000;
|
|
88
|
+
|
|
89
|
+
const buffer: LogRecord[] = [];
|
|
90
|
+
let flushTimer: ReturnType<typeof setTimeout> | null = null;
|
|
91
|
+
let disposed = false;
|
|
92
|
+
|
|
93
|
+
function flush(): void {
|
|
94
|
+
if (buffer.length === 0) return;
|
|
95
|
+
|
|
96
|
+
const records = buffer.splice(0);
|
|
97
|
+
for (const record of records) {
|
|
98
|
+
try {
|
|
99
|
+
sink(record);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
// Errors are handled by the sink infrastructure
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (flushTimer !== null) {
|
|
107
|
+
clearTimeout(flushTimer);
|
|
108
|
+
flushTimer = null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function scheduleFlush(): void {
|
|
113
|
+
if (flushInterval <= 0 || flushTimer !== null || disposed) return;
|
|
114
|
+
|
|
115
|
+
flushTimer = setTimeout(() => {
|
|
116
|
+
flushTimer = null;
|
|
117
|
+
flush();
|
|
118
|
+
}, flushInterval);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const bufferedSink: Sink & AsyncDisposable = (record: LogRecord) => {
|
|
122
|
+
if (disposed) return;
|
|
123
|
+
|
|
124
|
+
buffer.push(record);
|
|
125
|
+
|
|
126
|
+
if (buffer.length >= bufferSize) {
|
|
127
|
+
flush();
|
|
128
|
+
} else {
|
|
129
|
+
scheduleFlush();
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
bufferedSink[Symbol.asyncDispose] = async () => {
|
|
134
|
+
disposed = true;
|
|
135
|
+
if (flushTimer !== null) {
|
|
136
|
+
clearTimeout(flushTimer);
|
|
137
|
+
flushTimer = null;
|
|
138
|
+
}
|
|
139
|
+
flush();
|
|
140
|
+
|
|
141
|
+
// Dispose the underlying sink if it's disposable
|
|
142
|
+
if (Symbol.asyncDispose in sink) {
|
|
143
|
+
await (sink as AsyncDisposable)[Symbol.asyncDispose]();
|
|
144
|
+
} else if (Symbol.dispose in sink) {
|
|
145
|
+
(sink as Disposable)[Symbol.dispose]();
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return bufferedSink;
|
|
150
|
+
}
|
|
151
|
+
|
|
43
152
|
/**
|
|
44
153
|
* Options for the {@link getStreamSink} function.
|
|
45
154
|
*/
|