@logtape/logtape 1.0.0-dev.208 → 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 +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 +31 -0
- package/dist/sink.d.cts +32 -1
- package/dist/sink.d.cts.map +1 -1
- package/dist/sink.d.ts +32 -1
- package/dist/sink.d.ts.map +1 -1
- package/dist/sink.js +31 -1
- package/dist/sink.js.map +1 -1
- package/mod.ts +2 -0
- package/package.json +1 -1
- package/sink.test.ts +201 -0
- package/sink.ts +46 -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;
|
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 { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withBuffer, 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, 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 };
|
|
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 { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withBuffer, 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, 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 };
|
|
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, withBuffer, 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, withBuffer, 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
|
@@ -151,8 +151,39 @@ function getConsoleSink(options = {}) {
|
|
|
151
151
|
} else console[method](...args);
|
|
152
152
|
};
|
|
153
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
|
+
}
|
|
154
184
|
|
|
155
185
|
//#endregion
|
|
186
|
+
exports.fromAsyncSink = fromAsyncSink;
|
|
156
187
|
exports.getConsoleSink = getConsoleSink;
|
|
157
188
|
exports.getStreamSink = getStreamSink;
|
|
158
189
|
exports.withBuffer = withBuffer;
|
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.
|
|
@@ -146,6 +156,27 @@ interface ConsoleSinkOptions {
|
|
|
146
156
|
* @returns A sink that logs to the console.
|
|
147
157
|
*/
|
|
148
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;
|
|
149
180
|
//#endregion
|
|
150
|
-
export { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
181
|
+
export { AsyncSink, BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
151
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.
|
|
@@ -146,6 +156,27 @@ interface ConsoleSinkOptions {
|
|
|
146
156
|
* @returns A sink that logs to the console.
|
|
147
157
|
*/
|
|
148
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;
|
|
149
180
|
//#endregion
|
|
150
|
-
export { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
181
|
+
export { AsyncSink, BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
151
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
|
@@ -151,7 +151,37 @@ function getConsoleSink(options = {}) {
|
|
|
151
151
|
} else console[method](...args);
|
|
152
152
|
};
|
|
153
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
|
+
}
|
|
154
184
|
|
|
155
185
|
//#endregion
|
|
156
|
-
export { getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
186
|
+
export { fromAsyncSink, getConsoleSink, getStreamSink, withBuffer, withFilter };
|
|
157
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","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"}
|
|
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":"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"}
|
package/mod.ts
CHANGED
|
@@ -44,8 +44,10 @@ 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,
|
|
47
48
|
type BufferSinkOptions,
|
|
48
49
|
type ConsoleSinkOptions,
|
|
50
|
+
fromAsyncSink,
|
|
49
51
|
getConsoleSink,
|
|
50
52
|
getStreamSink,
|
|
51
53
|
type Sink,
|
package/package.json
CHANGED
package/sink.test.ts
CHANGED
|
@@ -9,6 +9,8 @@ import { defaultTextFormatter } from "./formatter.ts";
|
|
|
9
9
|
import type { LogLevel } from "./level.ts";
|
|
10
10
|
import type { LogRecord } from "./record.ts";
|
|
11
11
|
import {
|
|
12
|
+
type AsyncSink,
|
|
13
|
+
fromAsyncSink,
|
|
12
14
|
getConsoleSink,
|
|
13
15
|
getStreamSink,
|
|
14
16
|
type Sink,
|
|
@@ -635,3 +637,202 @@ test("withBuffer() - edge case: concurrent dispose and log calls", async () => {
|
|
|
635
637
|
assertEquals(buffer.length, 1);
|
|
636
638
|
assertEquals(buffer, [trace]);
|
|
637
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.
|
|
@@ -276,3 +287,38 @@ export function getConsoleSink(options: ConsoleSinkOptions = {}): Sink {
|
|
|
276
287
|
}
|
|
277
288
|
};
|
|
278
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
|
+
}
|