@logtape/logtape 2.1.1 → 2.1.3
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/dist/config.cjs +3 -2
- package/dist/config.d.cts.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +3 -2
- package/dist/config.js.map +1 -1
- package/dist/filter.cjs +12 -7
- package/dist/filter.d.cts.map +1 -1
- package/dist/filter.d.ts.map +1 -1
- package/dist/filter.js +12 -7
- package/dist/filter.js.map +1 -1
- package/dist/sink.cjs +56 -10
- package/dist/sink.d.cts.map +1 -1
- package/dist/sink.d.ts.map +1 -1
- package/dist/sink.js +56 -10
- package/dist/sink.js.map +1 -1
- package/package.json +1 -1
package/dist/config.cjs
CHANGED
|
@@ -33,7 +33,8 @@ const asyncSinkDisposables = /* @__PURE__ */ new Set();
|
|
|
33
33
|
* Check if a config is for the meta logger.
|
|
34
34
|
*/
|
|
35
35
|
function isLoggerConfigMeta(cfg) {
|
|
36
|
-
|
|
36
|
+
const category = Array.isArray(cfg.category) ? cfg.category : [cfg.category];
|
|
37
|
+
return category.length === 0 || category.length === 1 && category[0] === "logtape" || category.length === 2 && category[0] === "logtape" && category[1] === "meta";
|
|
37
38
|
}
|
|
38
39
|
function registerDisposeHook(allowAsync) {
|
|
39
40
|
const handler = allowAsync ? dispose : disposeSync;
|
|
@@ -330,7 +331,7 @@ var ConfigError = class extends Error {
|
|
|
330
331
|
*/
|
|
331
332
|
constructor(message) {
|
|
332
333
|
super(message);
|
|
333
|
-
this.name = "
|
|
334
|
+
this.name = "ConfigError";
|
|
334
335
|
}
|
|
335
336
|
};
|
|
336
337
|
|
package/dist/config.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.cts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AASA;AAAuB,UAAN,MAAM,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CA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,CAAA,GAAS;;;;;;iBAUf,SAAA,CAAA;;;;iBAgBM,OAAA,CAAA,GAAW;;;;;;iBA8BjB,WAAA,CAAA;;;;cA8FH,WAAA,SAAoB,KAAK"}
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AASA;AAAuB,UAAN,MAAM,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;;EAUK,KAAE,EALrB,MAKqB,CALd,OAKc,EALL,IAKK,CAAA;EAAU;;;;EAKjB,OAMqB,CAAA,EAXhC,MAWgC,CAXzB,SAWyB,EAXd,UAWc,CAAA;EAAM;AAAP;AAW3C;EAA6B,OAAA,EAjBlB,YAiBkB,CAjBL,OAiBK,EAjBI,SAiBJ,CAAA,EAAA;EAAA;;;AAoCL;
|
|
1
|
+
{"version":3,"file":"config.d.ts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AASA;AAAuB,UAAN,MAAM,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;;EAUK,KAAE,EALrB,MAKqB,CALd,OAKc,EALL,IAKK,CAAA;EAAU;;;;EAKjB,OAMqB,CAAA,EAXhC,MAWgC,CAXzB,SAWyB,EAXd,UAWc,CAAA;EAAM;AAAP;AAW3C;EAA6B,OAAA,EAjBlB,YAiBkB,CAjBL,OAiBK,EAjBI,SAiBJ,CAAA,EAAA;EAAA;;;AAoCL;EAwHF,mBAAS,CAAA,EAvKP,mBAuKO,CAvKa,MAuKb,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;EAAA;;;EAGI,KAAzB,CAAA,EAAA,OAAA;;AAAoC;AAgD9C;;AACiB,UAhNA,YAgNA,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAO;;AAAR;AA4HhB;EAOsB,QAAK,EAAA,MAAA,GAAI,MAAA,EAAO;EAUtB;AAgBhB;AA8BA;EA8Fa,KAAA,CAAA,EA5dH,OA4de,EAAA;;;;;;;;;;;;;;YA5cb;;;;;;gBAOI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAwHM,oEAGZ,OAAO,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgDvB,wEACN,OAAO,SAAS;;;;;iBA4HV,SAAA,CAAA,GAAa;;;;iBAOP,KAAA,CAAA,GAAS;;;;;;iBAUf,SAAA,CAAA;;;;iBAgBM,OAAA,CAAA,GAAW;;;;;;iBA8BjB,WAAA,CAAA;;;;cA8FH,WAAA,SAAoB,KAAK"}
|
package/dist/config.js
CHANGED
|
@@ -33,7 +33,8 @@ const asyncSinkDisposables = /* @__PURE__ */ new Set();
|
|
|
33
33
|
* Check if a config is for the meta logger.
|
|
34
34
|
*/
|
|
35
35
|
function isLoggerConfigMeta(cfg) {
|
|
36
|
-
|
|
36
|
+
const category = Array.isArray(cfg.category) ? cfg.category : [cfg.category];
|
|
37
|
+
return category.length === 0 || category.length === 1 && category[0] === "logtape" || category.length === 2 && category[0] === "logtape" && category[1] === "meta";
|
|
37
38
|
}
|
|
38
39
|
function registerDisposeHook(allowAsync) {
|
|
39
40
|
const handler = allowAsync ? dispose : disposeSync;
|
|
@@ -330,7 +331,7 @@ var ConfigError = class extends Error {
|
|
|
330
331
|
*/
|
|
331
332
|
constructor(message) {
|
|
332
333
|
super(message);
|
|
333
|
-
this.name = "
|
|
334
|
+
this.name = "ConfigError";
|
|
334
335
|
}
|
|
335
336
|
};
|
|
336
337
|
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","names":["currentConfig: Config<string, string> | null","strongRefs: Set<LoggerImpl>","filterDisposables: Set<Disposable>","sinkDisposables: Set<Disposable>","asyncFilterDisposables: Set<AsyncDisposable>","asyncSinkDisposables: Set<AsyncDisposable>","cfg: LoggerConfig<TSinkId, TFilterId>","allowAsync: boolean","config: Config<TSinkId, TFilterId>","errors: unknown[]","disposables: Set<Disposable>","disposables: Set<AsyncDisposable>","promises: PromiseLike<void>[]","promises: readonly PromiseLike<void>[]","errors: readonly unknown[]","message: string"],"sources":["../src/config.ts"],"sourcesContent":["import type { ContextLocalStorage } from \"./context.ts\";\nimport { type FilterLike, toFilter } from \"./filter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport { LoggerImpl } from \"./logger.ts\";\nimport { getConsoleSink, type Sink } from \"./sink.ts\";\n\n/**\n * A configuration for the loggers.\n */\nexport interface Config<TSinkId extends string, TFilterId extends string> {\n /**\n * The sinks to use. The keys are the sink identifiers, and the values are\n * {@link Sink}s.\n */\n sinks: Record<TSinkId, Sink>;\n /**\n * The filters to use. The keys are the filter identifiers, and the values\n * are either {@link Filter}s or {@link LogLevel}s.\n */\n filters?: Record<TFilterId, FilterLike>;\n\n /**\n * The loggers to configure.\n */\n loggers: LoggerConfig<TSinkId, TFilterId>[];\n\n /**\n * The context-local storage to use for implicit contexts.\n * @since 0.7.0\n */\n contextLocalStorage?: ContextLocalStorage<Record<string, unknown>>;\n\n /**\n * Whether to reset the configuration before applying this one.\n */\n reset?: boolean;\n}\n\n/**\n * A logger configuration.\n */\nexport interface LoggerConfig<\n TSinkId extends string,\n TFilterId extends string,\n> {\n /**\n * The category of the logger. If a string, it is equivalent to an array\n * with one element.\n */\n category: string | string[];\n\n /**\n * The sink identifiers to use.\n */\n sinks?: TSinkId[];\n\n /**\n * Whether to inherit the parent's sinks. If `inherit`, the parent's sinks\n * are used along with the specified sinks. If `override`, the parent's\n * sinks are not used, and only the specified sinks are used.\n *\n * The default is `inherit`.\n * @default `\"inherit\"\n * @since 0.6.0\n */\n parentSinks?: \"inherit\" | \"override\";\n\n /**\n * The filter identifiers to use.\n */\n filters?: TFilterId[];\n\n /**\n * The lowest log level to accept. If `null`, the logger will reject all\n * records.\n * @since 0.8.0\n */\n lowestLevel?: LogLevel | null;\n}\n\n/**\n * The current configuration, if any. Otherwise, `null`.\n */\nlet currentConfig: Config<string, string> | null = null;\n\n/**\n * Strong references to the loggers.\n * This is to prevent the loggers from being garbage collected so that their\n * sinks and filters are not removed.\n */\nconst strongRefs: Set<LoggerImpl> = new Set();\n\n/**\n * Sync filter disposables to dispose when resetting the configuration.\n */\nconst filterDisposables: Set<Disposable> = new Set();\n\n/**\n * Sync sink disposables to dispose when resetting the configuration.\n */\nconst sinkDisposables: Set<Disposable> = new Set();\n\n/**\n * Async filter disposables to dispose when resetting the configuration.\n */\nconst asyncFilterDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Async sink disposables to dispose when resetting the configuration.\n */\nconst asyncSinkDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Check if a config is for the meta logger.\n */\nfunction isLoggerConfigMeta<TSinkId extends string, TFilterId extends string>(\n cfg: LoggerConfig<TSinkId, TFilterId>,\n): boolean {\n return cfg.category.length === 0 ||\n (cfg.category.length === 1 && cfg.category[0] === \"logtape\") ||\n (cfg.category.length === 2 &&\n cfg.category[0] === \"logtape\" &&\n cfg.category[1] === \"meta\");\n}\n\nfunction registerDisposeHook(allowAsync: boolean): void {\n const handler = allowAsync ? dispose : disposeSync;\n\n if (\n // deno-lint-ignore no-explicit-any\n typeof (globalThis as any).EdgeRuntime !== \"string\" &&\n \"process\" in globalThis &&\n !(\"Deno\" in globalThis)\n ) {\n // deno-lint-ignore no-explicit-any\n const proc = (globalThis as any).process;\n // Use bracket notation to avoid static analysis detection in Edge Runtime.\n const onMethod = proc?.[\"on\"];\n if (typeof onMethod === \"function\") {\n onMethod.call(proc, \"exit\", handler);\n return;\n }\n }\n\n // Some edge runtimes expose neither process.on() nor addEventListener().\n // In those environments users can still call dispose()/disposeSync() manually.\n // deno-lint-ignore no-explicit-any\n const addEventListenerMethod = (globalThis as any).addEventListener;\n if (typeof addEventListenerMethod !== \"function\") return;\n\n if (\"Deno\" in globalThis) {\n addEventListenerMethod.call(globalThis, \"unload\", handler);\n } else {\n addEventListenerMethod.call(globalThis, \"pagehide\", handler);\n }\n}\n\n/**\n * Configure the loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * @example\n * ```typescript\n * await configure({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * filters: {\n * slow: (log) =>\n * \"duration\" in log.properties &&\n * log.properties.duration as number > 1000,\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: [\"my-app\", \"sql\"],\n * filters: [\"slow\"],\n * lowestLevel: \"debug\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n */\nexport async function configure<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>): Promise<void> {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n await reset();\n try {\n configureInternal(config, true);\n } catch (e) {\n if (e instanceof ConfigError) await reset();\n throw e;\n }\n}\n\n/**\n * Configure sync loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * Also note that passing async sinks or filters will throw. If\n * necessary use {@link resetSync} or {@link disposeSync}.\n *\n * @example\n * ```typescript\n * configureSync({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n * @since 0.9.0\n */\nexport function configureSync<TSinkId extends string, TFilterId extends string>(\n config: Config<TSinkId, TFilterId>,\n): void {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n if (asyncFilterDisposables.size > 0 || asyncSinkDisposables.size > 0) {\n throw new ConfigError(\n \"Previously configured async disposables are still active. \" +\n \"Use configure() instead or explicitly dispose them using dispose().\",\n );\n }\n resetSync();\n try {\n configureInternal(config, false);\n } catch (e) {\n if (e instanceof ConfigError) resetSync();\n throw e;\n }\n}\n\nfunction configureInternal<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>, allowAsync: boolean): void {\n currentConfig = config;\n\n let metaConfigured = false;\n const configuredCategories = new Set<string>();\n\n for (const cfg of config.loggers) {\n if (isLoggerConfigMeta(cfg)) {\n metaConfigured = true;\n }\n\n // Check for duplicate logger categories\n const categoryKey = Array.isArray(cfg.category)\n ? JSON.stringify(cfg.category)\n : JSON.stringify([cfg.category]);\n if (configuredCategories.has(categoryKey)) {\n throw new ConfigError(\n `Duplicate logger configuration for category: ${categoryKey}. ` +\n `Each category can only be configured once.`,\n );\n }\n configuredCategories.add(categoryKey);\n\n const logger = LoggerImpl.getLogger(cfg.category);\n for (const sinkId of cfg.sinks ?? []) {\n const sink = config.sinks[sinkId];\n if (!sink) {\n throw new ConfigError(`Sink not found: ${sinkId}.`);\n }\n logger.sinks.push(sink);\n }\n logger.parentSinks = cfg.parentSinks ?? \"inherit\";\n if (cfg.lowestLevel !== undefined) {\n logger.lowestLevel = cfg.lowestLevel;\n }\n for (const filterId of cfg.filters ?? []) {\n const filter = config.filters?.[filterId];\n if (filter === undefined) {\n throw new ConfigError(`Filter not found: ${filterId}.`);\n }\n logger.filters.push(toFilter(filter));\n }\n strongRefs.add(logger);\n }\n\n LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;\n\n for (const sink of Object.values<Sink>(config.sinks)) {\n if (Symbol.asyncDispose in sink) {\n if (allowAsync) asyncSinkDisposables.add(sink as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in sink) sinkDisposables.add(sink as Disposable);\n }\n\n for (const filter of Object.values<FilterLike>(config.filters ?? {})) {\n if (filter == null || typeof filter === \"string\") continue;\n if (Symbol.asyncDispose in filter) {\n if (allowAsync) asyncFilterDisposables.add(filter as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n asyncSinkDisposables.delete(filter as AsyncDisposable);\n }\n if (Symbol.dispose in filter) {\n filterDisposables.add(filter as Disposable);\n sinkDisposables.delete(filter as Disposable);\n }\n }\n\n registerDisposeHook(allowAsync);\n const meta = LoggerImpl.getLogger([\"logtape\", \"meta\"]);\n if (!metaConfigured) {\n meta.sinks.push(getConsoleSink());\n }\n\n meta.info(\n \"LogTape loggers are configured. Note that LogTape itself uses the meta \" +\n \"logger, which has category {metaLoggerCategory}. The meta logger is \" +\n \"used to log internal diagnostics such as sink exceptions. \" +\n \"It's recommended to configure the meta logger with a separate sink \" +\n \"so that you can easily notice if logging itself fails or is \" +\n \"misconfigured. To turn off this message, configure the meta logger \" +\n \"with higher log levels than {dismissLevel}. See also \" +\n \"<https://logtape.org/manual/categories#meta-logger>.\",\n { metaLoggerCategory: [\"logtape\", \"meta\"], dismissLevel: \"info\" },\n );\n}\n\n/**\n * Get the current configuration, if any. Otherwise, `null`.\n * @returns The current configuration, if any. Otherwise, `null`.\n */\nexport function getConfig(): Config<string, string> | null {\n return currentConfig;\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes.\n */\nexport async function reset(): Promise<void> {\n await dispose();\n resetInternal();\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes. Will not clear async\n * sinks, only use with sync sinks. Use {@link reset} if you have async sinks.\n * @since 0.9.0\n */\nexport function resetSync(): void {\n disposeSync();\n resetInternal();\n}\n\nfunction resetInternal(): void {\n const rootLogger = LoggerImpl.getLogger([]);\n rootLogger.resetDescendants();\n delete rootLogger.contextLocalStorage;\n strongRefs.clear();\n currentConfig = null;\n}\n\n/**\n * Dispose of the disposables.\n */\nexport async function dispose(): Promise<void> {\n const errors: unknown[] = [];\n try {\n disposeSyncFilters();\n } catch (error) {\n errors.push(error);\n }\n try {\n await disposeAsyncFilters();\n } catch (error) {\n errors.push(error);\n }\n try {\n disposeSyncSinks();\n } catch (error) {\n errors.push(error);\n }\n try {\n await disposeAsyncSinks();\n } catch (error) {\n errors.push(error);\n }\n throwDisposeErrors(errors);\n}\n\n/**\n * Dispose of the sync disposables. Async disposables will be untouched,\n * use {@link dispose} if you have async sinks.\n * @since 0.9.0\n */\nexport function disposeSync(): void {\n const errors: unknown[] = [];\n try {\n disposeSyncFilters();\n } catch (error) {\n errors.push(error);\n }\n try {\n disposeSyncSinks();\n } catch (error) {\n errors.push(error);\n }\n throwDisposeErrors(errors);\n}\n\nfunction disposeSyncFilters(): void {\n disposeSyncDisposables(filterDisposables);\n}\n\nfunction disposeSyncSinks(): void {\n disposeSyncDisposables(sinkDisposables);\n}\n\nfunction disposeSyncDisposables(disposables: Set<Disposable>): void {\n const errors: unknown[] = [];\n try {\n for (const disposable of disposables) {\n try {\n disposable[Symbol.dispose]();\n } catch (error) {\n errors.push(error);\n } finally {\n disposables.delete(disposable);\n }\n }\n } finally {\n disposables.clear();\n }\n throwDisposeErrors(errors);\n}\n\nasync function disposeAsyncFilters(): Promise<void> {\n await disposeAsyncDisposables(asyncFilterDisposables);\n}\n\nasync function disposeAsyncSinks(): Promise<void> {\n await disposeAsyncDisposables(asyncSinkDisposables);\n}\n\nasync function disposeAsyncDisposables(\n disposables: Set<AsyncDisposable>,\n): Promise<void> {\n const promises: PromiseLike<void>[] = [];\n try {\n for (const disposable of disposables) {\n try {\n promises.push(Promise.resolve(disposable[Symbol.asyncDispose]()));\n } catch (error) {\n promises.push(Promise.reject(error));\n } finally {\n disposables.delete(disposable);\n }\n }\n } finally {\n disposables.clear();\n }\n await settleDisposePromises(promises);\n}\n\nasync function settleDisposePromises(\n promises: readonly PromiseLike<void>[],\n): Promise<void> {\n const results = await Promise.allSettled(promises);\n throwDisposeErrors(\n results\n .filter((result): result is PromiseRejectedResult =>\n result.status === \"rejected\"\n )\n .map((result) => result.reason),\n );\n}\n\nfunction throwDisposeErrors(errors: readonly unknown[]): void {\n if (errors.length < 1) return;\n if (errors.length === 1) throw errors[0];\n throw new AggregateError(\n errors,\n \"Multiple errors occurred while disposing LogTape resources.\",\n );\n}\n\n/**\n * A configuration error.\n */\nexport class ConfigError extends Error {\n /**\n * Constructs a new configuration error.\n * @param message The error message.\n */\n constructor(message: string) {\n super(message);\n this.name = \"ConfigureError\";\n }\n}\n"],"mappings":";;;;;;;;AAmFA,IAAIA,gBAA+C;;;;;;AAOnD,MAAMC,6BAA8B,IAAI;;;;AAKxC,MAAMC,oCAAqC,IAAI;;;;AAK/C,MAAMC,kCAAmC,IAAI;;;;AAK7C,MAAMC,yCAA+C,IAAI;;;;AAKzD,MAAMC,uCAA6C,IAAI;;;;AAKvD,SAAS,mBACPC,KACS;AACT,QAAO,IAAI,SAAS,WAAW,KAC5B,IAAI,SAAS,WAAW,KAAK,IAAI,SAAS,OAAO,aACjD,IAAI,SAAS,WAAW,KACvB,IAAI,SAAS,OAAO,aACpB,IAAI,SAAS,OAAO;AACzB;AAED,SAAS,oBAAoBC,YAA2B;CACtD,MAAM,UAAU,aAAa,UAAU;AAEvC,YAEU,WAAmB,gBAAgB,YAC3C,aAAa,gBACX,UAAU,aACZ;EAEA,MAAM,OAAQ,WAAmB;EAEjC,MAAM,WAAW,OAAO;AACxB,aAAW,aAAa,YAAY;AAClC,YAAS,KAAK,MAAM,QAAQ,QAAQ;AACpC;EACD;CACF;CAKD,MAAM,yBAA0B,WAAmB;AACnD,YAAW,2BAA2B,WAAY;AAElD,KAAI,UAAU,WACZ,wBAAuB,KAAK,YAAY,UAAU,QAAQ;KAE1D,wBAAuB,KAAK,YAAY,YAAY,QAAQ;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCD,eAAsB,UAGpBC,QAA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O,eAAe,CAAC,CAAC;EAClE,SAAQ,OAAO;AACd,YAAS,KAAK,QAAQ,OAAO,MAAM,CAAC;EACrC,UAAS;AACR,eAAY,OAAO,WAAW;EAC/B;CAEJ,UAAS;AACR,cAAY,OAAO;CACpB;AACD,OAAM,sBAAsB,SAAS;AACtC;AAED,eAAe,sBACbC,UACe;CACf,MAAM,UAAU,MAAM,QAAQ,WAAW,SAAS;AAClD,oBACE,QACG,OAAO,CAAC,WACP,OAAO,WAAW,WACnB,CACA,IAAI,CAAC,WAAW,OAAO,OAAO,CAClC;AACF;AAED,SAAS,mBAAmBC,QAAkC;AAC5D,KAAI,OAAO,SAAS,EAAG;AACvB,KAAI,OAAO,WAAW,EAAG,OAAM,OAAO;AACtC,OAAM,IAAI,eACR,QACA;AAEH;;;;AAKD,IAAa,cAAb,cAAiC,MAAM;;;;;CAKrC,YAAYC,SAAiB;AAC3B,QAAM,QAAQ;AACd,OAAK,OAAO;CACb;AACF"}
|
|
1
|
+
{"version":3,"file":"config.js","names":["currentConfig: Config<string, string> | null","strongRefs: Set<LoggerImpl>","filterDisposables: Set<Disposable>","sinkDisposables: Set<Disposable>","asyncFilterDisposables: Set<AsyncDisposable>","asyncSinkDisposables: Set<AsyncDisposable>","cfg: LoggerConfig<TSinkId, TFilterId>","allowAsync: boolean","config: Config<TSinkId, TFilterId>","errors: unknown[]","disposables: Set<Disposable>","disposables: Set<AsyncDisposable>","promises: PromiseLike<void>[]","promises: readonly PromiseLike<void>[]","errors: readonly unknown[]","message: string"],"sources":["../src/config.ts"],"sourcesContent":["import type { ContextLocalStorage } from \"./context.ts\";\nimport { type FilterLike, toFilter } from \"./filter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport { LoggerImpl } from \"./logger.ts\";\nimport { getConsoleSink, type Sink } from \"./sink.ts\";\n\n/**\n * A configuration for the loggers.\n */\nexport interface Config<TSinkId extends string, TFilterId extends string> {\n /**\n * The sinks to use. The keys are the sink identifiers, and the values are\n * {@link Sink}s.\n */\n sinks: Record<TSinkId, Sink>;\n /**\n * The filters to use. The keys are the filter identifiers, and the values\n * are either {@link Filter}s or {@link LogLevel}s.\n */\n filters?: Record<TFilterId, FilterLike>;\n\n /**\n * The loggers to configure.\n */\n loggers: LoggerConfig<TSinkId, TFilterId>[];\n\n /**\n * The context-local storage to use for implicit contexts.\n * @since 0.7.0\n */\n contextLocalStorage?: ContextLocalStorage<Record<string, unknown>>;\n\n /**\n * Whether to reset the configuration before applying this one.\n */\n reset?: boolean;\n}\n\n/**\n * A logger configuration.\n */\nexport interface LoggerConfig<\n TSinkId extends string,\n TFilterId extends string,\n> {\n /**\n * The category of the logger. If a string, it is equivalent to an array\n * with one element.\n */\n category: string | string[];\n\n /**\n * The sink identifiers to use.\n */\n sinks?: TSinkId[];\n\n /**\n * Whether to inherit the parent's sinks. If `inherit`, the parent's sinks\n * are used along with the specified sinks. If `override`, the parent's\n * sinks are not used, and only the specified sinks are used.\n *\n * The default is `inherit`.\n * @default `\"inherit\"\n * @since 0.6.0\n */\n parentSinks?: \"inherit\" | \"override\";\n\n /**\n * The filter identifiers to use.\n */\n filters?: TFilterId[];\n\n /**\n * The lowest log level to accept. If `null`, the logger will reject all\n * records.\n * @since 0.8.0\n */\n lowestLevel?: LogLevel | null;\n}\n\n/**\n * The current configuration, if any. Otherwise, `null`.\n */\nlet currentConfig: Config<string, string> | null = null;\n\n/**\n * Strong references to the loggers.\n * This is to prevent the loggers from being garbage collected so that their\n * sinks and filters are not removed.\n */\nconst strongRefs: Set<LoggerImpl> = new Set();\n\n/**\n * Sync filter disposables to dispose when resetting the configuration.\n */\nconst filterDisposables: Set<Disposable> = new Set();\n\n/**\n * Sync sink disposables to dispose when resetting the configuration.\n */\nconst sinkDisposables: Set<Disposable> = new Set();\n\n/**\n * Async filter disposables to dispose when resetting the configuration.\n */\nconst asyncFilterDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Async sink disposables to dispose when resetting the configuration.\n */\nconst asyncSinkDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Check if a config is for the meta logger.\n */\nfunction isLoggerConfigMeta<TSinkId extends string, TFilterId extends string>(\n cfg: LoggerConfig<TSinkId, TFilterId>,\n): boolean {\n const category = Array.isArray(cfg.category) ? cfg.category : [cfg.category];\n return category.length === 0 ||\n (category.length === 1 && category[0] === \"logtape\") ||\n (category.length === 2 &&\n category[0] === \"logtape\" &&\n category[1] === \"meta\");\n}\n\nfunction registerDisposeHook(allowAsync: boolean): void {\n const handler = allowAsync ? dispose : disposeSync;\n\n if (\n // deno-lint-ignore no-explicit-any\n typeof (globalThis as any).EdgeRuntime !== \"string\" &&\n \"process\" in globalThis &&\n !(\"Deno\" in globalThis)\n ) {\n // deno-lint-ignore no-explicit-any\n const proc = (globalThis as any).process;\n // Use bracket notation to avoid static analysis detection in Edge Runtime.\n const onMethod = proc?.[\"on\"];\n if (typeof onMethod === \"function\") {\n onMethod.call(proc, \"exit\", handler);\n return;\n }\n }\n\n // Some edge runtimes expose neither process.on() nor addEventListener().\n // In those environments users can still call dispose()/disposeSync() manually.\n // deno-lint-ignore no-explicit-any\n const addEventListenerMethod = (globalThis as any).addEventListener;\n if (typeof addEventListenerMethod !== \"function\") return;\n\n if (\"Deno\" in globalThis) {\n addEventListenerMethod.call(globalThis, \"unload\", handler);\n } else {\n addEventListenerMethod.call(globalThis, \"pagehide\", handler);\n }\n}\n\n/**\n * Configure the loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * @example\n * ```typescript\n * await configure({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * filters: {\n * slow: (log) =>\n * \"duration\" in log.properties &&\n * log.properties.duration as number > 1000,\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: [\"my-app\", \"sql\"],\n * filters: [\"slow\"],\n * lowestLevel: \"debug\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n */\nexport async function configure<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>): Promise<void> {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n await reset();\n try {\n configureInternal(config, true);\n } catch (e) {\n if (e instanceof ConfigError) await reset();\n throw e;\n }\n}\n\n/**\n * Configure sync loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * Also note that passing async sinks or filters will throw. If\n * necessary use {@link resetSync} or {@link disposeSync}.\n *\n * @example\n * ```typescript\n * configureSync({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n * @since 0.9.0\n */\nexport function configureSync<TSinkId extends string, TFilterId extends string>(\n config: Config<TSinkId, TFilterId>,\n): void {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n if (asyncFilterDisposables.size > 0 || asyncSinkDisposables.size > 0) {\n throw new ConfigError(\n \"Previously configured async disposables are still active. \" +\n \"Use configure() instead or explicitly dispose them using dispose().\",\n );\n }\n resetSync();\n try {\n configureInternal(config, false);\n } catch (e) {\n if (e instanceof ConfigError) resetSync();\n throw e;\n }\n}\n\nfunction configureInternal<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>, allowAsync: boolean): void {\n currentConfig = config;\n\n let metaConfigured = false;\n const configuredCategories = new Set<string>();\n\n for (const cfg of config.loggers) {\n if (isLoggerConfigMeta(cfg)) {\n metaConfigured = true;\n }\n\n // Check for duplicate logger categories\n const categoryKey = Array.isArray(cfg.category)\n ? JSON.stringify(cfg.category)\n : JSON.stringify([cfg.category]);\n if (configuredCategories.has(categoryKey)) {\n throw new ConfigError(\n `Duplicate logger configuration for category: ${categoryKey}. ` +\n `Each category can only be configured once.`,\n );\n }\n configuredCategories.add(categoryKey);\n\n const logger = LoggerImpl.getLogger(cfg.category);\n for (const sinkId of cfg.sinks ?? []) {\n const sink = config.sinks[sinkId];\n if (!sink) {\n throw new ConfigError(`Sink not found: ${sinkId}.`);\n }\n logger.sinks.push(sink);\n }\n logger.parentSinks = cfg.parentSinks ?? \"inherit\";\n if (cfg.lowestLevel !== undefined) {\n logger.lowestLevel = cfg.lowestLevel;\n }\n for (const filterId of cfg.filters ?? []) {\n const filter = config.filters?.[filterId];\n if (filter === undefined) {\n throw new ConfigError(`Filter not found: ${filterId}.`);\n }\n logger.filters.push(toFilter(filter));\n }\n strongRefs.add(logger);\n }\n\n LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;\n\n for (const sink of Object.values<Sink>(config.sinks)) {\n if (Symbol.asyncDispose in sink) {\n if (allowAsync) asyncSinkDisposables.add(sink as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in sink) sinkDisposables.add(sink as Disposable);\n }\n\n for (const filter of Object.values<FilterLike>(config.filters ?? {})) {\n if (filter == null || typeof filter === \"string\") continue;\n if (Symbol.asyncDispose in filter) {\n if (allowAsync) asyncFilterDisposables.add(filter as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n asyncSinkDisposables.delete(filter as AsyncDisposable);\n }\n if (Symbol.dispose in filter) {\n filterDisposables.add(filter as Disposable);\n sinkDisposables.delete(filter as Disposable);\n }\n }\n\n registerDisposeHook(allowAsync);\n const meta = LoggerImpl.getLogger([\"logtape\", \"meta\"]);\n if (!metaConfigured) {\n meta.sinks.push(getConsoleSink());\n }\n\n meta.info(\n \"LogTape loggers are configured. Note that LogTape itself uses the meta \" +\n \"logger, which has category {metaLoggerCategory}. The meta logger is \" +\n \"used to log internal diagnostics such as sink exceptions. \" +\n \"It's recommended to configure the meta logger with a separate sink \" +\n \"so that you can easily notice if logging itself fails or is \" +\n \"misconfigured. To turn off this message, configure the meta logger \" +\n \"with higher log levels than {dismissLevel}. See also \" +\n \"<https://logtape.org/manual/categories#meta-logger>.\",\n { metaLoggerCategory: [\"logtape\", \"meta\"], dismissLevel: \"info\" },\n );\n}\n\n/**\n * Get the current configuration, if any. Otherwise, `null`.\n * @returns The current configuration, if any. Otherwise, `null`.\n */\nexport function getConfig(): Config<string, string> | null {\n return currentConfig;\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes.\n */\nexport async function reset(): Promise<void> {\n await dispose();\n resetInternal();\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes. Will not clear async\n * sinks, only use with sync sinks. Use {@link reset} if you have async sinks.\n * @since 0.9.0\n */\nexport function resetSync(): void {\n disposeSync();\n resetInternal();\n}\n\nfunction resetInternal(): void {\n const rootLogger = LoggerImpl.getLogger([]);\n rootLogger.resetDescendants();\n delete rootLogger.contextLocalStorage;\n strongRefs.clear();\n currentConfig = null;\n}\n\n/**\n * Dispose of the disposables.\n */\nexport async function dispose(): Promise<void> {\n const errors: unknown[] = [];\n try {\n disposeSyncFilters();\n } catch (error) {\n errors.push(error);\n }\n try {\n await disposeAsyncFilters();\n } catch (error) {\n errors.push(error);\n }\n try {\n disposeSyncSinks();\n } catch (error) {\n errors.push(error);\n }\n try {\n await disposeAsyncSinks();\n } catch (error) {\n errors.push(error);\n }\n throwDisposeErrors(errors);\n}\n\n/**\n * Dispose of the sync disposables. Async disposables will be untouched,\n * use {@link dispose} if you have async sinks.\n * @since 0.9.0\n */\nexport function disposeSync(): void {\n const errors: unknown[] = [];\n try {\n disposeSyncFilters();\n } catch (error) {\n errors.push(error);\n }\n try {\n disposeSyncSinks();\n } catch (error) {\n errors.push(error);\n }\n throwDisposeErrors(errors);\n}\n\nfunction disposeSyncFilters(): void {\n disposeSyncDisposables(filterDisposables);\n}\n\nfunction disposeSyncSinks(): void {\n disposeSyncDisposables(sinkDisposables);\n}\n\nfunction disposeSyncDisposables(disposables: Set<Disposable>): void {\n const errors: unknown[] = [];\n try {\n for (const disposable of disposables) {\n try {\n disposable[Symbol.dispose]();\n } catch (error) {\n errors.push(error);\n } finally {\n disposables.delete(disposable);\n }\n }\n } finally {\n disposables.clear();\n }\n throwDisposeErrors(errors);\n}\n\nasync function disposeAsyncFilters(): Promise<void> {\n await disposeAsyncDisposables(asyncFilterDisposables);\n}\n\nasync function disposeAsyncSinks(): Promise<void> {\n await disposeAsyncDisposables(asyncSinkDisposables);\n}\n\nasync function disposeAsyncDisposables(\n disposables: Set<AsyncDisposable>,\n): Promise<void> {\n const promises: PromiseLike<void>[] = [];\n try {\n for (const disposable of disposables) {\n try {\n promises.push(Promise.resolve(disposable[Symbol.asyncDispose]()));\n } catch (error) {\n promises.push(Promise.reject(error));\n } finally {\n disposables.delete(disposable);\n }\n }\n } finally {\n disposables.clear();\n }\n await settleDisposePromises(promises);\n}\n\nasync function settleDisposePromises(\n promises: readonly PromiseLike<void>[],\n): Promise<void> {\n const results = await Promise.allSettled(promises);\n throwDisposeErrors(\n results\n .filter((result): result is PromiseRejectedResult =>\n result.status === \"rejected\"\n )\n .map((result) => result.reason),\n );\n}\n\nfunction throwDisposeErrors(errors: readonly unknown[]): void {\n if (errors.length < 1) return;\n if (errors.length === 1) throw errors[0];\n throw new AggregateError(\n errors,\n \"Multiple errors occurred while disposing LogTape resources.\",\n );\n}\n\n/**\n * A configuration error.\n */\nexport class ConfigError extends Error {\n /**\n * Constructs a new configuration error.\n * @param message The error message.\n */\n constructor(message: string) {\n super(message);\n this.name = \"ConfigError\";\n }\n}\n"],"mappings":";;;;;;;;AAmFA,IAAIA,gBAA+C;;;;;;AAOnD,MAAMC,6BAA8B,IAAI;;;;AAKxC,MAAMC,oCAAqC,IA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AI,OAAO,WAAW,EAAG,OAAM,OAAO;AACtC,OAAM,IAAI,eACR,QACA;AAEH;;;;AAKD,IAAa,cAAb,cAAiC,MAAM;;;;;CAKrC,YAAYC,SAAiB;AAC3B,QAAM,QAAQ;AACd,OAAK,OAAO;CACb;AACF"}
|
package/dist/filter.cjs
CHANGED
|
@@ -53,6 +53,7 @@ function getThrottlingFilter(options) {
|
|
|
53
53
|
const maxKeys = options.maxKeys === void 0 ? 1e3 : options.maxKeys;
|
|
54
54
|
const buckets = /* @__PURE__ */ new Map();
|
|
55
55
|
const summaryRecord = Symbol.for("LogTape.throttlingSummaryRecord");
|
|
56
|
+
let accessCounter = 0;
|
|
56
57
|
let emittingSummary = false;
|
|
57
58
|
if (mode !== "fixed" && mode !== "sliding") throw new TypeError(`Invalid throttling mode: ${String(mode)}.`);
|
|
58
59
|
if (timeSource !== "clock" && timeSource !== "record") throw new TypeError(`Invalid throttling timeSource: ${String(timeSource)}.`);
|
|
@@ -72,13 +73,12 @@ function getThrottlingFilter(options) {
|
|
|
72
73
|
suppressed: 0,
|
|
73
74
|
firstRecord: record,
|
|
74
75
|
lastRecord: record,
|
|
75
|
-
lastTime: now
|
|
76
|
+
lastTime: now,
|
|
77
|
+
lastAccess: ++accessCounter
|
|
76
78
|
};
|
|
77
79
|
buckets.set(key, bucket);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
buckets.set(key, bucket);
|
|
81
|
-
}
|
|
80
|
+
evictKeysIfNeeded(now);
|
|
81
|
+
} else bucket.lastAccess = ++accessCounter;
|
|
82
82
|
if (mode === "fixed") return filterFixedWindow(key, bucket, record, now);
|
|
83
83
|
return filterSlidingWindow(key, bucket, record, now);
|
|
84
84
|
};
|
|
@@ -141,8 +141,13 @@ function getThrottlingFilter(options) {
|
|
|
141
141
|
}
|
|
142
142
|
function evictKeysIfNeeded(now) {
|
|
143
143
|
if (maxKeys == null) return;
|
|
144
|
-
while (buckets.size
|
|
145
|
-
|
|
144
|
+
while (buckets.size > maxKeys) {
|
|
145
|
+
let keyToEvict;
|
|
146
|
+
let oldestAccess = Infinity;
|
|
147
|
+
for (const [key, bucket$1] of buckets) if (bucket$1.lastAccess < oldestAccess) {
|
|
148
|
+
keyToEvict = key;
|
|
149
|
+
oldestAccess = bucket$1.lastAccess;
|
|
150
|
+
}
|
|
146
151
|
if (keyToEvict === void 0) return;
|
|
147
152
|
const bucket = buckets.get(keyToEvict);
|
|
148
153
|
if (bucket != null) {
|
package/dist/filter.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filter.d.cts","names":[],"sources":["../src/filter.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAQA;;;;AAuCgC,KA/CpB,MAAA,GA+CoB,CAAA,MAAA,EA/CF,SA+CE,EAAA,GAAA,OAAA;AAUhC;;;;AAGsB;AAStB;AAA+C,UA7D9B,uBAAA,CA6D8B;EAAA;;;EAYP,SAAK,GAAA,EAAA,MAAA;EAAQ;AAQb;AAQxC;EAAwC,SAAA,UAAA,EAAA,MAAA;EAAA;;AAsDW;EAOvC,SAAA,OAAU,EAAA,MAAA;EAAA;;;EAAoB,SAAA,MAAA,EAAA,QAAA,GAAA,UAAA,GAAA,SAAA;EAQ1B;;;EAA2B,SAAG,SAAA,EAAA,MAAA;EAAM;AAYpD;;EAA8B,SAAQ,OAAA,EAAA,MAAA;EAAQ;AAAgB;
|
|
1
|
+
{"version":3,"file":"filter.d.cts","names":[],"sources":["../src/filter.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAQA;;;;AAuCgC,KA/CpB,MAAA,GA+CoB,CAAA,MAAA,EA/CF,SA+CE,EAAA,GAAA,OAAA;AAUhC;;;;AAGsB;AAStB;AAA+C,UA7D9B,uBAAA,CA6D8B;EAAA;;;EAYP,SAAK,GAAA,EAAA,MAAA;EAAQ;AAQb;AAQxC;EAAwC,SAAA,UAAA,EAAA,MAAA;EAAA;;AAsDW;EAOvC,SAAA,OAAU,EAAA,MAAA;EAAA;;;EAAoB,SAAA,MAAA,EAAA,QAAA,GAAA,UAAA,GAAA,SAAA;EAQ1B;;;EAA2B,SAAG,SAAA,EAAA,MAAA;EAAM;AAYpD;;EAA8B,SAAQ,OAAA,EAAA,MAAA;EAAQ;AAAgB;AAsD9D;EAAmC,SAAA,WAAA,EA9LX,SA8LW;EAAA;;;EAEb,SAAA,UAAA,EA3LC,SA2LD;;;;;;;;;KAjLV,uBAAA,wBACS,0CAEL;;;;;;UASC,8BAAA;;;;mBAIE;;;;;mBAOb,sBACW,4BAA4B;;;;;yCAQ5B;;;;;;;UAQA,uBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0BAyCS;;;;;;;;;;;qBAaL;;;;;;KAOT,UAAA,GAAa,SAAS;;;;;;;iBAQlB,QAAA,SAAiB,aAAa;;;;;;;;iBAY9B,cAAA,QAAsB,kBAAkB;;;;;;;;;;;;;;iBAsDxC,mBAAA,UACL,0BACR,SAAS"}
|
package/dist/filter.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filter.d.ts","names":[],"sources":["../src/filter.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAQA;;;;AAuCgC,KA/CpB,MAAA,GA+CoB,CAAA,MAAA,EA/CF,SA+CE,EAAA,GAAA,OAAA;AAUhC;;;;AAGsB;AAStB;AAA+C,UA7D9B,uBAAA,CA6D8B;EAAA;;;EAYP,SAAK,GAAA,EAAA,MAAA;EAAQ;AAQb;AAQxC;EAAwC,SAAA,UAAA,EAAA,MAAA;EAAA;;AAsDW;EAOvC,SAAA,OAAU,EAAA,MAAA;EAAA;;;EAAoB,SAAA,MAAA,EAAA,QAAA,GAAA,UAAA,GAAA,SAAA;EAQ1B;;;EAA2B,SAAG,SAAA,EAAA,MAAA;EAAM;AAYpD;;EAA8B,SAAQ,OAAA,EAAA,MAAA;EAAQ;AAAgB;
|
|
1
|
+
{"version":3,"file":"filter.d.ts","names":[],"sources":["../src/filter.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAQA;;;;AAuCgC,KA/CpB,MAAA,GA+CoB,CAAA,MAAA,EA/CF,SA+CE,EAAA,GAAA,OAAA;AAUhC;;;;AAGsB;AAStB;AAA+C,UA7D9B,uBAAA,CA6D8B;EAAA;;;EAYP,SAAK,GAAA,EAAA,MAAA;EAAQ;AAQb;AAQxC;EAAwC,SAAA,UAAA,EAAA,MAAA;EAAA;;AAsDW;EAOvC,SAAA,OAAU,EAAA,MAAA;EAAA;;;EAAoB,SAAA,MAAA,EAAA,QAAA,GAAA,UAAA,GAAA,SAAA;EAQ1B;;;EAA2B,SAAG,SAAA,EAAA,MAAA;EAAM;AAYpD;;EAA8B,SAAQ,OAAA,EAAA,MAAA;EAAQ;AAAgB;AAsD9D;EAAmC,SAAA,WAAA,EA9LX,SA8LW;EAAA;;;EAEb,SAAA,UAAA,EA3LC,SA2LD;;;;;;;;;KAjLV,uBAAA,wBACS,0CAEL;;;;;;UASC,8BAAA;;;;mBAIE;;;;;mBAOb,sBACW,4BAA4B;;;;;yCAQ5B;;;;;;;UAQA,uBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0BAyCS;;;;;;;;;;;qBAaL;;;;;;KAOT,UAAA,GAAa,SAAS;;;;;;;iBAQlB,QAAA,SAAiB,aAAa;;;;;;;;iBAY9B,cAAA,QAAsB,kBAAkB;;;;;;;;;;;;;;iBAsDxC,mBAAA,UACL,0BACR,SAAS"}
|
package/dist/filter.js
CHANGED
|
@@ -52,6 +52,7 @@ function getThrottlingFilter(options) {
|
|
|
52
52
|
const maxKeys = options.maxKeys === void 0 ? 1e3 : options.maxKeys;
|
|
53
53
|
const buckets = /* @__PURE__ */ new Map();
|
|
54
54
|
const summaryRecord = Symbol.for("LogTape.throttlingSummaryRecord");
|
|
55
|
+
let accessCounter = 0;
|
|
55
56
|
let emittingSummary = false;
|
|
56
57
|
if (mode !== "fixed" && mode !== "sliding") throw new TypeError(`Invalid throttling mode: ${String(mode)}.`);
|
|
57
58
|
if (timeSource !== "clock" && timeSource !== "record") throw new TypeError(`Invalid throttling timeSource: ${String(timeSource)}.`);
|
|
@@ -71,13 +72,12 @@ function getThrottlingFilter(options) {
|
|
|
71
72
|
suppressed: 0,
|
|
72
73
|
firstRecord: record,
|
|
73
74
|
lastRecord: record,
|
|
74
|
-
lastTime: now
|
|
75
|
+
lastTime: now,
|
|
76
|
+
lastAccess: ++accessCounter
|
|
75
77
|
};
|
|
76
78
|
buckets.set(key, bucket);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
buckets.set(key, bucket);
|
|
80
|
-
}
|
|
79
|
+
evictKeysIfNeeded(now);
|
|
80
|
+
} else bucket.lastAccess = ++accessCounter;
|
|
81
81
|
if (mode === "fixed") return filterFixedWindow(key, bucket, record, now);
|
|
82
82
|
return filterSlidingWindow(key, bucket, record, now);
|
|
83
83
|
};
|
|
@@ -140,8 +140,13 @@ function getThrottlingFilter(options) {
|
|
|
140
140
|
}
|
|
141
141
|
function evictKeysIfNeeded(now) {
|
|
142
142
|
if (maxKeys == null) return;
|
|
143
|
-
while (buckets.size
|
|
144
|
-
|
|
143
|
+
while (buckets.size > maxKeys) {
|
|
144
|
+
let keyToEvict;
|
|
145
|
+
let oldestAccess = Infinity;
|
|
146
|
+
for (const [key, bucket$1] of buckets) if (bucket$1.lastAccess < oldestAccess) {
|
|
147
|
+
keyToEvict = key;
|
|
148
|
+
oldestAccess = bucket$1.lastAccess;
|
|
149
|
+
}
|
|
145
150
|
if (keyToEvict === void 0) return;
|
|
146
151
|
const bucket = buckets.get(keyToEvict);
|
|
147
152
|
if (bucket != null) {
|
package/dist/filter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filter.js","names":["filter: FilterLike","level: LogLevel | null","record: LogRecord","options: ThrottlingFilterOptions","key: string","bucket: ThrottlingBucket","now: number","acceptedAt: number","reason: ThrottlingFilterSummary[\"reason\"]","endTime: number","summary: ThrottlingFilterSummary","name: string","value: number","rawMessage: string | TemplateStringsArray","parts: readonly string[]","part: string"],"sources":["../src/filter.ts"],"sourcesContent":["import type { LogLevel } from \"./level.ts\";\nimport type { LogRecord } from \"./record.ts\";\n\n/**\n * A filter is a function that accepts a log record and returns `true` if the\n * record should be passed to the sink.\n *\n * @param record The log record to filter.\n * @returns `true` if the record should be passed to the sink.\n */\nexport type Filter = (record: LogRecord) => boolean;\n\n/**\n * Summary information emitted by {@link getThrottlingFilter} when suppressed\n * records are reported.\n *\n * @since 2.1.0\n */\nexport interface ThrottlingFilterSummary {\n /**\n * The throttling key whose records were suppressed.\n */\n readonly key: string;\n\n /**\n * The number of records suppressed since the previous summary for the key.\n */\n readonly suppressed: number;\n\n /**\n * The number of records allowed during the same summary period.\n */\n readonly allowed: number;\n\n /**\n * Why the summary was emitted.\n */\n readonly reason: \"window\" | \"eviction\" | \"dispose\";\n\n /**\n * The time at which the summary period started, in milliseconds.\n */\n readonly startTime: number;\n\n /**\n * The time at which the summary period ended, in milliseconds.\n */\n readonly endTime: number;\n\n /**\n * The first record observed in the summary period.\n */\n readonly firstRecord: LogRecord;\n\n /**\n * The most recent record observed in the summary period.\n */\n readonly lastRecord: LogRecord;\n}\n\n/**\n * A logger-like object used by {@link getThrottlingFilter} to emit summaries.\n *\n * A regular {@link Logger} returned by `getLogger()` satisfies this interface.\n *\n * @since 2.1.0\n */\nexport type ThrottlingSummaryLogger = {\n readonly [Level in LogLevel]?: (\n message: string,\n properties: Record<string, unknown>,\n ) => void;\n};\n\n/**\n * Summary logging options for {@link getThrottlingFilter}.\n *\n * @since 2.1.0\n */\nexport interface ThrottlingFilterSummaryOptions {\n /**\n * The logger used to emit summary records.\n */\n readonly logger: ThrottlingSummaryLogger;\n\n /**\n * The summary log level.\n * @default `\"warning\"`\n */\n readonly level?:\n | LogLevel\n | ((summary: ThrottlingFilterSummary) => LogLevel);\n\n /**\n * The summary message.\n * @default `\"Last log message was suppressed {suppressed} times.\"`\n */\n readonly message?:\n | string\n | ((summary: ThrottlingFilterSummary) => string);\n}\n\n/**\n * Options for {@link getThrottlingFilter}.\n *\n * @since 2.1.0\n */\nexport interface ThrottlingFilterOptions {\n /**\n * Number of records allowed for each key in the configured window.\n */\n readonly limit: number;\n\n /**\n * Window size in milliseconds.\n */\n readonly windowMs: number;\n\n /**\n * Window algorithm.\n *\n * - `\"fixed\"` starts a window when the first record for a key arrives.\n * - `\"sliding\"` counts records accepted during the previous `windowMs`.\n *\n * @default `\"fixed\"`\n */\n readonly mode?: \"fixed\" | \"sliding\";\n\n /**\n * Source of time for window calculations.\n *\n * - `\"clock\"` uses {@link ThrottlingFilterOptions.clock}.\n * - `\"record\"` uses {@link LogRecord.timestamp}.\n *\n * @default `\"clock\"`\n */\n readonly timeSource?: \"clock\" | \"record\";\n\n /**\n * Clock used when {@link timeSource} is `\"clock\"`.\n * @default `Date.now`\n */\n readonly clock?: () => number;\n\n /**\n * Derives a throttling key from a log record. By default, records are keyed\n * by category, level, and raw message template.\n */\n readonly key?: (record: LogRecord) => string;\n\n /**\n * Maximum number of keys tracked by the filter. When the limit is reached,\n * the least recently used key is evicted. Set to `null` to disable the cap.\n *\n * @default `1000`\n */\n readonly maxKeys?: number | null;\n\n /**\n * Summary logging options for suppressed records.\n */\n readonly summary?: ThrottlingFilterSummaryOptions;\n}\n\n/**\n * A filter-like value is either a {@link Filter} or a {@link LogLevel}.\n * `null` is also allowed to represent a filter that rejects all records.\n */\nexport type FilterLike = Filter | LogLevel | null;\n\n/**\n * Converts a {@link FilterLike} value to an actual {@link Filter}.\n *\n * @param filter The filter-like value to convert.\n * @returns The actual filter.\n */\nexport function toFilter(filter: FilterLike): Filter {\n if (typeof filter === \"function\") return filter;\n return getLevelFilter(filter);\n}\n\n/**\n * Returns a filter that accepts log records with the specified level.\n *\n * @param level The level to filter by. If `null`, the filter will reject all\n * records.\n * @returns The filter.\n */\nexport function getLevelFilter(level: LogLevel | null): Filter {\n if (level == null) return () => false;\n if (level === \"fatal\") {\n return (record: LogRecord) => record.level === \"fatal\";\n } else if (level === \"error\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" || record.level === \"error\";\n } else if (level === \"warning\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" ||\n record.level === \"error\" ||\n record.level === \"warning\";\n } else if (level === \"info\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" ||\n record.level === \"error\" ||\n record.level === \"warning\" ||\n record.level === \"info\";\n } else if (level === \"debug\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" ||\n record.level === \"error\" ||\n record.level === \"warning\" ||\n record.level === \"info\" ||\n record.level === \"debug\";\n } else if (level === \"trace\") return () => true;\n throw new TypeError(`Invalid log level: ${level}.`);\n}\n\ninterface ThrottlingBucket {\n windowStart: number;\n summaryStartTime: number;\n readonly acceptedAt: number[];\n allowed: number;\n suppressed: number;\n firstRecord: LogRecord;\n lastRecord: LogRecord;\n lastTime: number;\n}\n\n/**\n * Returns a stateful filter that rate-limits repeated log records.\n *\n * The default key treats records with the same category, level, and raw\n * message template as identical, ignoring substituted values. If summary\n * logging is enabled, the filter logs summaries as a side effect when\n * suppression ends, when a suppressed key is evicted, or when the filter is\n * disposed.\n *\n * @param options Throttling options.\n * @returns A throttling filter.\n * @since 2.1.0\n */\nexport function getThrottlingFilter(\n options: ThrottlingFilterOptions,\n): Filter & Disposable {\n validatePositiveInteger(\"limit\", options.limit);\n validatePositiveNumber(\"windowMs\", options.windowMs);\n if (options.maxKeys != null) {\n validatePositiveInteger(\"maxKeys\", options.maxKeys);\n }\n\n const limit = options.limit;\n const windowMs = options.windowMs;\n const mode = options.mode ?? \"fixed\";\n const timeSource = options.timeSource ?? \"clock\";\n const clock = options.clock ?? Date.now;\n const getKey = options.key ?? getDefaultThrottlingKey;\n const maxKeys = options.maxKeys === undefined ? 1000 : options.maxKeys;\n const buckets = new Map<string, ThrottlingBucket>();\n const summaryRecord = Symbol.for(\"LogTape.throttlingSummaryRecord\");\n let emittingSummary = false;\n\n if (mode !== \"fixed\" && mode !== \"sliding\") {\n throw new TypeError(`Invalid throttling mode: ${String(mode)}.`);\n }\n if (timeSource !== \"clock\" && timeSource !== \"record\") {\n throw new TypeError(\n `Invalid throttling timeSource: ${String(timeSource)}.`,\n );\n }\n\n const filter = ((record: LogRecord): boolean => {\n if (emittingSummary && isSummaryRecord(record)) {\n return true;\n }\n\n const key = getKey(record);\n let bucket = buckets.get(key);\n const observedTime = timeSource === \"record\" ? record.timestamp : clock();\n const now = bucket == null || timeSource === \"record\"\n ? observedTime\n : Math.max(observedTime, bucket.lastTime);\n\n if (bucket == null) {\n evictKeysIfNeeded(now);\n bucket = {\n windowStart: now,\n summaryStartTime: now,\n acceptedAt: [],\n allowed: 0,\n suppressed: 0,\n firstRecord: record,\n lastRecord: record,\n lastTime: now,\n };\n buckets.set(key, bucket);\n } else if (maxKeys != null) {\n buckets.delete(key);\n buckets.set(key, bucket);\n }\n\n if (mode === \"fixed\") {\n return filterFixedWindow(key, bucket, record, now);\n }\n return filterSlidingWindow(key, bucket, record, now);\n }) as Filter & Disposable;\n\n filter[Symbol.dispose] = () => {\n for (const [key, bucket] of buckets) {\n const endTime = timeSource === \"record\"\n ? bucket.lastTime\n : Math.max(clock(), bucket.lastTime);\n emitSummary(key, bucket, \"dispose\", endTime);\n }\n buckets.clear();\n };\n\n return filter;\n\n function filterFixedWindow(\n key: string,\n bucket: ThrottlingBucket,\n record: LogRecord,\n now: number,\n ): boolean {\n if (now - bucket.windowStart >= windowMs) {\n emitSummary(key, bucket, \"window\", now);\n bucket.windowStart = now;\n bucket.summaryStartTime = now;\n bucket.acceptedAt.length = 0;\n bucket.allowed = 0;\n bucket.suppressed = 0;\n bucket.firstRecord = record;\n }\n\n const allowed = bucket.allowed < limit;\n if (allowed) {\n bucket.allowed++;\n } else {\n bucket.suppressed++;\n }\n\n bucket.lastRecord = record;\n bucket.lastTime = now;\n return allowed;\n }\n\n function filterSlidingWindow(\n key: string,\n bucket: ThrottlingBucket,\n record: LogRecord,\n now: number,\n ): boolean {\n pruneExpiredAcceptedAt(bucket, now);\n\n const allowed = bucket.acceptedAt.length < limit;\n if (allowed) {\n if (bucket.suppressed > 0) {\n emitSummary(key, bucket, \"window\", now);\n bucket.allowed = 0;\n bucket.suppressed = 0;\n bucket.summaryStartTime = now;\n bucket.firstRecord = record;\n }\n pushAcceptedAt(bucket, now);\n bucket.allowed++;\n } else {\n bucket.suppressed++;\n }\n\n bucket.lastRecord = record;\n bucket.lastTime = now;\n return allowed;\n }\n\n function pruneExpiredAcceptedAt(\n bucket: ThrottlingBucket,\n now: number,\n ): void {\n let acceptedAtCount = 0;\n for (let i = 0; i < bucket.acceptedAt.length; i++) {\n const acceptedAt: number = bucket.acceptedAt[i];\n if (acceptedAt <= now && now - acceptedAt < windowMs) {\n bucket.acceptedAt[acceptedAtCount] = acceptedAt;\n acceptedAtCount++;\n }\n }\n bucket.acceptedAt.length = acceptedAtCount;\n }\n\n function pushAcceptedAt(bucket: ThrottlingBucket, acceptedAt: number): void {\n bucket.acceptedAt.push(acceptedAt);\n }\n\n function evictKeysIfNeeded(now: number): void {\n if (maxKeys == null) return;\n while (buckets.size >= maxKeys) {\n const keyToEvict = buckets.keys().next().value;\n if (keyToEvict === undefined) return;\n const bucket = buckets.get(keyToEvict);\n if (bucket != null) {\n const endTime = timeSource === \"record\"\n ? bucket.lastTime\n : Math.max(now, bucket.lastTime);\n emitSummary(keyToEvict, bucket, \"eviction\", endTime);\n buckets.delete(keyToEvict);\n }\n }\n }\n\n function emitSummary(\n key: string,\n bucket: ThrottlingBucket,\n reason: ThrottlingFilterSummary[\"reason\"],\n endTime: number,\n ): void {\n const summaryOptions = options.summary;\n if (summaryOptions == null || bucket.suppressed < 1) return;\n\n const summary: ThrottlingFilterSummary = {\n key,\n suppressed: bucket.suppressed,\n allowed: bucket.allowed,\n reason,\n startTime: bucket.summaryStartTime,\n endTime,\n firstRecord: bucket.firstRecord,\n lastRecord: bucket.lastRecord,\n };\n const level = typeof summaryOptions.level === \"function\"\n ? summaryOptions.level(summary)\n : summaryOptions.level ?? \"warning\";\n const message = typeof summaryOptions.message === \"function\"\n ? summaryOptions.message(summary)\n : summaryOptions.message ??\n \"Last log message was suppressed {suppressed} times.\";\n const log = summaryOptions.logger[level];\n if (typeof log !== \"function\") return;\n const properties = {\n key: summary.key,\n suppressed: summary.suppressed,\n allowed: summary.allowed,\n reason: summary.reason,\n startTime: summary.startTime,\n endTime: summary.endTime,\n firstRecord: summary.firstRecord,\n lastRecord: summary.lastRecord,\n [summaryRecord]: true,\n } as Record<string, unknown> & Record<typeof summaryRecord, true>;\n\n emittingSummary = true;\n try {\n log.call(summaryOptions.logger, message, properties);\n } finally {\n emittingSummary = false;\n }\n }\n\n function isSummaryRecord(record: LogRecord): boolean {\n const properties = record.properties;\n return properties != null &&\n typeof properties === \"object\" &&\n (properties as Record<typeof summaryRecord, unknown>)[summaryRecord] ===\n true;\n }\n}\n\nfunction validatePositiveInteger(name: string, value: number): void {\n if (!Number.isInteger(value) || value < 1) {\n throw new RangeError(`${name} must be a positive integer.`);\n }\n}\n\nfunction validatePositiveNumber(name: string, value: number): void {\n if (!Number.isFinite(value) || value <= 0) {\n throw new RangeError(`${name} must be a positive number.`);\n }\n}\n\nfunction getDefaultThrottlingKey(record: LogRecord): string {\n const categoryKey = encodeKeyParts(record.category);\n const rawMessage = getRawMessageTemplate(record.rawMessage);\n const rawMessageKey = typeof rawMessage === \"string\"\n ? `s${encodeKeyPart(rawMessage)}`\n : `t${encodeKeyParts(rawMessage)}`;\n return `c${categoryKey}l${encodeKeyPart(record.level)}r${rawMessageKey}`;\n}\n\nfunction getRawMessageTemplate(\n rawMessage: string | TemplateStringsArray,\n): string | readonly string[] {\n if (typeof rawMessage === \"string\") return rawMessage;\n for (const part of rawMessage) {\n if (typeof part !== \"string\") return rawMessage.raw;\n }\n return rawMessage;\n}\n\nfunction encodeKeyParts(parts: readonly string[]): string {\n let key = `${parts.length}:`;\n for (const part of parts) key += encodeKeyP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M,aAAa,sBAAsB,OAAO,WAAW;CAC3D,MAAM,uBAAuB,eAAe,YACvC,GAAG,cAAc,WAAW,CAAC,KAC7B,GAAG,eAAe,WAAW,CAAC;AACnC,SAAQ,GAAG,YAAY,GAAG,cAAc,OAAO,MAAM,CAAC,GAAG,cAAc;AACxE;AAED,SAAS,sBACPW,YAC4B;AAC5B,YAAW,eAAe,SAAU,QAAO;AAC3C,MAAK,MAAM,QAAQ,WACjB,YAAW,SAAS,SAAU,QAAO,WAAW;AAElD,QAAO;AACR;AAED,SAAS,eAAeC,OAAkC;CACxD,IAAI,OAAO,EAAE,MAAM,OAAO;AAC1B,MAAK,MAAM,QAAQ,MAAO,QAAO,cAAc,KAAK;AACpD,QAAO;AACR;AAED,SAAS,cAAcC,MAAsB;AAC3C,SAAQ,EAAE,KAAK,OAAO,GAAG,KAAK;AAC/B"}
|
|
1
|
+
{"version":3,"file":"filter.js","names":["filter: FilterLike","level: LogLevel | null","record: LogRecord","options: ThrottlingFilterOptions","key: string","bucket: ThrottlingBucket","now: number","acceptedAt: number","keyToEvict: string | undefined","bucket","reason: ThrottlingFilterSummary[\"reason\"]","endTime: number","summary: ThrottlingFilterSummary","name: string","value: number","rawMessage: string | TemplateStringsArray","parts: readonly string[]","part: string"],"sources":["../src/filter.ts"],"sourcesContent":["import type { LogLevel } from \"./level.ts\";\nimport type { LogRecord } from \"./record.ts\";\n\n/**\n * A filter is a function that accepts a log record and returns `true` if the\n * record should be passed to the sink.\n *\n * @param record The log record to filter.\n * @returns `true` if the record should be passed to the sink.\n */\nexport type Filter = (record: LogRecord) => boolean;\n\n/**\n * Summary information emitted by {@link getThrottlingFilter} when suppressed\n * records are reported.\n *\n * @since 2.1.0\n */\nexport interface ThrottlingFilterSummary {\n /**\n * The throttling key whose records were suppressed.\n */\n readonly key: string;\n\n /**\n * The number of records suppressed since the previous summary for the key.\n */\n readonly suppressed: number;\n\n /**\n * The number of records allowed during the same summary period.\n */\n readonly allowed: number;\n\n /**\n * Why the summary was emitted.\n */\n readonly reason: \"window\" | \"eviction\" | \"dispose\";\n\n /**\n * The time at which the summary period started, in milliseconds.\n */\n readonly startTime: number;\n\n /**\n * The time at which the summary period ended, in milliseconds.\n */\n readonly endTime: number;\n\n /**\n * The first record observed in the summary period.\n */\n readonly firstRecord: LogRecord;\n\n /**\n * The most recent record observed in the summary period.\n */\n readonly lastRecord: LogRecord;\n}\n\n/**\n * A logger-like object used by {@link getThrottlingFilter} to emit summaries.\n *\n * A regular {@link Logger} returned by `getLogger()` satisfies this interface.\n *\n * @since 2.1.0\n */\nexport type ThrottlingSummaryLogger = {\n readonly [Level in LogLevel]?: (\n message: string,\n properties: Record<string, unknown>,\n ) => void;\n};\n\n/**\n * Summary logging options for {@link getThrottlingFilter}.\n *\n * @since 2.1.0\n */\nexport interface ThrottlingFilterSummaryOptions {\n /**\n * The logger used to emit summary records.\n */\n readonly logger: ThrottlingSummaryLogger;\n\n /**\n * The summary log level.\n * @default `\"warning\"`\n */\n readonly level?:\n | LogLevel\n | ((summary: ThrottlingFilterSummary) => LogLevel);\n\n /**\n * The summary message.\n * @default `\"Last log message was suppressed {suppressed} times.\"`\n */\n readonly message?:\n | string\n | ((summary: ThrottlingFilterSummary) => string);\n}\n\n/**\n * Options for {@link getThrottlingFilter}.\n *\n * @since 2.1.0\n */\nexport interface ThrottlingFilterOptions {\n /**\n * Number of records allowed for each key in the configured window.\n */\n readonly limit: number;\n\n /**\n * Window size in milliseconds.\n */\n readonly windowMs: number;\n\n /**\n * Window algorithm.\n *\n * - `\"fixed\"` starts a window when the first record for a key arrives.\n * - `\"sliding\"` counts records accepted during the previous `windowMs`.\n *\n * @default `\"fixed\"`\n */\n readonly mode?: \"fixed\" | \"sliding\";\n\n /**\n * Source of time for window calculations.\n *\n * - `\"clock\"` uses {@link ThrottlingFilterOptions.clock}.\n * - `\"record\"` uses {@link LogRecord.timestamp}.\n *\n * @default `\"clock\"`\n */\n readonly timeSource?: \"clock\" | \"record\";\n\n /**\n * Clock used when {@link timeSource} is `\"clock\"`.\n * @default `Date.now`\n */\n readonly clock?: () => number;\n\n /**\n * Derives a throttling key from a log record. By default, records are keyed\n * by category, level, and raw message template.\n */\n readonly key?: (record: LogRecord) => string;\n\n /**\n * Maximum number of keys tracked by the filter. When the limit is reached,\n * the least recently used key is evicted. Set to `null` to disable the cap.\n *\n * @default `1000`\n */\n readonly maxKeys?: number | null;\n\n /**\n * Summary logging options for suppressed records.\n */\n readonly summary?: ThrottlingFilterSummaryOptions;\n}\n\n/**\n * A filter-like value is either a {@link Filter} or a {@link LogLevel}.\n * `null` is also allowed to represent a filter that rejects all records.\n */\nexport type FilterLike = Filter | LogLevel | null;\n\n/**\n * Converts a {@link FilterLike} value to an actual {@link Filter}.\n *\n * @param filter The filter-like value to convert.\n * @returns The actual filter.\n */\nexport function toFilter(filter: FilterLike): Filter {\n if (typeof filter === \"function\") return filter;\n return getLevelFilter(filter);\n}\n\n/**\n * Returns a filter that accepts log records with the specified level.\n *\n * @param level The level to filter by. If `null`, the filter will reject all\n * records.\n * @returns The filter.\n */\nexport function getLevelFilter(level: LogLevel | null): Filter {\n if (level == null) return () => false;\n if (level === \"fatal\") {\n return (record: LogRecord) => record.level === \"fatal\";\n } else if (level === \"error\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" || record.level === \"error\";\n } else if (level === \"warning\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" ||\n record.level === \"error\" ||\n record.level === \"warning\";\n } else if (level === \"info\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" ||\n record.level === \"error\" ||\n record.level === \"warning\" ||\n record.level === \"info\";\n } else if (level === \"debug\") {\n return (record: LogRecord) =>\n record.level === \"fatal\" ||\n record.level === \"error\" ||\n record.level === \"warning\" ||\n record.level === \"info\" ||\n record.level === \"debug\";\n } else if (level === \"trace\") return () => true;\n throw new TypeError(`Invalid log level: ${level}.`);\n}\n\ninterface ThrottlingBucket {\n windowStart: number;\n summaryStartTime: number;\n readonly acceptedAt: number[];\n allowed: number;\n suppressed: number;\n firstRecord: LogRecord;\n lastRecord: LogRecord;\n lastTime: number;\n lastAccess: number;\n}\n\n/**\n * Returns a stateful filter that rate-limits repeated log records.\n *\n * The default key treats records with the same category, level, and raw\n * message template as identical, ignoring substituted values. If summary\n * logging is enabled, the filter logs summaries as a side effect when\n * suppression ends, when a suppressed key is evicted, or when the filter is\n * disposed.\n *\n * @param options Throttling options.\n * @returns A throttling filter.\n * @since 2.1.0\n */\nexport function getThrottlingFilter(\n options: ThrottlingFilterOptions,\n): Filter & Disposable {\n validatePositiveInteger(\"limit\", options.limit);\n validatePositiveNumber(\"windowMs\", options.windowMs);\n if (options.maxKeys != null) {\n validatePositiveInteger(\"maxKeys\", options.maxKeys);\n }\n\n const limit = options.limit;\n const windowMs = options.windowMs;\n const mode = options.mode ?? \"fixed\";\n const timeSource = options.timeSource ?? \"clock\";\n const clock = options.clock ?? Date.now;\n const getKey = options.key ?? getDefaultThrottlingKey;\n const maxKeys = options.maxKeys === undefined ? 1000 : options.maxKeys;\n const buckets = new Map<string, ThrottlingBucket>();\n const summaryRecord = Symbol.for(\"LogTape.throttlingSummaryRecord\");\n let accessCounter = 0;\n let emittingSummary = false;\n\n if (mode !== \"fixed\" && mode !== \"sliding\") {\n throw new TypeError(`Invalid throttling mode: ${String(mode)}.`);\n }\n if (timeSource !== \"clock\" && timeSource !== \"record\") {\n throw new TypeError(\n `Invalid throttling timeSource: ${String(timeSource)}.`,\n );\n }\n\n const filter = ((record: LogRecord): boolean => {\n if (emittingSummary && isSummaryRecord(record)) {\n return true;\n }\n\n const key = getKey(record);\n let bucket = buckets.get(key);\n const observedTime = timeSource === \"record\" ? record.timestamp : clock();\n const now = bucket == null || timeSource === \"record\"\n ? observedTime\n : Math.max(observedTime, bucket.lastTime);\n\n if (bucket == null) {\n evictKeysIfNeeded(now);\n bucket = {\n windowStart: now,\n summaryStartTime: now,\n acceptedAt: [],\n allowed: 0,\n suppressed: 0,\n firstRecord: record,\n lastRecord: record,\n lastTime: now,\n lastAccess: ++accessCounter,\n };\n buckets.set(key, bucket);\n evictKeysIfNeeded(now);\n } else {\n bucket.lastAccess = ++accessCounter;\n }\n\n if (mode === \"fixed\") {\n return filterFixedWindow(key, bucket, record, now);\n }\n return filterSlidingWindow(key, bucket, record, now);\n }) as Filter & Disposable;\n\n filter[Symbol.dispose] = () => {\n for (const [key, bucket] of buckets) {\n const endTime = timeSource === \"record\"\n ? bucket.lastTime\n : Math.max(clock(), bucket.lastTime);\n emitSummary(key, bucket, \"dispose\", endTime);\n }\n buckets.clear();\n };\n\n return filter;\n\n function filterFixedWindow(\n key: string,\n bucket: ThrottlingBucket,\n record: LogRecord,\n now: number,\n ): boolean {\n if (now - bucket.windowStart >= windowMs) {\n emitSummary(key, bucket, \"window\", now);\n bucket.windowStart = now;\n bucket.summaryStartTime = now;\n bucket.acceptedAt.length = 0;\n bucket.allowed = 0;\n bucket.suppressed = 0;\n bucket.firstRecord = record;\n }\n\n const allowed = bucket.allowed < limit;\n if (allowed) {\n bucket.allowed++;\n } else {\n bucket.suppressed++;\n }\n\n bucket.lastRecord = record;\n bucket.lastTime = now;\n return allowed;\n }\n\n function filterSlidingWindow(\n key: string,\n bucket: ThrottlingBucket,\n record: LogRecord,\n now: number,\n ): boolean {\n pruneExpiredAcceptedAt(bucket, now);\n\n const allowed = bucket.acceptedAt.length < limit;\n if (allowed) {\n if (bucket.suppressed > 0) {\n emitSummary(key, bucket, \"window\", now);\n bucket.allowed = 0;\n bucket.suppressed = 0;\n bucket.summaryStartTime = now;\n bucket.firstRecord = record;\n }\n pushAcceptedAt(bucket, now);\n bucket.allowed++;\n } else {\n bucket.suppressed++;\n }\n\n bucket.lastRecord = record;\n bucket.lastTime = now;\n return allowed;\n }\n\n function pruneExpiredAcceptedAt(\n bucket: ThrottlingBucket,\n now: number,\n ): void {\n let acceptedAtCount = 0;\n for (let i = 0; i < bucket.acceptedAt.length; i++) {\n const acceptedAt: number = bucket.acceptedAt[i];\n if (acceptedAt <= now && now - acceptedAt < windowMs) {\n bucket.acceptedAt[acceptedAtCount] = acceptedAt;\n acceptedAtCount++;\n }\n }\n bucket.acceptedAt.length = acceptedAtCount;\n }\n\n function pushAcceptedAt(bucket: ThrottlingBucket, acceptedAt: number): void {\n bucket.acceptedAt.push(acceptedAt);\n }\n\n function evictKeysIfNeeded(now: number): void {\n if (maxKeys == null) return;\n while (buckets.size > maxKeys) {\n let keyToEvict: string | undefined;\n let oldestAccess = Infinity;\n for (const [key, bucket] of buckets) {\n if (bucket.lastAccess < oldestAccess) {\n keyToEvict = key;\n oldestAccess = bucket.lastAccess;\n }\n }\n if (keyToEvict === undefined) return;\n const bucket = buckets.get(keyToEvict);\n if (bucket != null) {\n const endTime = timeSource === \"record\"\n ? bucket.lastTime\n : Math.max(now, bucket.lastTime);\n emitSummary(keyToEvict, bucket, \"eviction\", endTime);\n buckets.delete(keyToEvict);\n }\n }\n }\n\n function emitSummary(\n key: string,\n bucket: ThrottlingBucket,\n reason: ThrottlingFilterSummary[\"reason\"],\n endTime: number,\n ): void {\n const summaryOptions = options.summary;\n if (summaryOptions == null || bucket.suppressed < 1) return;\n\n const summary: ThrottlingFilterSummary = {\n key,\n suppressed: bucket.suppressed,\n allowed: bucket.allowed,\n reason,\n startTime: bucket.summaryStartTime,\n endTime,\n firstRecord: bucket.firstRecord,\n lastRecord: bucket.lastRecord,\n };\n const level = typeof summaryOptions.level === \"function\"\n ? summaryOptions.level(summary)\n : summaryOptions.level ?? \"warning\";\n const message = typeof summaryOptions.message === \"function\"\n ? summaryOptions.message(summary)\n : summaryOptions.message ??\n \"Last log message was suppressed {suppressed} times.\";\n const log = summaryOptions.logger[level];\n if (typeof log !== \"function\") return;\n const properties = {\n key: summary.key,\n suppressed: summary.suppressed,\n allowed: summary.allowed,\n reason: summary.reason,\n startTime: summary.startTime,\n endTime: summary.endTime,\n firstRecord: summary.firstRecord,\n lastRecord: summary.lastRecord,\n [summaryRecord]: true,\n } as Record<string, unknown> & Record<typeof summaryRecord, true>;\n\n emittingSummary = true;\n try {\n log.call(summaryOptions.logger, message, properties);\n } finally {\n emittingSummary = false;\n }\n }\n\n function isSummaryRecord(record: LogRecord): boolean {\n const properties = record.properties;\n return properties != null &&\n typeof properties === \"object\" &&\n (properties as Record<typeof summaryRecord, unknown>)[summaryRecord] ===\n true;\n }\n}\n\nfunction validatePositiveInteger(name: string, value: number): void {\n if (!Number.isInteger(value) || value < 1) {\n throw new RangeError(`${name} must be a positive integer.`);\n }\n}\n\nfunction validatePositiveNumber(name: string, value: number): void {\n if (!Number.isFinite(value) || value <= 0) {\n throw new RangeError(`${name} must be a positive number.`);\n }\n}\n\nfunction getDefaultThrottlingKey(record: LogRecord): string {\n const categoryKey = encodeKeyParts(record.category);\n const rawMessage = getRawMessageTemplate(record.rawMessage);\n const rawMessageKey = typeof rawMessage === \"string\"\n ? `s${encodeKeyPart(rawMessage)}`\n : `t${encodeKeyParts(rawMessage)}`;\n return `c${categoryKey}l${encodeKeyPart(record.level)}r${rawMessageKey}`;\n}\n\nfunction getRawMessageTemplate(\n rawMessage: string | TemplateStringsArray,\n): string | readonly string[] {\n if (typeof rawMessage === \"string\") return rawMessage;\n for (const part of rawMessage) {\n if (typeof part !== \"string\") return rawMessage.raw;\n }\n return rawMessage;\n}\n\nfunction encodeKeyParts(parts: readonly string[]): string {\n let key = `${parts.length}:`;\n for (const part of parts) key += encodeKeyPart(part);\n return key;\n}\n\nfunction encodeKeyPart(part: string): string {\n return `${part.length}:${part}`;\n}\n"],"mappings":";;;;;;;AAgLA,SAAgB,SAASA,QAA4B;AACnD,YAAW,WAAW,WAAY,QAAO;AACzC,QAAO,eAAe,OAAO;AAC9B;;;;;;;;AASD,SAAgB,eAAeC,OAAgC;AAC7D,KAAI,SAAS,KAAM,QAAO,MAAM;AAChC,KAAI,UAAU,QACZ,QAAO,CAACC,WAAsB,OAAO,UAAU;UACtC,UAAU,QACnB,QAAO,CAACA,WACN,OAAO,UAAU,WAAW,OAAO,UAAU;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,QAAQ,WACjB,YAAW,SAAS,SAAU,QAAO,WAAW;AAElD,QAAO;AACR;AAED,SAAS,eAAeC,OAAkC;CACxD,IAAI,OAAO,EAAE,MAAM,OAAO;AAC1B,MAAK,MAAM,QAAQ,MAAO,QAAO,cAAc,KAAK;AACpD,QAAO;AACR;AAED,SAAS,cAAcC,MAAsB;AAC3C,SAAQ,EAAE,KAAK,OAAO,GAAG,KAAK;AAC/B"}
|
package/dist/sink.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const require_filter = require('./filter.cjs');
|
|
2
2
|
const require_level = require('./level.cjs');
|
|
3
|
+
const require_logger = require('./logger.cjs');
|
|
3
4
|
const require_formatter = require('./formatter.cjs');
|
|
4
5
|
|
|
5
6
|
//#region src/sink.ts
|
|
@@ -19,9 +20,13 @@ const require_formatter = require('./formatter.cjs');
|
|
|
19
20
|
*/
|
|
20
21
|
function withFilter(sink, filter) {
|
|
21
22
|
const filterFunc = require_filter.toFilter(filter);
|
|
22
|
-
|
|
23
|
+
const filtered = (record) => {
|
|
23
24
|
if (filterFunc(record)) sink(record);
|
|
24
25
|
};
|
|
26
|
+
const disposableSink = sink;
|
|
27
|
+
if (Symbol.dispose in disposableSink) filtered[Symbol.dispose] = disposableSink[Symbol.dispose]?.bind(sink);
|
|
28
|
+
if (Symbol.asyncDispose in disposableSink) filtered[Symbol.asyncDispose] = disposableSink[Symbol.asyncDispose]?.bind(sink);
|
|
29
|
+
return filtered;
|
|
25
30
|
}
|
|
26
31
|
/**
|
|
27
32
|
* A factory that returns a sink that writes to a {@link WritableStream}.
|
|
@@ -146,6 +151,7 @@ function getConsoleSink(options = {}) {
|
|
|
146
151
|
const flushInterval = nonBlockingConfig.flushInterval ?? 100;
|
|
147
152
|
const buffer = [];
|
|
148
153
|
let flushTimer = null;
|
|
154
|
+
let scheduledFlushTimer = null;
|
|
149
155
|
let disposed = false;
|
|
150
156
|
let flushScheduled = false;
|
|
151
157
|
const maxBufferSize = bufferSize * 2;
|
|
@@ -159,7 +165,8 @@ function getConsoleSink(options = {}) {
|
|
|
159
165
|
function scheduleFlush() {
|
|
160
166
|
if (flushScheduled) return;
|
|
161
167
|
flushScheduled = true;
|
|
162
|
-
setTimeout(() => {
|
|
168
|
+
scheduledFlushTimer = setTimeout(() => {
|
|
169
|
+
scheduledFlushTimer = null;
|
|
163
170
|
flushScheduled = false;
|
|
164
171
|
flush();
|
|
165
172
|
}, 0);
|
|
@@ -183,6 +190,11 @@ function getConsoleSink(options = {}) {
|
|
|
183
190
|
clearInterval(flushTimer);
|
|
184
191
|
flushTimer = null;
|
|
185
192
|
}
|
|
193
|
+
if (scheduledFlushTimer !== null) {
|
|
194
|
+
clearTimeout(scheduledFlushTimer);
|
|
195
|
+
scheduledFlushTimer = null;
|
|
196
|
+
flushScheduled = false;
|
|
197
|
+
}
|
|
186
198
|
flush();
|
|
187
199
|
};
|
|
188
200
|
return nonBlockingSink;
|
|
@@ -210,13 +222,41 @@ function getConsoleSink(options = {}) {
|
|
|
210
222
|
function fromAsyncSink(asyncSink) {
|
|
211
223
|
let lastPromise = Promise.resolve();
|
|
212
224
|
const sink = (record) => {
|
|
213
|
-
lastPromise = lastPromise.then(() => asyncSink(record)).catch(() => {
|
|
225
|
+
lastPromise = lastPromise.then(() => asyncSink(record)).catch((error) => {
|
|
226
|
+
try {
|
|
227
|
+
if (_asyncSinkError in record) return;
|
|
228
|
+
const metaLogger = require_logger.LoggerImpl.getLogger(["logtape", "meta"]);
|
|
229
|
+
const errorRecord = {
|
|
230
|
+
category: ["logtape", "meta"],
|
|
231
|
+
level: "error",
|
|
232
|
+
timestamp: Date.now(),
|
|
233
|
+
rawMessage: "Async sink error: {error}",
|
|
234
|
+
message: [
|
|
235
|
+
"Async sink error: ",
|
|
236
|
+
error,
|
|
237
|
+
""
|
|
238
|
+
],
|
|
239
|
+
properties: {
|
|
240
|
+
error,
|
|
241
|
+
sink: asyncSink,
|
|
242
|
+
record
|
|
243
|
+
},
|
|
244
|
+
[_asyncSinkError]: true
|
|
245
|
+
};
|
|
246
|
+
metaLogger.emit(errorRecord, new Set([sink]));
|
|
247
|
+
} catch {}
|
|
248
|
+
});
|
|
214
249
|
};
|
|
215
250
|
sink[Symbol.asyncDispose] = async () => {
|
|
216
|
-
|
|
251
|
+
for (;;) {
|
|
252
|
+
const promise = lastPromise;
|
|
253
|
+
await promise;
|
|
254
|
+
if (promise === lastPromise) break;
|
|
255
|
+
}
|
|
217
256
|
};
|
|
218
257
|
return sink;
|
|
219
258
|
}
|
|
259
|
+
const _asyncSinkError = Symbol.for("logtape.asyncSinkError");
|
|
220
260
|
/**
|
|
221
261
|
* Creates a sink that buffers log records until a trigger level is reached.
|
|
222
262
|
* This pattern, known as "fingers crossed" logging, keeps detailed debug logs
|
|
@@ -334,7 +374,7 @@ function fingersCrossed(sink, options = {}) {
|
|
|
334
374
|
context: contextPart
|
|
335
375
|
};
|
|
336
376
|
}
|
|
337
|
-
function cleanupExpiredBuffers(buffers) {
|
|
377
|
+
function cleanupExpiredBuffers(buffers, triggered) {
|
|
338
378
|
if (!hasTtl) return;
|
|
339
379
|
const now = Date.now();
|
|
340
380
|
const expiredKeys = [];
|
|
@@ -343,7 +383,11 @@ function fingersCrossed(sink, options = {}) {
|
|
|
343
383
|
const lastRecordTimestamp = metadata.buffer[metadata.buffer.length - 1].timestamp;
|
|
344
384
|
if (now - lastRecordTimestamp > bufferTtlMs) expiredKeys.push(key);
|
|
345
385
|
}
|
|
346
|
-
for (const key of expiredKeys)
|
|
386
|
+
for (const key of expiredKeys) {
|
|
387
|
+
buffers.delete(key);
|
|
388
|
+
triggered.delete(key);
|
|
389
|
+
}
|
|
390
|
+
for (const [key, triggeredAt] of triggered) if (now - triggeredAt > bufferTtlMs) triggered.delete(key);
|
|
347
391
|
}
|
|
348
392
|
function evictLruBuffers(buffers, numToEvict) {
|
|
349
393
|
if (!hasLru) return;
|
|
@@ -376,15 +420,16 @@ function fingersCrossed(sink, options = {}) {
|
|
|
376
420
|
};
|
|
377
421
|
} else {
|
|
378
422
|
const buffers = /* @__PURE__ */ new Map();
|
|
379
|
-
const triggered = /* @__PURE__ */ new
|
|
423
|
+
const triggered = /* @__PURE__ */ new Map();
|
|
380
424
|
let accessCounter = 0;
|
|
381
425
|
let cleanupTimer = null;
|
|
382
426
|
if (hasTtl) cleanupTimer = setInterval(() => {
|
|
383
|
-
cleanupExpiredBuffers(buffers);
|
|
427
|
+
cleanupExpiredBuffers(buffers, triggered);
|
|
384
428
|
}, cleanupIntervalMs);
|
|
385
429
|
const fingersCrossedSink = (record) => {
|
|
386
430
|
const bufferKey = getBufferKey(record.category, record.properties);
|
|
387
431
|
if (triggered.has(bufferKey)) {
|
|
432
|
+
triggered.set(bufferKey, Date.now());
|
|
388
433
|
sink(record);
|
|
389
434
|
return;
|
|
390
435
|
}
|
|
@@ -405,17 +450,18 @@ function fingersCrossed(sink, options = {}) {
|
|
|
405
450
|
if (contextMatches && categoryMatches) keysToFlush.add(bufferedKey);
|
|
406
451
|
}
|
|
407
452
|
const allRecordsToFlush = [];
|
|
453
|
+
const triggeredAt = Date.now();
|
|
408
454
|
for (const key of keysToFlush) {
|
|
409
455
|
const metadata = buffers.get(key);
|
|
410
456
|
if (metadata) {
|
|
411
457
|
allRecordsToFlush.push(...metadata.buffer);
|
|
412
458
|
buffers.delete(key);
|
|
413
|
-
triggered.
|
|
459
|
+
triggered.set(key, triggeredAt);
|
|
414
460
|
}
|
|
415
461
|
}
|
|
416
462
|
allRecordsToFlush.sort((a, b) => a.timestamp - b.timestamp);
|
|
417
463
|
for (const bufferedRecord of allRecordsToFlush) sink(bufferedRecord);
|
|
418
|
-
triggered.
|
|
464
|
+
triggered.set(bufferKey, triggeredAt);
|
|
419
465
|
sink(record);
|
|
420
466
|
} else if (bufferLevel != null && require_level.compareLogLevel(record.level, bufferLevel) > 0) sink(record);
|
|
421
467
|
else {
|
package/dist/sink.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0KT,cAAA,OACR,gBACG,wBACR,QAAQ,OAAO"}
|
package/dist/sink.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sink.d.ts","names":[],"sources":["../src/sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"sink.d.ts","names":[],"sources":["../src/sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAoBA;AAWA;;;;AAAsD;AAgBtD;AAA0B,KA3Bd,IAAA,GA2Bc,CAAA,MAAA,EA3BE,SA2BF,EAAA,GAAA,IAAA;;;;AAAsC;AAsBhE;;;;AAS8C;AA+D9B,KA9GJ,SAAA,GA8GiB,CAAA,MAAA,EA9GI,SA8GJ,EAAA,GA9GkB,OA8GlB,CAAA,IAAA,CAAA;;;;;;AAGJ;AAgGxB;AAOD;;;;;;;AA2BY,iBAnOI,UAAA,CAmOJ,IAAA,EAnOqB,IAmOrB,EAAA,MAAA,EAnOmC,UAmOnC,CAAA,EAnOgD,IAmOhD;AAAO;AA8CnB;;AACW,UA5PM,iBAAA,CA4PN;EAAuB;;;EACN,SAAA,CAAA,EAzPd,aAyPc;EAmIZ;;;EAAkC,OAAG,CAAA,EAAA;IAAO,MAAA,CAAA,IAAA,EAAA,MAAA,CAAA,EAvXxB,UAuXwB;EAAe,CAAA;EA2C1D;;;;AAiCgB;AA0KjC;;;;;;;AAG4B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAjjBZ,aAAA,SACN,0BACC,oBACR,OAAO;KAkGL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA8CI,cAAA,WACL,qBACR,QAAQ,OAAO;;;;;;;;;;;;;;;;;;;;;iBAmIF,aAAA,YAAyB,YAAY,OAAO;;;;;UA2C3C,qBAAA;;;;;;;0BAOS;;;;;;;;;;;;;;;;;;;;;;;;;yBA0BD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0KT,cAAA,OACR,gBACG,wBACR,QAAQ,OAAO"}
|
package/dist/sink.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { toFilter } from "./filter.js";
|
|
2
2
|
import { compareLogLevel } from "./level.js";
|
|
3
|
+
import { LoggerImpl } from "./logger.js";
|
|
3
4
|
import { defaultConsoleFormatter, defaultTextFormatter } from "./formatter.js";
|
|
4
5
|
|
|
5
6
|
//#region src/sink.ts
|
|
@@ -19,9 +20,13 @@ import { defaultConsoleFormatter, defaultTextFormatter } from "./formatter.js";
|
|
|
19
20
|
*/
|
|
20
21
|
function withFilter(sink, filter) {
|
|
21
22
|
const filterFunc = toFilter(filter);
|
|
22
|
-
|
|
23
|
+
const filtered = (record) => {
|
|
23
24
|
if (filterFunc(record)) sink(record);
|
|
24
25
|
};
|
|
26
|
+
const disposableSink = sink;
|
|
27
|
+
if (Symbol.dispose in disposableSink) filtered[Symbol.dispose] = disposableSink[Symbol.dispose]?.bind(sink);
|
|
28
|
+
if (Symbol.asyncDispose in disposableSink) filtered[Symbol.asyncDispose] = disposableSink[Symbol.asyncDispose]?.bind(sink);
|
|
29
|
+
return filtered;
|
|
25
30
|
}
|
|
26
31
|
/**
|
|
27
32
|
* A factory that returns a sink that writes to a {@link WritableStream}.
|
|
@@ -146,6 +151,7 @@ function getConsoleSink(options = {}) {
|
|
|
146
151
|
const flushInterval = nonBlockingConfig.flushInterval ?? 100;
|
|
147
152
|
const buffer = [];
|
|
148
153
|
let flushTimer = null;
|
|
154
|
+
let scheduledFlushTimer = null;
|
|
149
155
|
let disposed = false;
|
|
150
156
|
let flushScheduled = false;
|
|
151
157
|
const maxBufferSize = bufferSize * 2;
|
|
@@ -159,7 +165,8 @@ function getConsoleSink(options = {}) {
|
|
|
159
165
|
function scheduleFlush() {
|
|
160
166
|
if (flushScheduled) return;
|
|
161
167
|
flushScheduled = true;
|
|
162
|
-
setTimeout(() => {
|
|
168
|
+
scheduledFlushTimer = setTimeout(() => {
|
|
169
|
+
scheduledFlushTimer = null;
|
|
163
170
|
flushScheduled = false;
|
|
164
171
|
flush();
|
|
165
172
|
}, 0);
|
|
@@ -183,6 +190,11 @@ function getConsoleSink(options = {}) {
|
|
|
183
190
|
clearInterval(flushTimer);
|
|
184
191
|
flushTimer = null;
|
|
185
192
|
}
|
|
193
|
+
if (scheduledFlushTimer !== null) {
|
|
194
|
+
clearTimeout(scheduledFlushTimer);
|
|
195
|
+
scheduledFlushTimer = null;
|
|
196
|
+
flushScheduled = false;
|
|
197
|
+
}
|
|
186
198
|
flush();
|
|
187
199
|
};
|
|
188
200
|
return nonBlockingSink;
|
|
@@ -210,13 +222,41 @@ function getConsoleSink(options = {}) {
|
|
|
210
222
|
function fromAsyncSink(asyncSink) {
|
|
211
223
|
let lastPromise = Promise.resolve();
|
|
212
224
|
const sink = (record) => {
|
|
213
|
-
lastPromise = lastPromise.then(() => asyncSink(record)).catch(() => {
|
|
225
|
+
lastPromise = lastPromise.then(() => asyncSink(record)).catch((error) => {
|
|
226
|
+
try {
|
|
227
|
+
if (_asyncSinkError in record) return;
|
|
228
|
+
const metaLogger = LoggerImpl.getLogger(["logtape", "meta"]);
|
|
229
|
+
const errorRecord = {
|
|
230
|
+
category: ["logtape", "meta"],
|
|
231
|
+
level: "error",
|
|
232
|
+
timestamp: Date.now(),
|
|
233
|
+
rawMessage: "Async sink error: {error}",
|
|
234
|
+
message: [
|
|
235
|
+
"Async sink error: ",
|
|
236
|
+
error,
|
|
237
|
+
""
|
|
238
|
+
],
|
|
239
|
+
properties: {
|
|
240
|
+
error,
|
|
241
|
+
sink: asyncSink,
|
|
242
|
+
record
|
|
243
|
+
},
|
|
244
|
+
[_asyncSinkError]: true
|
|
245
|
+
};
|
|
246
|
+
metaLogger.emit(errorRecord, new Set([sink]));
|
|
247
|
+
} catch {}
|
|
248
|
+
});
|
|
214
249
|
};
|
|
215
250
|
sink[Symbol.asyncDispose] = async () => {
|
|
216
|
-
|
|
251
|
+
for (;;) {
|
|
252
|
+
const promise = lastPromise;
|
|
253
|
+
await promise;
|
|
254
|
+
if (promise === lastPromise) break;
|
|
255
|
+
}
|
|
217
256
|
};
|
|
218
257
|
return sink;
|
|
219
258
|
}
|
|
259
|
+
const _asyncSinkError = Symbol.for("logtape.asyncSinkError");
|
|
220
260
|
/**
|
|
221
261
|
* Creates a sink that buffers log records until a trigger level is reached.
|
|
222
262
|
* This pattern, known as "fingers crossed" logging, keeps detailed debug logs
|
|
@@ -334,7 +374,7 @@ function fingersCrossed(sink, options = {}) {
|
|
|
334
374
|
context: contextPart
|
|
335
375
|
};
|
|
336
376
|
}
|
|
337
|
-
function cleanupExpiredBuffers(buffers) {
|
|
377
|
+
function cleanupExpiredBuffers(buffers, triggered) {
|
|
338
378
|
if (!hasTtl) return;
|
|
339
379
|
const now = Date.now();
|
|
340
380
|
const expiredKeys = [];
|
|
@@ -343,7 +383,11 @@ function fingersCrossed(sink, options = {}) {
|
|
|
343
383
|
const lastRecordTimestamp = metadata.buffer[metadata.buffer.length - 1].timestamp;
|
|
344
384
|
if (now - lastRecordTimestamp > bufferTtlMs) expiredKeys.push(key);
|
|
345
385
|
}
|
|
346
|
-
for (const key of expiredKeys)
|
|
386
|
+
for (const key of expiredKeys) {
|
|
387
|
+
buffers.delete(key);
|
|
388
|
+
triggered.delete(key);
|
|
389
|
+
}
|
|
390
|
+
for (const [key, triggeredAt] of triggered) if (now - triggeredAt > bufferTtlMs) triggered.delete(key);
|
|
347
391
|
}
|
|
348
392
|
function evictLruBuffers(buffers, numToEvict) {
|
|
349
393
|
if (!hasLru) return;
|
|
@@ -376,15 +420,16 @@ function fingersCrossed(sink, options = {}) {
|
|
|
376
420
|
};
|
|
377
421
|
} else {
|
|
378
422
|
const buffers = /* @__PURE__ */ new Map();
|
|
379
|
-
const triggered = /* @__PURE__ */ new
|
|
423
|
+
const triggered = /* @__PURE__ */ new Map();
|
|
380
424
|
let accessCounter = 0;
|
|
381
425
|
let cleanupTimer = null;
|
|
382
426
|
if (hasTtl) cleanupTimer = setInterval(() => {
|
|
383
|
-
cleanupExpiredBuffers(buffers);
|
|
427
|
+
cleanupExpiredBuffers(buffers, triggered);
|
|
384
428
|
}, cleanupIntervalMs);
|
|
385
429
|
const fingersCrossedSink = (record) => {
|
|
386
430
|
const bufferKey = getBufferKey(record.category, record.properties);
|
|
387
431
|
if (triggered.has(bufferKey)) {
|
|
432
|
+
triggered.set(bufferKey, Date.now());
|
|
388
433
|
sink(record);
|
|
389
434
|
return;
|
|
390
435
|
}
|
|
@@ -405,17 +450,18 @@ function fingersCrossed(sink, options = {}) {
|
|
|
405
450
|
if (contextMatches && categoryMatches) keysToFlush.add(bufferedKey);
|
|
406
451
|
}
|
|
407
452
|
const allRecordsToFlush = [];
|
|
453
|
+
const triggeredAt = Date.now();
|
|
408
454
|
for (const key of keysToFlush) {
|
|
409
455
|
const metadata = buffers.get(key);
|
|
410
456
|
if (metadata) {
|
|
411
457
|
allRecordsToFlush.push(...metadata.buffer);
|
|
412
458
|
buffers.delete(key);
|
|
413
|
-
triggered.
|
|
459
|
+
triggered.set(key, triggeredAt);
|
|
414
460
|
}
|
|
415
461
|
}
|
|
416
462
|
allRecordsToFlush.sort((a, b) => a.timestamp - b.timestamp);
|
|
417
463
|
for (const bufferedRecord of allRecordsToFlush) sink(bufferedRecord);
|
|
418
|
-
triggered.
|
|
464
|
+
triggered.set(bufferKey, triggeredAt);
|
|
419
465
|
sink(record);
|
|
420
466
|
} else if (bufferLevel != null && compareLogLevel(record.level, bufferLevel) > 0) sink(record);
|
|
421
467
|
else {
|
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","buffer: LogRecord[]","flushTimer: ReturnType<typeof setInterval> | null","activeFlush: Promise<void> | null","nonBlockingSink: Sink & AsyncDisposable","options: ConsoleSinkOptions","levelMap: Record<LogLevel, ConsoleMethod>","nonBlockingSink: Sink & Disposable","asyncSink: AsyncSink","options: FingersCrossedOptions","parent: readonly string[]","child: readonly string[]","shouldFlushBuffer:\n | ((\n triggerCategory: readonly string[],\n bufferedCategory: readonly string[],\n ) => boolean)\n | null","category: readonly string[]","key: string","properties: Record<string, unknown>","contextValues: Record<string, unknown>","buffers: Map<string, BufferMetadata>","expiredKeys: string[]","numToEvict?: number","cleanupTimer: ReturnType<typeof setInterval> | null","allRecordsToFlush: LogRecord[]"],"sources":["../src/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 { compareLogLevel, 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 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 * Enable non-blocking mode with optional buffer configuration.\n * When enabled, log records are buffered and flushed in the background.\n *\n * @example Simple non-blocking mode\n * ```typescript\n * getStreamSink(stream, { nonBlocking: true });\n * ```\n *\n * @example Custom buffer configuration\n * ```typescript\n * getStreamSink(stream, {\n * nonBlocking: {\n * bufferSize: 1000,\n * flushInterval: 50\n * }\n * });\n * ```\n *\n * @default `false`\n * @since 1.0.0\n */\n nonBlocking?: boolean | {\n /**\n * Maximum number of records to buffer before flushing.\n * @default `100`\n */\n bufferSize?: number;\n\n /**\n * Interval in milliseconds between automatic flushes.\n * @default `100`\n */\n flushInterval?: number;\n };\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\n if (!options.nonBlocking) {\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\n // Non-blocking mode implementation\n const nonBlockingConfig = options.nonBlocking === true\n ? {}\n : options.nonBlocking;\n const bufferSize = nonBlockingConfig.bufferSize ?? 100;\n const flushInterval = nonBlockingConfig.flushInterval ?? 100;\n\n const buffer: LogRecord[] = [];\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n let disposed = false;\n let activeFlush: Promise<void> | null = null;\n const maxBufferSize = bufferSize * 2; // Overflow protection\n\n async function flush(): Promise<void> {\n if (buffer.length === 0) return;\n\n const records = buffer.splice(0);\n for (const record of records) {\n try {\n const bytes = encoder.encode(formatter(record));\n await writer.ready;\n await writer.write(bytes);\n } catch {\n // Silently ignore errors in non-blocking mode to avoid disrupting the application\n }\n }\n }\n\n function scheduleFlush(): void {\n if (activeFlush) return;\n\n activeFlush = flush().finally(() => {\n activeFlush = null;\n });\n }\n\n function startFlushTimer(): void {\n if (flushTimer !== null || disposed) return;\n\n flushTimer = setInterval(() => {\n scheduleFlush();\n }, flushInterval);\n }\n\n const nonBlockingSink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n\n // Buffer overflow protection: drop oldest records if buffer is too large\n if (buffer.length >= maxBufferSize) {\n buffer.shift(); // Remove oldest record\n }\n\n buffer.push(record);\n\n if (buffer.length >= bufferSize) {\n scheduleFlush();\n } else if (flushTimer === null) {\n startFlushTimer();\n }\n };\n\n nonBlockingSink[Symbol.asyncDispose] = async () => {\n disposed = true;\n if (flushTimer !== null) {\n clearInterval(flushTimer);\n flushTimer = null;\n }\n await flush();\n try {\n await writer.close();\n } catch {\n // Writer might already be closed or errored\n }\n };\n\n return nonBlockingSink;\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 * Enable non-blocking mode with optional buffer configuration.\n * When enabled, log records are buffered and flushed in the background.\n *\n * @example Simple non-blocking mode\n * ```typescript\n * getConsoleSink({ nonBlocking: true });\n * ```\n *\n * @example Custom buffer configuration\n * ```typescript\n * getConsoleSink({\n * nonBlocking: {\n * bufferSize: 1000,\n * flushInterval: 50\n * }\n * });\n * ```\n *\n * @default `false`\n * @since 1.0.0\n */\n nonBlocking?: boolean | {\n /**\n * Maximum number of records to buffer before flushing.\n * @default `100`\n */\n bufferSize?: number;\n\n /**\n * Interval in milliseconds between automatic flushes.\n * @default `100`\n */\n flushInterval?: number;\n };\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. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link Disposable}.\n */\nexport function getConsoleSink(\n options: ConsoleSinkOptions = {},\n): Sink | (Sink & Disposable) {\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\n const baseSink = (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 if (!options.nonBlocking) {\n return baseSink;\n }\n\n // Non-blocking mode implementation\n const nonBlockingConfig = options.nonBlocking === true\n ? {}\n : options.nonBlocking;\n const bufferSize = nonBlockingConfig.bufferSize ?? 100;\n const flushInterval = nonBlockingConfig.flushInterval ?? 100;\n\n const buffer: LogRecord[] = [];\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n let disposed = false;\n let flushScheduled = false;\n const maxBufferSize = bufferSize * 2; // Overflow protection\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 baseSink(record);\n } catch {\n // Silently ignore errors in non-blocking mode to avoid disrupting the application\n }\n }\n }\n\n function scheduleFlush(): void {\n if (flushScheduled) return;\n\n flushScheduled = true;\n setTimeout(() => {\n flushScheduled = false;\n flush();\n }, 0);\n }\n\n function startFlushTimer(): void {\n if (flushTimer !== null || disposed) return;\n\n flushTimer = setInterval(() => {\n flush();\n }, flushInterval);\n }\n\n const nonBlockingSink: Sink & Disposable = (record: LogRecord) => {\n if (disposed) return;\n\n // Buffer overflow protection: drop oldest records if buffer is too large\n if (buffer.length >= maxBufferSize) {\n buffer.shift(); // Remove oldest record\n }\n\n buffer.push(record);\n\n if (buffer.length >= bufferSize) {\n scheduleFlush();\n } else if (flushTimer === null) {\n startFlushTimer();\n }\n };\n\n nonBlockingSink[Symbol.dispose] = () => {\n disposed = true;\n if (flushTimer !== null) {\n clearInterval(flushTimer);\n flushTimer = null;\n }\n flush();\n };\n\n return nonBlockingSink;\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\n/**\n * Options for the {@link fingersCrossed} function.\n * @since 1.1.0\n */\nexport interface FingersCrossedOptions {\n /**\n * Minimum log level that triggers buffer flush.\n * When a log record at or above this level is received, all buffered\n * records are flushed to the wrapped sink.\n * @default `\"error\"`\n */\n readonly triggerLevel?: LogLevel;\n\n /**\n * Maximum log level that will be buffered.\n * Log records at or below this level are buffered, while records above\n * this level (but below {@link triggerLevel}) pass through immediately\n * without buffering.\n *\n * When `undefined` (default), all records below {@link triggerLevel} are\n * buffered (equivalent to setting this to the level just below triggerLevel).\n *\n * When `null`, all records below {@link triggerLevel} are buffered\n * (same as `undefined`, but explicit).\n *\n * @example Buffer only trace and debug, pass through info immediately\n * ```typescript\n * fingersCrossed(sink, {\n * bufferLevel: \"debug\", // trace, debug → buffered\n * triggerLevel: \"warning\", // warning+ → trigger flush\n * // info → passes through immediately (not buffered, not trigger)\n * })\n * ```\n *\n * @default `undefined` (buffer all levels below triggerLevel)\n * @since 2.0.0\n */\n readonly bufferLevel?: LogLevel | null;\n\n /**\n * Maximum buffer size before oldest records are dropped.\n * When the buffer exceeds this size, the oldest records are removed\n * to prevent unbounded memory growth.\n * @default `1000`\n */\n readonly maxBufferSize?: number;\n\n /**\n * Category isolation mode or custom matcher function.\n *\n * When `undefined` (default), all log records share a single buffer.\n *\n * When set to a mode string:\n *\n * - `\"descendant\"`: Flush child category buffers when parent triggers\n * - `\"ancestor\"`: Flush parent category buffers when child triggers\n * - `\"both\"`: Flush both parent and child category buffers\n *\n * When set to a function, it receives the trigger category and buffered\n * category and should return true if the buffered category should be flushed.\n *\n * @default `undefined` (no isolation, single global buffer)\n */\n readonly isolateByCategory?:\n | \"descendant\"\n | \"ancestor\"\n | \"both\"\n | ((\n triggerCategory: readonly string[],\n bufferedCategory: readonly string[],\n ) => boolean);\n\n /**\n * Enable context-based buffer isolation.\n * When enabled, buffers are isolated based on specified context keys.\n * This is useful for scenarios like HTTP request tracing where logs\n * should be isolated per request.\n *\n * @example\n * ```typescript\n * fingersCrossed(sink, {\n * isolateByContext: { keys: ['requestId'] }\n * })\n * ```\n *\n * @example Combined with category isolation\n * ```typescript\n * fingersCrossed(sink, {\n * isolateByCategory: 'descendant',\n * isolateByContext: { keys: ['requestId', 'sessionId'] }\n * })\n * ```\n *\n * @example With TTL-based buffer cleanup\n * ```typescript\n * fingersCrossed(sink, {\n * isolateByContext: {\n * keys: ['requestId'],\n * bufferTtlMs: 30000, // 30 seconds\n * cleanupIntervalMs: 10000 // cleanup every 10 seconds\n * }\n * })\n * ```\n *\n * @default `undefined` (no context isolation)\n * @since 1.2.0\n */\n readonly isolateByContext?: {\n /**\n * Context keys to use for isolation.\n * Buffers will be separate for different combinations of these context values.\n */\n readonly keys: readonly string[];\n\n /**\n * Maximum number of context buffers to maintain simultaneously.\n * When this limit is exceeded, the least recently used (LRU) buffers\n * will be evicted to make room for new ones.\n *\n * This provides memory protection in high-concurrency scenarios where\n * many different context values might be active simultaneously.\n *\n * When set to 0 or undefined, no limit is enforced.\n *\n * @default `undefined` (no limit)\n * @since 1.2.0\n */\n readonly maxContexts?: number;\n\n /**\n * Time-to-live for context buffers in milliseconds.\n * Buffers that haven't been accessed for this duration will be automatically\n * cleaned up to prevent memory leaks in long-running applications.\n *\n * When set to 0 or undefined, buffers will never expire based on time.\n *\n * @default `undefined` (no TTL)\n * @since 1.2.0\n */\n readonly bufferTtlMs?: number;\n\n /**\n * Interval in milliseconds for running cleanup operations.\n * The cleanup process removes expired buffers based on {@link bufferTtlMs}.\n *\n * This option is ignored if {@link bufferTtlMs} is not set.\n *\n * @default `30000` (30 seconds)\n * @since 1.2.0\n */\n readonly cleanupIntervalMs?: number;\n };\n}\n\n/**\n * Metadata for context-based buffer tracking.\n * Used internally by {@link fingersCrossed} to manage buffer lifecycle with LRU support.\n * @since 1.2.0\n */\ninterface BufferMetadata {\n /**\n * The actual log records buffer.\n */\n readonly buffer: LogRecord[];\n\n /**\n * Monotonically increasing order of the last access to this buffer.\n * Used for LRU-based eviction when {@link FingersCrossedOptions.isolateByContext.maxContexts} is set.\n */\n lastAccess: number;\n}\n\n/**\n * Creates a sink that buffers log records until a trigger level is reached.\n * This pattern, known as \"fingers crossed\" logging, keeps detailed debug logs\n * in memory and only outputs them when an error or other significant event occurs.\n *\n * @example Basic usage with default settings\n * ```typescript\n * const sink = fingersCrossed(getConsoleSink());\n * // Debug and info logs are buffered\n * // When an error occurs, all buffered logs + the error are output\n * ```\n *\n * @example Custom trigger level and buffer size\n * ```typescript\n * const sink = fingersCrossed(getConsoleSink(), {\n * triggerLevel: \"warning\", // Trigger on warning or higher\n * maxBufferSize: 500 // Keep last 500 records\n * });\n * ```\n *\n * @example Category isolation\n * ```typescript\n * const sink = fingersCrossed(getConsoleSink(), {\n * isolateByCategory: \"descendant\" // Separate buffers per category\n * });\n * // Error in [\"app\"] triggers flush of [\"app\"] and [\"app\", \"module\"] buffers\n * // But not [\"other\"] buffer\n * ```\n *\n * @param sink The sink to wrap. Buffered records are sent to this sink when\n * triggered.\n * @param options Configuration options for the fingers crossed behavior.\n * @returns A sink that buffers records until the trigger level is reached.\n * @since 1.1.0\n */\nexport function fingersCrossed(\n sink: Sink,\n options: FingersCrossedOptions = {},\n): Sink | (Sink & Disposable) {\n const triggerLevel = options.triggerLevel ?? \"error\";\n const bufferLevel = options.bufferLevel;\n const maxBufferSize = Math.max(0, options.maxBufferSize ?? 1000);\n const isolateByCategory = options.isolateByCategory;\n const isolateByContext = options.isolateByContext;\n\n // TTL and LRU configuration\n const bufferTtlMs = isolateByContext?.bufferTtlMs;\n const cleanupIntervalMs = isolateByContext?.cleanupIntervalMs ?? 30000;\n const maxContexts = isolateByContext?.maxContexts;\n const hasTtl = bufferTtlMs != null && bufferTtlMs > 0;\n const hasLru = maxContexts != null && maxContexts > 0;\n\n // Validate trigger level early\n try {\n compareLogLevel(\"trace\", triggerLevel); // Test with any valid level\n } catch (error) {\n throw new TypeError(\n `Invalid triggerLevel: ${JSON.stringify(triggerLevel)}. ${\n error instanceof Error ? error.message : String(error)\n }`,\n );\n }\n\n // Validate buffer level if provided\n if (bufferLevel != null) {\n try {\n compareLogLevel(\"trace\", bufferLevel); // Test with any valid level\n } catch (error) {\n throw new TypeError(\n `Invalid bufferLevel: ${JSON.stringify(bufferLevel)}. ${\n error instanceof Error ? error.message : String(error)\n }`,\n );\n }\n\n // bufferLevel must be strictly less than triggerLevel\n if (compareLogLevel(bufferLevel, triggerLevel) >= 0) {\n throw new RangeError(\n `bufferLevel (${JSON.stringify(bufferLevel)}) must be lower than ` +\n `triggerLevel (${JSON.stringify(triggerLevel)}).`,\n );\n }\n }\n\n // Helper functions for category matching\n function isDescendant(\n parent: readonly string[],\n child: readonly string[],\n ): boolean {\n if (parent.length === 0 || child.length === 0) return false; // Empty categories are isolated\n if (parent.length > child.length) return false;\n return parent.every((p, i) => p === child[i]);\n }\n\n function isAncestor(\n child: readonly string[],\n parent: readonly string[],\n ): boolean {\n if (child.length === 0 || parent.length === 0) return false; // Empty categories are isolated\n if (child.length < parent.length) return false;\n return parent.every((p, i) => p === child[i]);\n }\n\n // Determine matcher function based on isolation mode\n let shouldFlushBuffer:\n | ((\n triggerCategory: readonly string[],\n bufferedCategory: readonly string[],\n ) => boolean)\n | null = null;\n\n if (isolateByCategory) {\n if (typeof isolateByCategory === \"function\") {\n shouldFlushBuffer = isolateByCategory;\n } else {\n switch (isolateByCategory) {\n case \"descendant\":\n shouldFlushBuffer = (trigger, buffered) =>\n isDescendant(trigger, buffered);\n break;\n case \"ancestor\":\n shouldFlushBuffer = (trigger, buffered) =>\n isAncestor(trigger, buffered);\n break;\n case \"both\":\n shouldFlushBuffer = (trigger, buffered) =>\n isDescendant(trigger, buffered) || isAncestor(trigger, buffered);\n break;\n }\n }\n }\n\n // Helper functions for category serialization\n function getCategoryKey(category: readonly string[]): string {\n return JSON.stringify(category);\n }\n\n function parseCategoryKey(key: string): string[] {\n return JSON.parse(key);\n }\n\n // Helper function to extract context values from properties\n function getContextKey(properties: Record<string, unknown>): string {\n if (!isolateByContext || isolateByContext.keys.length === 0) {\n return \"\";\n }\n const contextValues: Record<string, unknown> = {};\n for (const key of isolateByContext.keys) {\n if (key in properties) {\n contextValues[key] = properties[key];\n }\n }\n return JSON.stringify(contextValues);\n }\n\n // Helper function to generate buffer key\n function getBufferKey(\n category: readonly string[],\n properties: Record<string, unknown>,\n ): string {\n const categoryKey = getCategoryKey(category);\n if (!isolateByContext) {\n return categoryKey;\n }\n const contextKey = getContextKey(properties);\n return `${categoryKey}:${contextKey}`;\n }\n\n // Helper function to parse buffer key\n function parseBufferKey(key: string): {\n category: string[];\n context: string;\n } {\n if (!isolateByContext) {\n return { category: parseCategoryKey(key), context: \"\" };\n }\n // Find the separator between category and context\n // The category part is JSON-encoded, so we need to find where it ends\n // We look for \"]:\" which indicates end of category array and start of context\n const separatorIndex = key.indexOf(\"]:\");\n if (separatorIndex === -1) {\n // No context part, entire key is category\n return { category: parseCategoryKey(key), context: \"\" };\n }\n const categoryPart = key.substring(0, separatorIndex + 1); // Include the ]\n const contextPart = key.substring(separatorIndex + 2); // Skip ]:\n return { category: parseCategoryKey(categoryPart), context: contextPart };\n }\n\n // TTL-based cleanup function\n function cleanupExpiredBuffers(buffers: Map<string, BufferMetadata>): void {\n if (!hasTtl) return;\n\n const now = Date.now();\n const expiredKeys: string[] = [];\n\n for (const [key, metadata] of buffers) {\n if (metadata.buffer.length === 0) continue;\n\n // Use the timestamp of the last (most recent) record in the buffer\n const lastRecordTimestamp =\n metadata.buffer[metadata.buffer.length - 1].timestamp;\n if (now - lastRecordTimestamp > bufferTtlMs!) {\n expiredKeys.push(key);\n }\n }\n\n // Remove expired buffers\n for (const key of expiredKeys) {\n buffers.delete(key);\n }\n }\n\n // LRU-based eviction function\n function evictLruBuffers(\n buffers: Map<string, BufferMetadata>,\n numToEvict?: number,\n ): void {\n if (!hasLru) return;\n\n // Use provided numToEvict or calculate based on current size vs limit\n const toEvict = numToEvict ?? Math.max(0, buffers.size - maxContexts!);\n if (toEvict <= 0) return;\n\n // Sort by lastAccess timestamp (oldest first)\n const sortedEntries = Array.from(buffers.entries())\n .sort(([, a], [, b]) => a.lastAccess - b.lastAccess);\n\n // Remove the oldest buffers\n for (let i = 0; i < toEvict; i++) {\n const [key] = sortedEntries[i];\n buffers.delete(key);\n }\n }\n\n // Buffer management\n if (!isolateByCategory && !isolateByContext) {\n // Single global buffer\n const buffer: LogRecord[] = [];\n let triggered = false;\n\n return (record: LogRecord) => {\n if (triggered) {\n // Already triggered, pass through directly\n sink(record);\n return;\n }\n\n // Check if this record triggers flush\n if (compareLogLevel(record.level, triggerLevel) >= 0) {\n triggered = true;\n\n // Flush buffer\n for (const bufferedRecord of buffer) {\n sink(bufferedRecord);\n }\n buffer.length = 0;\n\n // Send trigger record\n sink(record);\n } else if (\n bufferLevel != null &&\n compareLogLevel(record.level, bufferLevel) > 0\n ) {\n // Record is above bufferLevel but below triggerLevel: pass through\n sink(record);\n } else {\n // Buffer the record\n buffer.push(record);\n\n // Enforce max buffer size\n while (buffer.length > maxBufferSize) {\n buffer.shift();\n }\n }\n };\n } else {\n // Category and/or context-isolated buffers\n const buffers = new Map<string, BufferMetadata>();\n const triggered = new Set<string>();\n let accessCounter = 0;\n\n // Set up TTL cleanup timer if enabled\n let cleanupTimer: ReturnType<typeof setInterval> | null = null;\n if (hasTtl) {\n cleanupTimer = setInterval(() => {\n cleanupExpiredBuffers(buffers);\n }, cleanupIntervalMs);\n }\n\n const fingersCrossedSink = (record: LogRecord) => {\n const bufferKey = getBufferKey(record.category, record.properties);\n\n // Check if this buffer is already triggered\n if (triggered.has(bufferKey)) {\n sink(record);\n return;\n }\n\n // Check if this record triggers flush\n if (compareLogLevel(record.level, triggerLevel) >= 0) {\n // Find all buffers that should be flushed\n const keysToFlush = new Set<string>();\n\n for (const [bufferedKey] of buffers) {\n if (bufferedKey === bufferKey) {\n keysToFlush.add(bufferedKey);\n } else {\n const { category: bufferedCategory, context: bufferedContext } =\n parseBufferKey(bufferedKey);\n const { context: triggerContext } = parseBufferKey(bufferKey);\n\n // Check context match\n let contextMatches = true;\n if (isolateByContext) {\n contextMatches = bufferedContext === triggerContext;\n }\n\n // Check category match\n let categoryMatches = false;\n if (!isolateByCategory) {\n // No category isolation, so all categories match if context matches\n categoryMatches = contextMatches;\n } else if (shouldFlushBuffer) {\n try {\n categoryMatches = shouldFlushBuffer(\n record.category,\n bufferedCategory,\n );\n } catch {\n // Ignore errors from custom matcher\n }\n } else {\n // Same category only\n categoryMatches = getCategoryKey(record.category) ===\n getCategoryKey(bufferedCategory);\n }\n\n // Both must match for the buffer to be flushed\n if (contextMatches && categoryMatches) {\n keysToFlush.add(bufferedKey);\n }\n }\n }\n\n // Flush matching buffers\n const allRecordsToFlush: LogRecord[] = [];\n for (const key of keysToFlush) {\n const metadata = buffers.get(key);\n if (metadata) {\n allRecordsToFlush.push(...metadata.buffer);\n buffers.delete(key);\n triggered.add(key);\n }\n }\n\n // Sort by timestamp to maintain chronological order\n allRecordsToFlush.sort((a, b) => a.timestamp - b.timestamp);\n\n // Flush all records\n for (const bufferedRecord of allRecordsToFlush) {\n sink(bufferedRecord);\n }\n\n // Mark trigger buffer as triggered and send trigger record\n triggered.add(bufferKey);\n sink(record);\n } else if (\n bufferLevel != null &&\n compareLogLevel(record.level, bufferLevel) > 0\n ) {\n // Record is above bufferLevel but below triggerLevel: pass through\n sink(record);\n } else {\n // Buffer the record\n let metadata = buffers.get(bufferKey);\n if (!metadata) {\n // Apply LRU eviction if adding new buffer would exceed capacity\n if (hasLru && buffers.size >= maxContexts!) {\n // Calculate how many buffers to evict to make room for the new one\n const numToEvict = buffers.size - maxContexts! + 1;\n evictLruBuffers(buffers, numToEvict);\n }\n\n metadata = {\n buffer: [],\n lastAccess: ++accessCounter,\n };\n buffers.set(bufferKey, metadata);\n } else {\n // Update last access order for LRU\n metadata.lastAccess = ++accessCounter;\n }\n\n metadata.buffer.push(record);\n\n // Enforce max buffer size per buffer\n while (metadata.buffer.length > maxBufferSize) {\n metadata.buffer.shift();\n }\n }\n };\n\n // Add disposal functionality to clean up timer\n if (cleanupTimer !== null) {\n (fingersCrossedSink as Sink & Disposable)[Symbol.dispose] = () => {\n if (cleanupTimer !== null) {\n clearInterval(cleanupTimer);\n cleanupTimer = null;\n }\n };\n }\n\n return fingersCrossedSink;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA8CA,SAAgB,WAAWA,MAAYC,QAA0B;CAC/D,MAAM,aAAa,SAAS,OAAO;AACnC,QAAO,CAACC,WAAsB;AAC5B,MAAI,WAAW,OAAO,CAAE,MAAK,OAAO;CACrC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;AA6ED,SAAgB,cACdC,QACAC,UAA6B,CAAE,GACP;CACxB,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,UAAU,QAAQ,WAAW,IAAI;CACvC,MAAM,SAAS,OAAO,WAAW;AAEjC,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AAS,OAAO,OAAO;GAE1B;EACF;AAGD,MAAI,iBAAiB,KACnB,CAAC,mBAAyC,OAAO,WAAW,MAAM;AAChE,OAAI,iBAAiB,MAAM;AACzB,kBAAc,aAAa;AAC3B,mBAAe;GAChB;EACF;AAGH,SAAO;CACR;AACF"}
|
|
1
|
+
{"version":3,"file":"sink.js","names":["sink: Sink","filter: FilterLike","filtered: Sink & Partial<Disposable & AsyncDisposable>","record: LogRecord","stream: WritableStream","options: StreamSinkOptions","sink: Sink & AsyncDisposable","buffer: LogRecord[]","flushTimer: ReturnType<typeof setInterval> | null","activeFlush: Promise<void> | null","nonBlockingSink: Sink & AsyncDisposable","options: ConsoleSinkOptions","levelMap: Record<LogLevel, ConsoleMethod>","scheduledFlushTimer: ReturnType<typeof setTimeout> | null","nonBlockingSink: Sink & Disposable","asyncSink: AsyncSink","options: FingersCrossedOptions","parent: readonly string[]","child: readonly string[]","shouldFlushBuffer:\n | ((\n triggerCategory: readonly string[],\n bufferedCategory: readonly string[],\n ) => boolean)\n | null","category: readonly string[]","key: string","properties: Record<string, unknown>","contextValues: Record<string, unknown>","buffers: Map<string, BufferMetadata>","triggered: Map<string, number>","expiredKeys: string[]","numToEvict?: number","cleanupTimer: ReturnType<typeof setInterval> | null","allRecordsToFlush: LogRecord[]"],"sources":["../src/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 { compareLogLevel, type LogLevel } from \"./level.ts\";\nimport { LoggerImpl } from \"./logger.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 const filtered: Sink & Partial<Disposable & AsyncDisposable> = (\n record: LogRecord,\n ) => {\n if (filterFunc(record)) sink(record);\n };\n const disposableSink = sink as Sink & Partial<Disposable & AsyncDisposable>;\n if (Symbol.dispose in disposableSink) {\n filtered[Symbol.dispose] = disposableSink[Symbol.dispose]?.bind(sink);\n }\n if (Symbol.asyncDispose in disposableSink) {\n filtered[Symbol.asyncDispose] = disposableSink[Symbol.asyncDispose]?.bind(\n sink,\n );\n }\n return filtered;\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 * Enable non-blocking mode with optional buffer configuration.\n * When enabled, log records are buffered and flushed in the background.\n *\n * @example Simple non-blocking mode\n * ```typescript\n * getStreamSink(stream, { nonBlocking: true });\n * ```\n *\n * @example Custom buffer configuration\n * ```typescript\n * getStreamSink(stream, {\n * nonBlocking: {\n * bufferSize: 1000,\n * flushInterval: 50\n * }\n * });\n * ```\n *\n * @default `false`\n * @since 1.0.0\n */\n nonBlocking?: boolean | {\n /**\n * Maximum number of records to buffer before flushing.\n * @default `100`\n */\n bufferSize?: number;\n\n /**\n * Interval in milliseconds between automatic flushes.\n * @default `100`\n */\n flushInterval?: number;\n };\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\n if (!options.nonBlocking) {\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\n // Non-blocking mode implementation\n const nonBlockingConfig = options.nonBlocking === true\n ? {}\n : options.nonBlocking;\n const bufferSize = nonBlockingConfig.bufferSize ?? 100;\n const flushInterval = nonBlockingConfig.flushInterval ?? 100;\n\n const buffer: LogRecord[] = [];\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n let disposed = false;\n let activeFlush: Promise<void> | null = null;\n const maxBufferSize = bufferSize * 2; // Overflow protection\n\n async function flush(): Promise<void> {\n if (buffer.length === 0) return;\n\n const records = buffer.splice(0);\n for (const record of records) {\n try {\n const bytes = encoder.encode(formatter(record));\n await writer.ready;\n await writer.write(bytes);\n } catch {\n // Silently ignore errors in non-blocking mode to avoid disrupting the application\n }\n }\n }\n\n function scheduleFlush(): void {\n if (activeFlush) return;\n\n activeFlush = flush().finally(() => {\n activeFlush = null;\n });\n }\n\n function startFlushTimer(): void {\n if (flushTimer !== null || disposed) return;\n\n flushTimer = setInterval(() => {\n scheduleFlush();\n }, flushInterval);\n }\n\n const nonBlockingSink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n\n // Buffer overflow protection: drop oldest records if buffer is too large\n if (buffer.length >= maxBufferSize) {\n buffer.shift(); // Remove oldest record\n }\n\n buffer.push(record);\n\n if (buffer.length >= bufferSize) {\n scheduleFlush();\n } else if (flushTimer === null) {\n startFlushTimer();\n }\n };\n\n nonBlockingSink[Symbol.asyncDispose] = async () => {\n disposed = true;\n if (flushTimer !== null) {\n clearInterval(flushTimer);\n flushTimer = null;\n }\n await flush();\n try {\n await writer.close();\n } catch {\n // Writer might already be closed or errored\n }\n };\n\n return nonBlockingSink;\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 * Enable non-blocking mode with optional buffer configuration.\n * When enabled, log records are buffered and flushed in the background.\n *\n * @example Simple non-blocking mode\n * ```typescript\n * getConsoleSink({ nonBlocking: true });\n * ```\n *\n * @example Custom buffer configuration\n * ```typescript\n * getConsoleSink({\n * nonBlocking: {\n * bufferSize: 1000,\n * flushInterval: 50\n * }\n * });\n * ```\n *\n * @default `false`\n * @since 1.0.0\n */\n nonBlocking?: boolean | {\n /**\n * Maximum number of records to buffer before flushing.\n * @default `100`\n */\n bufferSize?: number;\n\n /**\n * Interval in milliseconds between automatic flushes.\n * @default `100`\n */\n flushInterval?: number;\n };\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. If `nonBlocking` is enabled,\n * returns a sink that also implements {@link Disposable}.\n */\nexport function getConsoleSink(\n options: ConsoleSinkOptions = {},\n): Sink | (Sink & Disposable) {\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\n const baseSink = (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 if (!options.nonBlocking) {\n return baseSink;\n }\n\n // Non-blocking mode implementation\n const nonBlockingConfig = options.nonBlocking === true\n ? {}\n : options.nonBlocking;\n const bufferSize = nonBlockingConfig.bufferSize ?? 100;\n const flushInterval = nonBlockingConfig.flushInterval ?? 100;\n\n const buffer: LogRecord[] = [];\n let flushTimer: ReturnType<typeof setInterval> | null = null;\n let scheduledFlushTimer: ReturnType<typeof setTimeout> | null = null;\n let disposed = false;\n let flushScheduled = false;\n const maxBufferSize = bufferSize * 2; // Overflow protection\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 baseSink(record);\n } catch {\n // Silently ignore errors in non-blocking mode to avoid disrupting the application\n }\n }\n }\n\n function scheduleFlush(): void {\n if (flushScheduled) return;\n\n flushScheduled = true;\n scheduledFlushTimer = setTimeout(() => {\n scheduledFlushTimer = null;\n flushScheduled = false;\n flush();\n }, 0);\n }\n\n function startFlushTimer(): void {\n if (flushTimer !== null || disposed) return;\n\n flushTimer = setInterval(() => {\n flush();\n }, flushInterval);\n }\n\n const nonBlockingSink: Sink & Disposable = (record: LogRecord) => {\n if (disposed) return;\n\n // Buffer overflow protection: drop oldest records if buffer is too large\n if (buffer.length >= maxBufferSize) {\n buffer.shift(); // Remove oldest record\n }\n\n buffer.push(record);\n\n if (buffer.length >= bufferSize) {\n scheduleFlush();\n } else if (flushTimer === null) {\n startFlushTimer();\n }\n };\n\n nonBlockingSink[Symbol.dispose] = () => {\n disposed = true;\n if (flushTimer !== null) {\n clearInterval(flushTimer);\n flushTimer = null;\n }\n if (scheduledFlushTimer !== null) {\n clearTimeout(scheduledFlushTimer);\n scheduledFlushTimer = null;\n flushScheduled = false;\n }\n flush();\n };\n\n return nonBlockingSink;\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((error) => {\n try {\n if (_asyncSinkError in record) return;\n\n const metaLogger = LoggerImpl.getLogger([\"logtape\", \"meta\"]);\n const errorRecord = {\n category: [\"logtape\", \"meta\"],\n level: \"error\",\n timestamp: Date.now(),\n rawMessage: \"Async sink error: {error}\",\n message: [\"Async sink error: \", error, \"\"],\n properties: { error, sink: asyncSink, record },\n [_asyncSinkError]: true,\n } as LogRecord;\n metaLogger.emit(errorRecord, new Set([sink]));\n } catch {\n // Last resort – cannot log at all\n }\n });\n };\n sink[Symbol.asyncDispose] = async () => {\n // Drain the promise chain until it settles – catch handlers\n // may enqueue additional async work (e.g., meta-logger writes)\n for (;;) {\n const promise = lastPromise;\n await promise;\n if (promise === lastPromise) break;\n }\n };\n return sink;\n}\n\nconst _asyncSinkError = Symbol.for(\"logtape.asyncSinkError\");\n\n/**\n * Options for the {@link fingersCrossed} function.\n * @since 1.1.0\n */\nexport interface FingersCrossedOptions {\n /**\n * Minimum log level that triggers buffer flush.\n * When a log record at or above this level is received, all buffered\n * records are flushed to the wrapped sink.\n * @default `\"error\"`\n */\n readonly triggerLevel?: LogLevel;\n\n /**\n * Maximum log level that will be buffered.\n * Log records at or below this level are buffered, while records above\n * this level (but below {@link triggerLevel}) pass through immediately\n * without buffering.\n *\n * When `undefined` (default), all records below {@link triggerLevel} are\n * buffered (equivalent to setting this to the level just below triggerLevel).\n *\n * When `null`, all records below {@link triggerLevel} are buffered\n * (same as `undefined`, but explicit).\n *\n * @example Buffer only trace and debug, pass through info immediately\n * ```typescript\n * fingersCrossed(sink, {\n * bufferLevel: \"debug\", // trace, debug → buffered\n * triggerLevel: \"warning\", // warning+ → trigger flush\n * // info → passes through immediately (not buffered, not trigger)\n * })\n * ```\n *\n * @default `undefined` (buffer all levels below triggerLevel)\n * @since 2.0.0\n */\n readonly bufferLevel?: LogLevel | null;\n\n /**\n * Maximum buffer size before oldest records are dropped.\n * When the buffer exceeds this size, the oldest records are removed\n * to prevent unbounded memory growth.\n * @default `1000`\n */\n readonly maxBufferSize?: number;\n\n /**\n * Category isolation mode or custom matcher function.\n *\n * When `undefined` (default), all log records share a single buffer.\n *\n * When set to a mode string:\n *\n * - `\"descendant\"`: Flush child category buffers when parent triggers\n * - `\"ancestor\"`: Flush parent category buffers when child triggers\n * - `\"both\"`: Flush both parent and child category buffers\n *\n * When set to a function, it receives the trigger category and buffered\n * category and should return true if the buffered category should be flushed.\n *\n * @default `undefined` (no isolation, single global buffer)\n */\n readonly isolateByCategory?:\n | \"descendant\"\n | \"ancestor\"\n | \"both\"\n | ((\n triggerCategory: readonly string[],\n bufferedCategory: readonly string[],\n ) => boolean);\n\n /**\n * Enable context-based buffer isolation.\n * When enabled, buffers are isolated based on specified context keys.\n * This is useful for scenarios like HTTP request tracing where logs\n * should be isolated per request.\n *\n * @example\n * ```typescript\n * fingersCrossed(sink, {\n * isolateByContext: { keys: ['requestId'] }\n * })\n * ```\n *\n * @example Combined with category isolation\n * ```typescript\n * fingersCrossed(sink, {\n * isolateByCategory: 'descendant',\n * isolateByContext: { keys: ['requestId', 'sessionId'] }\n * })\n * ```\n *\n * @example With TTL-based buffer cleanup\n * ```typescript\n * fingersCrossed(sink, {\n * isolateByContext: {\n * keys: ['requestId'],\n * bufferTtlMs: 30000, // 30 seconds\n * cleanupIntervalMs: 10000 // cleanup every 10 seconds\n * }\n * })\n * ```\n *\n * @default `undefined` (no context isolation)\n * @since 1.2.0\n */\n readonly isolateByContext?: {\n /**\n * Context keys to use for isolation.\n * Buffers will be separate for different combinations of these context values.\n */\n readonly keys: readonly string[];\n\n /**\n * Maximum number of context buffers to maintain simultaneously.\n * When this limit is exceeded, the least recently used (LRU) buffers\n * will be evicted to make room for new ones.\n *\n * This provides memory protection in high-concurrency scenarios where\n * many different context values might be active simultaneously.\n *\n * When set to 0 or undefined, no limit is enforced.\n *\n * @default `undefined` (no limit)\n * @since 1.2.0\n */\n readonly maxContexts?: number;\n\n /**\n * Time-to-live for context buffers in milliseconds.\n * Buffers that haven't been accessed for this duration will be automatically\n * cleaned up to prevent memory leaks in long-running applications.\n *\n * When set to 0 or undefined, buffers will never expire based on time.\n *\n * @default `undefined` (no TTL)\n * @since 1.2.0\n */\n readonly bufferTtlMs?: number;\n\n /**\n * Interval in milliseconds for running cleanup operations.\n * The cleanup process removes expired buffers based on {@link bufferTtlMs}.\n *\n * This option is ignored if {@link bufferTtlMs} is not set.\n *\n * @default `30000` (30 seconds)\n * @since 1.2.0\n */\n readonly cleanupIntervalMs?: number;\n };\n}\n\n/**\n * Metadata for context-based buffer tracking.\n * Used internally by {@link fingersCrossed} to manage buffer lifecycle with LRU support.\n * @since 1.2.0\n */\ninterface BufferMetadata {\n /**\n * The actual log records buffer.\n */\n readonly buffer: LogRecord[];\n\n /**\n * Monotonically increasing order of the last access to this buffer.\n * Used for LRU-based eviction when {@link FingersCrossedOptions.isolateByContext.maxContexts} is set.\n */\n lastAccess: number;\n}\n\n/**\n * Creates a sink that buffers log records until a trigger level is reached.\n * This pattern, known as \"fingers crossed\" logging, keeps detailed debug logs\n * in memory and only outputs them when an error or other significant event occurs.\n *\n * @example Basic usage with default settings\n * ```typescript\n * const sink = fingersCrossed(getConsoleSink());\n * // Debug and info logs are buffered\n * // When an error occurs, all buffered logs + the error are output\n * ```\n *\n * @example Custom trigger level and buffer size\n * ```typescript\n * const sink = fingersCrossed(getConsoleSink(), {\n * triggerLevel: \"warning\", // Trigger on warning or higher\n * maxBufferSize: 500 // Keep last 500 records\n * });\n * ```\n *\n * @example Category isolation\n * ```typescript\n * const sink = fingersCrossed(getConsoleSink(), {\n * isolateByCategory: \"descendant\" // Separate buffers per category\n * });\n * // Error in [\"app\"] triggers flush of [\"app\"] and [\"app\", \"module\"] buffers\n * // But not [\"other\"] buffer\n * ```\n *\n * @param sink The sink to wrap. Buffered records are sent to this sink when\n * triggered.\n * @param options Configuration options for the fingers crossed behavior.\n * @returns A sink that buffers records until the trigger level is reached.\n * @since 1.1.0\n */\nexport function fingersCrossed(\n sink: Sink,\n options: FingersCrossedOptions = {},\n): Sink | (Sink & Disposable) {\n const triggerLevel = options.triggerLevel ?? \"error\";\n const bufferLevel = options.bufferLevel;\n const maxBufferSize = Math.max(0, options.maxBufferSize ?? 1000);\n const isolateByCategory = options.isolateByCategory;\n const isolateByContext = options.isolateByContext;\n\n // TTL and LRU configuration\n const bufferTtlMs = isolateByContext?.bufferTtlMs;\n const cleanupIntervalMs = isolateByContext?.cleanupIntervalMs ?? 30000;\n const maxContexts = isolateByContext?.maxContexts;\n const hasTtl = bufferTtlMs != null && bufferTtlMs > 0;\n const hasLru = maxContexts != null && maxContexts > 0;\n\n // Validate trigger level early\n try {\n compareLogLevel(\"trace\", triggerLevel); // Test with any valid level\n } catch (error) {\n throw new TypeError(\n `Invalid triggerLevel: ${JSON.stringify(triggerLevel)}. ${\n error instanceof Error ? error.message : String(error)\n }`,\n );\n }\n\n // Validate buffer level if provided\n if (bufferLevel != null) {\n try {\n compareLogLevel(\"trace\", bufferLevel); // Test with any valid level\n } catch (error) {\n throw new TypeError(\n `Invalid bufferLevel: ${JSON.stringify(bufferLevel)}. ${\n error instanceof Error ? error.message : String(error)\n }`,\n );\n }\n\n // bufferLevel must be strictly less than triggerLevel\n if (compareLogLevel(bufferLevel, triggerLevel) >= 0) {\n throw new RangeError(\n `bufferLevel (${JSON.stringify(bufferLevel)}) must be lower than ` +\n `triggerLevel (${JSON.stringify(triggerLevel)}).`,\n );\n }\n }\n\n // Helper functions for category matching\n function isDescendant(\n parent: readonly string[],\n child: readonly string[],\n ): boolean {\n if (parent.length === 0 || child.length === 0) return false; // Empty categories are isolated\n if (parent.length > child.length) return false;\n return parent.every((p, i) => p === child[i]);\n }\n\n function isAncestor(\n child: readonly string[],\n parent: readonly string[],\n ): boolean {\n if (child.length === 0 || parent.length === 0) return false; // Empty categories are isolated\n if (child.length < parent.length) return false;\n return parent.every((p, i) => p === child[i]);\n }\n\n // Determine matcher function based on isolation mode\n let shouldFlushBuffer:\n | ((\n triggerCategory: readonly string[],\n bufferedCategory: readonly string[],\n ) => boolean)\n | null = null;\n\n if (isolateByCategory) {\n if (typeof isolateByCategory === \"function\") {\n shouldFlushBuffer = isolateByCategory;\n } else {\n switch (isolateByCategory) {\n case \"descendant\":\n shouldFlushBuffer = (trigger, buffered) =>\n isDescendant(trigger, buffered);\n break;\n case \"ancestor\":\n shouldFlushBuffer = (trigger, buffered) =>\n isAncestor(trigger, buffered);\n break;\n case \"both\":\n shouldFlushBuffer = (trigger, buffered) =>\n isDescendant(trigger, buffered) || isAncestor(trigger, buffered);\n break;\n }\n }\n }\n\n // Helper functions for category serialization\n function getCategoryKey(category: readonly string[]): string {\n return JSON.stringify(category);\n }\n\n function parseCategoryKey(key: string): string[] {\n return JSON.parse(key);\n }\n\n // Helper function to extract context values from properties\n function getContextKey(properties: Record<string, unknown>): string {\n if (!isolateByContext || isolateByContext.keys.length === 0) {\n return \"\";\n }\n const contextValues: Record<string, unknown> = {};\n for (const key of isolateByContext.keys) {\n if (key in properties) {\n contextValues[key] = properties[key];\n }\n }\n return JSON.stringify(contextValues);\n }\n\n // Helper function to generate buffer key\n function getBufferKey(\n category: readonly string[],\n properties: Record<string, unknown>,\n ): string {\n const categoryKey = getCategoryKey(category);\n if (!isolateByContext) {\n return categoryKey;\n }\n const contextKey = getContextKey(properties);\n return `${categoryKey}:${contextKey}`;\n }\n\n // Helper function to parse buffer key\n function parseBufferKey(key: string): {\n category: string[];\n context: string;\n } {\n if (!isolateByContext) {\n return { category: parseCategoryKey(key), context: \"\" };\n }\n // Find the separator between category and context\n // The category part is JSON-encoded, so we need to find where it ends\n // We look for \"]:\" which indicates end of category array and start of context\n const separatorIndex = key.indexOf(\"]:\");\n if (separatorIndex === -1) {\n // No context part, entire key is category\n return { category: parseCategoryKey(key), context: \"\" };\n }\n const categoryPart = key.substring(0, separatorIndex + 1); // Include the ]\n const contextPart = key.substring(separatorIndex + 2); // Skip ]:\n return { category: parseCategoryKey(categoryPart), context: contextPart };\n }\n\n // TTL-based cleanup function\n function cleanupExpiredBuffers(\n buffers: Map<string, BufferMetadata>,\n triggered: Map<string, number>,\n ): void {\n if (!hasTtl) return;\n\n const now = Date.now();\n const expiredKeys: string[] = [];\n\n for (const [key, metadata] of buffers) {\n if (metadata.buffer.length === 0) continue;\n\n // Use the timestamp of the last (most recent) record in the buffer\n const lastRecordTimestamp =\n metadata.buffer[metadata.buffer.length - 1].timestamp;\n if (now - lastRecordTimestamp > bufferTtlMs!) {\n expiredKeys.push(key);\n }\n }\n\n // Remove expired buffers\n for (const key of expiredKeys) {\n buffers.delete(key);\n triggered.delete(key);\n }\n\n // Triggered buffers no longer have buffer metadata, so they must be\n // expired separately to avoid unbounded growth for dynamic contexts.\n for (const [key, triggeredAt] of triggered) {\n if (now - triggeredAt > bufferTtlMs!) {\n triggered.delete(key);\n }\n }\n }\n\n // LRU-based eviction function\n function evictLruBuffers(\n buffers: Map<string, BufferMetadata>,\n numToEvict?: number,\n ): void {\n if (!hasLru) return;\n\n // Use provided numToEvict or calculate based on current size vs limit\n const toEvict = numToEvict ?? Math.max(0, buffers.size - maxContexts!);\n if (toEvict <= 0) return;\n\n // Sort by lastAccess timestamp (oldest first)\n const sortedEntries = Array.from(buffers.entries())\n .sort(([, a], [, b]) => a.lastAccess - b.lastAccess);\n\n // Remove the oldest buffers\n for (let i = 0; i < toEvict; i++) {\n const [key] = sortedEntries[i];\n buffers.delete(key);\n }\n }\n\n // Buffer management\n if (!isolateByCategory && !isolateByContext) {\n // Single global buffer\n const buffer: LogRecord[] = [];\n let triggered = false;\n\n return (record: LogRecord) => {\n if (triggered) {\n // Already triggered, pass through directly\n sink(record);\n return;\n }\n\n // Check if this record triggers flush\n if (compareLogLevel(record.level, triggerLevel) >= 0) {\n triggered = true;\n\n // Flush buffer\n for (const bufferedRecord of buffer) {\n sink(bufferedRecord);\n }\n buffer.length = 0;\n\n // Send trigger record\n sink(record);\n } else if (\n bufferLevel != null &&\n compareLogLevel(record.level, bufferLevel) > 0\n ) {\n // Record is above bufferLevel but below triggerLevel: pass through\n sink(record);\n } else {\n // Buffer the record\n buffer.push(record);\n\n // Enforce max buffer size\n while (buffer.length > maxBufferSize) {\n buffer.shift();\n }\n }\n };\n } else {\n // Category and/or context-isolated buffers\n const buffers = new Map<string, BufferMetadata>();\n const triggered = new Map<string, number>();\n let accessCounter = 0;\n\n // Set up TTL cleanup timer if enabled\n let cleanupTimer: ReturnType<typeof setInterval> | null = null;\n if (hasTtl) {\n cleanupTimer = setInterval(() => {\n cleanupExpiredBuffers(buffers, triggered);\n }, cleanupIntervalMs);\n }\n\n const fingersCrossedSink = (record: LogRecord) => {\n const bufferKey = getBufferKey(record.category, record.properties);\n\n // Check if this buffer is already triggered\n if (triggered.has(bufferKey)) {\n triggered.set(bufferKey, Date.now());\n sink(record);\n return;\n }\n\n // Check if this record triggers flush\n if (compareLogLevel(record.level, triggerLevel) >= 0) {\n // Find all buffers that should be flushed\n const keysToFlush = new Set<string>();\n\n for (const [bufferedKey] of buffers) {\n if (bufferedKey === bufferKey) {\n keysToFlush.add(bufferedKey);\n } else {\n const { category: bufferedCategory, context: bufferedContext } =\n parseBufferKey(bufferedKey);\n const { context: triggerContext } = parseBufferKey(bufferKey);\n\n // Check context match\n let contextMatches = true;\n if (isolateByContext) {\n contextMatches = bufferedContext === triggerContext;\n }\n\n // Check category match\n let categoryMatches = false;\n if (!isolateByCategory) {\n // No category isolation, so all categories match if context matches\n categoryMatches = contextMatches;\n } else if (shouldFlushBuffer) {\n try {\n categoryMatches = shouldFlushBuffer(\n record.category,\n bufferedCategory,\n );\n } catch {\n // Ignore errors from custom matcher\n }\n } else {\n // Same category only\n categoryMatches = getCategoryKey(record.category) ===\n getCategoryKey(bufferedCategory);\n }\n\n // Both must match for the buffer to be flushed\n if (contextMatches && categoryMatches) {\n keysToFlush.add(bufferedKey);\n }\n }\n }\n\n // Flush matching buffers\n const allRecordsToFlush: LogRecord[] = [];\n const triggeredAt = Date.now();\n for (const key of keysToFlush) {\n const metadata = buffers.get(key);\n if (metadata) {\n allRecordsToFlush.push(...metadata.buffer);\n buffers.delete(key);\n triggered.set(key, triggeredAt);\n }\n }\n\n // Sort by timestamp to maintain chronological order\n allRecordsToFlush.sort((a, b) => a.timestamp - b.timestamp);\n\n // Flush all records\n for (const bufferedRecord of allRecordsToFlush) {\n sink(bufferedRecord);\n }\n\n // Mark trigger buffer as triggered and send trigger record\n triggered.set(bufferKey, triggeredAt);\n sink(record);\n } else if (\n bufferLevel != null &&\n compareLogLevel(record.level, bufferLevel) > 0\n ) {\n // Record is above bufferLevel but below triggerLevel: pass through\n sink(record);\n } else {\n // Buffer the record\n let metadata = buffers.get(bufferKey);\n if (!metadata) {\n // Apply LRU eviction if adding new buffer would exceed capacity\n if (hasLru && buffers.size >= maxContexts!) {\n // Calculate how many buffers to evict to make room for the new one\n const numToEvict = buffers.size - maxContexts! + 1;\n evictLruBuffers(buffers, numToEvict);\n }\n\n metadata = {\n buffer: [],\n lastAccess: ++accessCounter,\n };\n buffers.set(bufferKey, metadata);\n } else {\n // Update last access order for LRU\n metadata.lastAccess = ++accessCounter;\n }\n\n metadata.buffer.push(record);\n\n // Enforce max buffer size per buffer\n while (metadata.buffer.length > maxBufferSize) {\n metadata.buffer.shift();\n }\n }\n };\n\n // Add disposal functionality to clean up timer\n if (cleanupTimer !== null) {\n (fingersCrossedSink as Sink & Disposable)[Symbol.dispose] = () => {\n if (cleanupTimer !== null) {\n clearInterval(cleanupTimer);\n cleanupTimer = null;\n }\n };\n }\n\n return fingersCrossedSink;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA+CA,SAAgB,WAAWA,MAAYC,QAA0B;CAC/D,MAAM,aAAa,SAAS,OAAO;CACnC,MAAMC,WAAyD,CAC7DC,WACG;AACH,MAAI,WAAW,OAAO,CAAE,MAAK,OAAO;CACrC;CACD,MAAM,iBAAiB;AACvB,KAAI,OAAO,WAAW,eACpB,UAAS,OAAO,WAAW,eAAe,OAAO,UAAU,KAAK,KAAK;AAEvE,KAAI,OAAO,gBAAgB,eACzB,UAAS,OAAO,gBAAgB,e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