@logtape/logtape 2.2.3 → 2.2.5

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 CHANGED
@@ -29,6 +29,7 @@ const asyncFilterDisposables = /* @__PURE__ */ new Set();
29
29
  * Async sink disposables to dispose when resetting the configuration.
30
30
  */
31
31
  const asyncSinkDisposables = /* @__PURE__ */ new Set();
32
+ let unregisterDisposeHook;
32
33
  /**
33
34
  * Check if a config is for the meta logger.
34
35
  */
@@ -37,19 +38,35 @@ function isLoggerConfigMeta(cfg) {
37
38
  return category.length === 0 || category.length === 1 && category[0] === "logtape" || category.length === 2 && category[0] === "logtape" && category[1] === "meta";
38
39
  }
39
40
  function registerDisposeHook(allowAsync) {
41
+ unregisterDisposeHook?.();
42
+ unregisterDisposeHook = void 0;
40
43
  const handler = allowAsync ? dispose : disposeSync;
41
44
  if (typeof globalThis.EdgeRuntime !== "string" && "process" in globalThis && !("Deno" in globalThis)) {
42
45
  const proc = globalThis.process;
43
46
  const onMethod = proc?.["on"];
44
47
  if (typeof onMethod === "function") {
45
48
  onMethod.call(proc, "exit", handler);
49
+ unregisterDisposeHook = () => {
50
+ const offMethod = proc?.["off"] ?? proc?.["removeListener"];
51
+ if (typeof offMethod === "function") offMethod.call(proc, "exit", handler);
52
+ };
46
53
  return;
47
54
  }
48
55
  }
49
56
  const addEventListenerMethod = globalThis.addEventListener;
50
57
  if (typeof addEventListenerMethod !== "function") return;
51
- if ("Deno" in globalThis) addEventListenerMethod.call(globalThis, "unload", handler);
52
- else addEventListenerMethod.call(globalThis, "pagehide", handler);
58
+ const removeEventListenerMethod = globalThis.removeEventListener;
59
+ if ("Deno" in globalThis) {
60
+ addEventListenerMethod.call(globalThis, "unload", handler);
61
+ if (typeof removeEventListenerMethod === "function") unregisterDisposeHook = () => {
62
+ removeEventListenerMethod.call(globalThis, "unload", handler);
63
+ };
64
+ } else {
65
+ addEventListenerMethod.call(globalThis, "pagehide", handler);
66
+ if (typeof removeEventListenerMethod === "function") unregisterDisposeHook = () => {
67
+ removeEventListenerMethod.call(globalThis, "pagehide", handler);
68
+ };
69
+ }
53
70
  }
54
71
  /**
55
72
  * Configure the loggers with the specified configuration.
@@ -218,6 +235,8 @@ function resetSync() {
218
235
  resetInternal();
219
236
  }
220
237
  function resetInternal() {
238
+ unregisterDisposeHook?.();
239
+ unregisterDisposeHook = void 0;
221
240
  const rootLogger = require_logger.LoggerImpl.getLogger([]);
222
241
  rootLogger.resetDescendants();
223
242
  delete rootLogger.contextLocalStorage;
@@ -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,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;;;;;;;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"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;EA+IF,mBAAS,CAAA,EA9LP,mBA8LO,CA9La,MA8Lb,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;EAAA;;;EAGI,KAAzB,CAAA,EAAA,OAAA;;AAAoC;AAgD9C;;AACiB,UAvOA,YAuOA,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAO;;AAAR;AA4HhB;EAOsB,QAAK,EAAA,MAAA,GAAI,MAAA,EAAO;EAUtB;AAkBhB;AA8BA;EA8Fa,KAAA,CAAA,EArfH,OAqfe,EAAA;;;;;;;;;;;;;;YAreb;;;;;;gBAOI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA+IM,oEAGZ,OAAO,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgDvB,wEACN,OAAO,SAAS;;;;;iBA4HV,SAAA,CAAA,GAAa;;;;iBAOP,KAAA,CAAA,GAAS;;;;;;iBAUf,SAAA,CAAA;;;;iBAkBM,OAAA,CAAA,GAAW;;;;;;iBA8BjB,WAAA,CAAA;;;;cA8FH,WAAA,SAAoB,KAAK"}
package/dist/config.js CHANGED
@@ -29,6 +29,7 @@ const asyncFilterDisposables = /* @__PURE__ */ new Set();
29
29
  * Async sink disposables to dispose when resetting the configuration.
30
30
  */
31
31
  const asyncSinkDisposables = /* @__PURE__ */ new Set();
32
+ let unregisterDisposeHook;
32
33
  /**
33
34
  * Check if a config is for the meta logger.
34
35
  */
@@ -37,19 +38,35 @@ function isLoggerConfigMeta(cfg) {
37
38
  return category.length === 0 || category.length === 1 && category[0] === "logtape" || category.length === 2 && category[0] === "logtape" && category[1] === "meta";
38
39
  }
39
40
  function registerDisposeHook(allowAsync) {
41
+ unregisterDisposeHook?.();
42
+ unregisterDisposeHook = void 0;
40
43
  const handler = allowAsync ? dispose : disposeSync;
41
44
  if (typeof globalThis.EdgeRuntime !== "string" && "process" in globalThis && !("Deno" in globalThis)) {
42
45
  const proc = globalThis.process;
43
46
  const onMethod = proc?.["on"];
44
47
  if (typeof onMethod === "function") {
45
48
  onMethod.call(proc, "exit", handler);
49
+ unregisterDisposeHook = () => {
50
+ const offMethod = proc?.["off"] ?? proc?.["removeListener"];
51
+ if (typeof offMethod === "function") offMethod.call(proc, "exit", handler);
52
+ };
46
53
  return;
47
54
  }
48
55
  }
49
56
  const addEventListenerMethod = globalThis.addEventListener;
50
57
  if (typeof addEventListenerMethod !== "function") return;
51
- if ("Deno" in globalThis) addEventListenerMethod.call(globalThis, "unload", handler);
52
- else addEventListenerMethod.call(globalThis, "pagehide", handler);
58
+ const removeEventListenerMethod = globalThis.removeEventListener;
59
+ if ("Deno" in globalThis) {
60
+ addEventListenerMethod.call(globalThis, "unload", handler);
61
+ if (typeof removeEventListenerMethod === "function") unregisterDisposeHook = () => {
62
+ removeEventListenerMethod.call(globalThis, "unload", handler);
63
+ };
64
+ } else {
65
+ addEventListenerMethod.call(globalThis, "pagehide", handler);
66
+ if (typeof removeEventListenerMethod === "function") unregisterDisposeHook = () => {
67
+ removeEventListenerMethod.call(globalThis, "pagehide", handler);
68
+ };
69
+ }
53
70
  }
54
71
  /**
55
72
  * Configure the loggers with the specified configuration.
@@ -218,6 +235,8 @@ function resetSync() {
218
235
  resetInternal();
219
236
  }
220
237
  function resetInternal() {
238
+ unregisterDisposeHook?.();
239
+ unregisterDisposeHook = void 0;
221
240
  const rootLogger = LoggerImpl.getLogger([]);
222
241
  rootLogger.resetDescendants();
223
242
  delete rootLogger.contextLocalStorage;
@@ -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 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,IAAI;;;;AAK/C,MAAMC,kCAAmC,IAAI;;;;AAK7C,MAAMC,yCAA+C,IAAI;;;;AAKzD,MAAMC,uCAA6C,IAAI;;;;AAKvD,SAAS,mBACPC,KACS;CACT,MAAM,WAAW,MAAM,QAAQ,IAAI,SAAS,GAAG,IAAI,WAAW,CAAC,IAAI,QAAS;AAC5E,QAAO,SAAS,WAAW,KACxB,SAAS,WAAW,KAAK,SAAS,OAAO,aACzC,SAAS,WAAW,KACnB,SAAS,OAAO,aAChB,SAAS,OAAO;AACrB;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;AACx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;CAEJ,UAAS;AACR,cAAY,OAAO;CACpB;AACD,oBAAmB,OAAO;AAC3B;AAED,eAAe,sBAAqC;AAClD,OAAM,wBAAwB,uBAAuB;AACtD;AAED,eAAe,oBAAmC;AAChD,OAAM,wBAAwB,qBAAqB;AACpD;AAED,eAAe,wBACbE,aACe;CACf,MAAMC,WAAgC,CAAE;AACxC,KAAI;AACF,OAAK,MAAM,cAAc,YACvB,KAAI;AACF,YAAS,KAAK,QAAQ,QAAQ,WAAW,OAAO,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>","unregisterDisposeHook: (() => void) | undefined","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\nlet unregisterDisposeHook: (() => void) | undefined;\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 unregisterDisposeHook?.();\n unregisterDisposeHook = undefined;\n\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 unregisterDisposeHook = () => {\n const offMethod = proc?.[\"off\"] ?? proc?.[\"removeListener\"];\n if (typeof offMethod === \"function\") {\n offMethod.call(proc, \"exit\", handler);\n }\n };\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 // deno-lint-ignore no-explicit-any\n const removeEventListenerMethod = (globalThis as any).removeEventListener;\n\n if (\"Deno\" in globalThis) {\n addEventListenerMethod.call(globalThis, \"unload\", handler);\n if (typeof removeEventListenerMethod === \"function\") {\n unregisterDisposeHook = () => {\n removeEventListenerMethod.call(globalThis, \"unload\", handler);\n };\n }\n } else {\n addEventListenerMethod.call(globalThis, \"pagehide\", handler);\n if (typeof removeEventListenerMethod === \"function\") {\n unregisterDisposeHook = () => {\n removeEventListenerMethod.call(globalThis, \"pagehide\", handler);\n };\n }\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 unregisterDisposeHook?.();\n unregisterDisposeHook = undefined;\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,IAAI;;;;AAK/C,MAAMC,kCAAmC,IAAI;;;;AAK7C,MAAMC,yCAA+C,IAAI;;;;AAKzD,MAAMC,uCAA6C,IAAI;AAEvD,IAAIC;;;;AAKJ,SAAS,mBACPC,KACS;CACT,MAAM,WAAW,MAAM,QAAQ,IAAI,SAAS,GAAG,IAAI,WAAW,CAAC,IAAI,QAAS;AAC5E,QAAO,SAAS,WAAW,KACxB,SAAS,WAAW,KAAK,SAAS,OAAO,aACzC,SAAS,WAAW,KACnB,SAAS,OAAO,aAChB,SAAS,OAAO;AACrB;AAED,SAAS,oBAAoBC,YAA2B;AACtD,0BAAyB;AACzB;CAEA,MAAM,UAAU,aAAa,UAAU;AAEvC,YAEU,W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;AACtC,OAAM,IAAI,eACR,QACA;AAEH;;;;AAKD,IAAa,cAAb,cAAiC,MAAM;;;;;CAKrC,YAAYC,SAAiB;AAC3B,QAAM,QAAQ;AACd,OAAK,OAAO;CACb;AACF"}
package/dist/sink.cjs CHANGED
@@ -262,41 +262,6 @@ function fromAsyncSink(asyncSink) {
262
262
  return sink;
263
263
  }
264
264
  const _asyncSinkError = Symbol.for("logtape.asyncSinkError");
265
- /**
266
- * Creates a sink that buffers log records until a trigger level is reached.
267
- * This pattern, known as "fingers crossed" logging, keeps detailed debug logs
268
- * in memory and only outputs them when an error or other significant event occurs.
269
- *
270
- * @example Basic usage with default settings
271
- * ```typescript
272
- * const sink = fingersCrossed(getConsoleSink());
273
- * // Debug and info logs are buffered
274
- * // When an error occurs, all buffered logs + the error are output
275
- * ```
276
- *
277
- * @example Custom trigger level and buffer size
278
- * ```typescript
279
- * const sink = fingersCrossed(getConsoleSink(), {
280
- * triggerLevel: "warning", // Trigger on warning or higher
281
- * maxBufferSize: 500 // Keep last 500 records
282
- * });
283
- * ```
284
- *
285
- * @example Category isolation
286
- * ```typescript
287
- * const sink = fingersCrossed(getConsoleSink(), {
288
- * isolateByCategory: "descendant" // Separate buffers per category
289
- * });
290
- * // Error in ["app"] triggers flush of ["app"] and ["app", "module"] buffers
291
- * // But not ["other"] buffer
292
- * ```
293
- *
294
- * @param sink The sink to wrap. Buffered records are sent to this sink when
295
- * triggered.
296
- * @param options Configuration options for the fingers crossed behavior.
297
- * @returns A sink that buffers records until the trigger level is reached.
298
- * @since 1.1.0
299
- */
300
265
  function fingersCrossed(sink, options = {}) {
301
266
  const triggerLevel = options.triggerLevel ?? "error";
302
267
  const bufferLevel = options.bufferLevel;
@@ -308,6 +273,21 @@ function fingersCrossed(sink, options = {}) {
308
273
  const maxContexts = isolateByContext?.maxContexts;
309
274
  const hasTtl = bufferTtlMs != null && bufferTtlMs > 0;
310
275
  const hasLru = maxContexts != null && maxContexts > 0;
276
+ function wrapSink(wrapped, disposeSelf) {
277
+ const disposableSink = sink;
278
+ const disposableWrapped = wrapped;
279
+ const disposeSink = disposableSink[Symbol.dispose];
280
+ const asyncDisposeSink = disposableSink[Symbol.asyncDispose];
281
+ if (disposeSelf != null || disposeSink != null) disposableWrapped[Symbol.dispose] = () => {
282
+ disposeSelf?.();
283
+ disposeSink?.call(sink);
284
+ };
285
+ if (asyncDisposeSink != null) disposableWrapped[Symbol.asyncDispose] = async () => {
286
+ disposeSelf?.();
287
+ await asyncDisposeSink.call(sink);
288
+ };
289
+ return disposableWrapped;
290
+ }
311
291
  try {
312
292
  require_level.compareLogLevel("trace", triggerLevel);
313
293
  } catch (error) {
@@ -407,7 +387,7 @@ function fingersCrossed(sink, options = {}) {
407
387
  if (!isolateByCategory && !isolateByContext) {
408
388
  const buffer = [];
409
389
  let triggered = false;
410
- return (record) => {
390
+ return wrapSink((record) => {
411
391
  if (triggered) {
412
392
  sink(record);
413
393
  return;
@@ -422,7 +402,7 @@ function fingersCrossed(sink, options = {}) {
422
402
  buffer.push(record);
423
403
  while (buffer.length > maxBufferSize) buffer.shift();
424
404
  }
425
- };
405
+ });
426
406
  } else {
427
407
  const buffers = /* @__PURE__ */ new Map();
428
408
  const triggered = /* @__PURE__ */ new Map();
@@ -486,13 +466,12 @@ function fingersCrossed(sink, options = {}) {
486
466
  while (metadata.buffer.length > maxBufferSize) metadata.buffer.shift();
487
467
  }
488
468
  };
489
- if (cleanupTimer !== null) fingersCrossedSink[Symbol.dispose] = () => {
469
+ return wrapSink(fingersCrossedSink, cleanupTimer === null ? void 0 : () => {
490
470
  if (cleanupTimer !== null) {
491
471
  clearInterval(cleanupTimer);
492
472
  cleanupTimer = null;
493
473
  }
494
- };
495
- return fingersCrossedSink;
474
+ });
496
475
  }
497
476
  }
498
477
 
package/dist/sink.d.cts CHANGED
@@ -382,7 +382,10 @@ interface FingersCrossedOptions {
382
382
  * @returns A sink that buffers records until the trigger level is reached.
383
383
  * @since 1.1.0
384
384
  */
385
- declare function fingersCrossed(sink: Sink, options?: FingersCrossedOptions): Sink | (Sink & Disposable);
385
+ declare function fingersCrossed(sink: Sink & Disposable & AsyncDisposable, options?: FingersCrossedOptions): Sink & Disposable & AsyncDisposable;
386
+ declare function fingersCrossed(sink: Sink & AsyncDisposable, options?: FingersCrossedOptions): Sink & AsyncDisposable & Partial<Disposable>;
387
+ declare function fingersCrossed(sink: Sink & Disposable, options?: FingersCrossedOptions): Sink & Disposable;
388
+ declare function fingersCrossed(sink: Sink, options?: FingersCrossedOptions): Sink & Partial<Disposable>;
386
389
  //#endregion
387
390
  export { AsyncSink, ConsoleSinkOptions, FingersCrossedOptions, Sink, StreamSinkOptions, fingersCrossed, fromAsyncSink, getConsoleSink, getStreamSink, withFilter };
388
391
  //# sourceMappingURL=sink.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sink.d.cts","names":[],"sources":["../src/sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AA+BA;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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2C1D;;;;AAiCgB;AA0KjC;;;;;;;;;AAGsC;AACtC;;;;;;;EAGyB,WAAW,CAAA,EAAA,OAAA,GAAA;IAAR;AAAO;AACnC;;IACQ,UAAA,CAAA,EAAA,MAAA;IAAO;;;;IAEK,aAAA,CAAA,EAAA,MAAA;EACJ,CAAA;;;;;;;AAGC;;;;;;;;;;;;;;;;;;;iBA7jBD,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,OAAO,aAAa,2BAChB,wBACT,OAAO,aAAa;iBACP,cAAA,OACR,OAAO,2BACH,wBACT,OAAO,kBAAkB,QAAQ;iBACpB,cAAA,OACR,OAAO,sBACH,wBACT,OAAO;iBACM,cAAA,OACR,gBACI,wBACT,OAAO,QAAQ"}
package/dist/sink.d.ts CHANGED
@@ -382,7 +382,10 @@ interface FingersCrossedOptions {
382
382
  * @returns A sink that buffers records until the trigger level is reached.
383
383
  * @since 1.1.0
384
384
  */
385
- declare function fingersCrossed(sink: Sink, options?: FingersCrossedOptions): Sink | (Sink & Disposable);
385
+ declare function fingersCrossed(sink: Sink & Disposable & AsyncDisposable, options?: FingersCrossedOptions): Sink & Disposable & AsyncDisposable;
386
+ declare function fingersCrossed(sink: Sink & AsyncDisposable, options?: FingersCrossedOptions): Sink & AsyncDisposable & Partial<Disposable>;
387
+ declare function fingersCrossed(sink: Sink & Disposable, options?: FingersCrossedOptions): Sink & Disposable;
388
+ declare function fingersCrossed(sink: Sink, options?: FingersCrossedOptions): Sink & Partial<Disposable>;
386
389
  //#endregion
387
390
  export { AsyncSink, ConsoleSinkOptions, FingersCrossedOptions, Sink, StreamSinkOptions, fingersCrossed, fromAsyncSink, getConsoleSink, getStreamSink, withFilter };
388
391
  //# sourceMappingURL=sink.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sink.d.ts","names":[],"sources":["../src/sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AA+BA;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;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AEK,aAAA,CAAA,EAAA,MAAA;EACJ,CAAA;;;;;;;AAGC;;;;;;;;;;;;;;;;;;;iBA7jBD,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,OAAO,aAAa,2BAChB,wBACT,OAAO,aAAa;iBACP,cAAA,OACR,OAAO,2BACH,wBACT,OAAO,kBAAkB,QAAQ;iBACpB,cAAA,OACR,OAAO,sBACH,wBACT,OAAO;iBACM,cAAA,OACR,gBACI,wBACT,OAAO,QAAQ"}
package/dist/sink.js CHANGED
@@ -262,41 +262,6 @@ function fromAsyncSink(asyncSink) {
262
262
  return sink;
263
263
  }
264
264
  const _asyncSinkError = Symbol.for("logtape.asyncSinkError");
265
- /**
266
- * Creates a sink that buffers log records until a trigger level is reached.
267
- * This pattern, known as "fingers crossed" logging, keeps detailed debug logs
268
- * in memory and only outputs them when an error or other significant event occurs.
269
- *
270
- * @example Basic usage with default settings
271
- * ```typescript
272
- * const sink = fingersCrossed(getConsoleSink());
273
- * // Debug and info logs are buffered
274
- * // When an error occurs, all buffered logs + the error are output
275
- * ```
276
- *
277
- * @example Custom trigger level and buffer size
278
- * ```typescript
279
- * const sink = fingersCrossed(getConsoleSink(), {
280
- * triggerLevel: "warning", // Trigger on warning or higher
281
- * maxBufferSize: 500 // Keep last 500 records
282
- * });
283
- * ```
284
- *
285
- * @example Category isolation
286
- * ```typescript
287
- * const sink = fingersCrossed(getConsoleSink(), {
288
- * isolateByCategory: "descendant" // Separate buffers per category
289
- * });
290
- * // Error in ["app"] triggers flush of ["app"] and ["app", "module"] buffers
291
- * // But not ["other"] buffer
292
- * ```
293
- *
294
- * @param sink The sink to wrap. Buffered records are sent to this sink when
295
- * triggered.
296
- * @param options Configuration options for the fingers crossed behavior.
297
- * @returns A sink that buffers records until the trigger level is reached.
298
- * @since 1.1.0
299
- */
300
265
  function fingersCrossed(sink, options = {}) {
301
266
  const triggerLevel = options.triggerLevel ?? "error";
302
267
  const bufferLevel = options.bufferLevel;
@@ -308,6 +273,21 @@ function fingersCrossed(sink, options = {}) {
308
273
  const maxContexts = isolateByContext?.maxContexts;
309
274
  const hasTtl = bufferTtlMs != null && bufferTtlMs > 0;
310
275
  const hasLru = maxContexts != null && maxContexts > 0;
276
+ function wrapSink(wrapped, disposeSelf) {
277
+ const disposableSink = sink;
278
+ const disposableWrapped = wrapped;
279
+ const disposeSink = disposableSink[Symbol.dispose];
280
+ const asyncDisposeSink = disposableSink[Symbol.asyncDispose];
281
+ if (disposeSelf != null || disposeSink != null) disposableWrapped[Symbol.dispose] = () => {
282
+ disposeSelf?.();
283
+ disposeSink?.call(sink);
284
+ };
285
+ if (asyncDisposeSink != null) disposableWrapped[Symbol.asyncDispose] = async () => {
286
+ disposeSelf?.();
287
+ await asyncDisposeSink.call(sink);
288
+ };
289
+ return disposableWrapped;
290
+ }
311
291
  try {
312
292
  compareLogLevel("trace", triggerLevel);
313
293
  } catch (error) {
@@ -407,7 +387,7 @@ function fingersCrossed(sink, options = {}) {
407
387
  if (!isolateByCategory && !isolateByContext) {
408
388
  const buffer = [];
409
389
  let triggered = false;
410
- return (record) => {
390
+ return wrapSink((record) => {
411
391
  if (triggered) {
412
392
  sink(record);
413
393
  return;
@@ -422,7 +402,7 @@ function fingersCrossed(sink, options = {}) {
422
402
  buffer.push(record);
423
403
  while (buffer.length > maxBufferSize) buffer.shift();
424
404
  }
425
- };
405
+ });
426
406
  } else {
427
407
  const buffers = /* @__PURE__ */ new Map();
428
408
  const triggered = /* @__PURE__ */ new Map();
@@ -486,13 +466,12 @@ function fingersCrossed(sink, options = {}) {
486
466
  while (metadata.buffer.length > maxBufferSize) metadata.buffer.shift();
487
467
  }
488
468
  };
489
- if (cleanupTimer !== null) fingersCrossedSink[Symbol.dispose] = () => {
469
+ return wrapSink(fingersCrossedSink, cleanupTimer === null ? void 0 : () => {
490
470
  if (cleanupTimer !== null) {
491
471
  clearInterval(cleanupTimer);
492
472
  cleanupTimer = null;
493
473
  }
494
- };
495
- return fingersCrossedSink;
474
+ });
496
475
  }
497
476
  }
498
477
 
package/dist/sink.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"sink.js","names":["sink: T","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\nconst immediateSinkSymbol = Symbol.for(\n \"LogTape.sinkSnapshotPolicy.immediate\",\n);\n\nfunction markSinkAsImmediate<T extends Sink>(sink: T): T {\n Object.defineProperty(sink, immediateSinkSymbol, {\n value: true,\n });\n return sink;\n}\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 markSinkAsImmediate(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":";;;;;;AAWA,MAAM,sBAAsB,OAAO,IACjC,uCACD;AAED,SAAS,oBAAoCA,MAAY;AACvD,QAAO,eAAe,MAAM,qBAAqB,EAC/C,OAAO,KACR,EAAC;AACF,QAAO;AACR;;;;;;;;;;;;;;;AAsCD,SAAgB,WAAWC,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,eAAe,OAAO,eAAe,KACnE,KACD;AAEH,QAAO;AACR;;;;;;;;;;;;;;;;;;;;;;;;;AA6ED,SAAgB,cACdC,QACAC,UAA6B,CAAE,GACP;CACxB,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,UAAU,QAAQ,WAAW,IAAI;CACvC,MAAM,SAAS,OAAO,WAAW;AAEjC,MAAK,QAAQ,aAAa;EACxB,IAAI,cAAc,QAAQ,SAAS;EACnC,MAAMC,OAA+B,CAACH,WAAsB;GAC1D,MAAM,QAAQ,QAAQ,OAAO,UAAU,OAAO,CAAC;AAC/C,iBAAc,YACX,KAAK,MAAM,OAAO,MAAM,CACxB,KAAK,MAAM,OAAO,MAAM,MAAM,CAAC;EACnC;AACD,OAAK,OAAO,gBAAgB,YAAY;AACtC,SAAM;AACN,SAAM,OAAO,OAAO;EACrB;AACD,SAAO,oBAAoB,KAAK;CACjC;CAG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BAAkB,kBAC3B,MAAK,eAAe;AAItB,cAAU,IAAI,WAAW,YAAY;AACrC,SAAK,OAAO;GACb,WACC,eAAe,QACf,gBAAgB,OAAO,OAAO,YAAY,GAAG,EAG7C,MAAK,OAAO;QACP;IAEL,IAAI,WAAW,QAAQ,IAAI,UAAU;AACrC,SAAK,UAAU;AAEb,SAAI,UAAU,QAAQ,QAAQ,aAAc;MAE1C,MAAM,aAAa,QAAQ,OAAO,cAAe;AACjD,sBAAgB,SAAS,WAAW;KACrC;AAED,gBAAW;MACT,QAAQ,CAAE;MACV,YAAY,EAAE;KACf;AACD,aAAQ,IAAI,WAAW,SAAS;IACjC,MAEC,UAAS,aAAa,EAAE;AAG1B,aAAS,OAAO,KAAK,OAAO;AAG5B,WAAO,SAAS,OAAO,SAAS,cAC9B,UAAS,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: T","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","wrapped: Sink","disposeSelf?: () => void","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\nconst immediateSinkSymbol = Symbol.for(\n \"LogTape.sinkSnapshotPolicy.immediate\",\n);\n\nfunction markSinkAsImmediate<T extends Sink>(sink: T): T {\n Object.defineProperty(sink, immediateSinkSymbol, {\n value: true,\n });\n return sink;\n}\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 markSinkAsImmediate(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 & Disposable & AsyncDisposable,\n options?: FingersCrossedOptions,\n): Sink & Disposable & AsyncDisposable;\nexport function fingersCrossed(\n sink: Sink & AsyncDisposable,\n options?: FingersCrossedOptions,\n): Sink & AsyncDisposable & Partial<Disposable>;\nexport function fingersCrossed(\n sink: Sink & Disposable,\n options?: FingersCrossedOptions,\n): Sink & Disposable;\nexport function fingersCrossed(\n sink: Sink,\n options?: FingersCrossedOptions,\n): Sink & Partial<Disposable>;\nexport function fingersCrossed(\n sink: Sink,\n options: FingersCrossedOptions = {},\n): Sink & Partial<Disposable & AsyncDisposable> {\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 function wrapSink(\n wrapped: Sink,\n disposeSelf?: () => void,\n ): Sink & Partial<Disposable & AsyncDisposable> {\n const disposableSink = sink as\n & Sink\n & Partial<Disposable & AsyncDisposable>;\n const disposableWrapped = wrapped as\n & Sink\n & Partial<Disposable & AsyncDisposable>;\n const disposeSink = disposableSink[Symbol.dispose];\n const asyncDisposeSink = disposableSink[Symbol.asyncDispose];\n\n if (disposeSelf != null || disposeSink != null) {\n disposableWrapped[Symbol.dispose] = () => {\n disposeSelf?.();\n disposeSink?.call(sink);\n };\n }\n if (asyncDisposeSink != null) {\n disposableWrapped[Symbol.asyncDispose] = async () => {\n disposeSelf?.();\n await asyncDisposeSink.call(sink);\n };\n }\n return disposableWrapped;\n }\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 wrapSink((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 return wrapSink(\n fingersCrossedSink,\n cleanupTimer === null ? undefined : () => {\n if (cleanupTimer !== null) {\n clearInterval(cleanupTimer);\n cleanupTimer = null;\n }\n },\n );\n }\n}\n"],"mappings":";;;;;;AAWA,MAAM,sBAAsB,OAAO,IACjC,uCACD;AAED,SAAS,oBAAoCA,MAAY;AACvD,QAAO,eAAe,MAAM,qBAAqB,EAC/C,OAAO,KACR,EAAC;AACF,QAAO;A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3B,MAAK,eAAe;AAItB,cAAU,IAAI,WAAW,YAAY;AACrC,SAAK,OAAO;GACb,WACC,eAAe,QACf,gBAAgB,OAAO,OAAO,YAAY,GAAG,EAG7C,MAAK,OAAO;QACP;IAEL,IAAI,WAAW,QAAQ,IAAI,UAAU;AACrC,SAAK,UAAU;AAEb,SAAI,UAAU,QAAQ,QAAQ,aAAc;MAE1C,MAAM,aAAa,QAAQ,OAAO,cAAe;AACjD,sBAAgB,SAAS,WAAW;KACrC;AAED,gBAAW;MACT,QAAQ,CAAE;MACV,YAAY,EAAE;KACf;AACD,aAAQ,IAAI,WAAW,SAAS;IACjC,MAEC,UAAS,aAAa,EAAE;AAG1B,aAAS,OAAO,KAAK,OAAO;AAG5B,WAAO,SAAS,OAAO,SAAS,cAC9B,UAAS,OAAO,OAAO;GAE1B;EACF;AAED,SAAO,SACL,oBACA,iBAAiB,gBAAmB,MAAM;AACxC,OAAI,iBAAiB,MAAM;AACzB,kBAAc,aAAa;AAC3B,mBAAe;GAChB;EACF,EACF;CACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/logtape",
3
- "version": "2.2.3",
3
+ "version": "2.2.5",
4
4
  "description": "Unobtrusive logging library with zero dependencies—library-first design for Deno/Node.js/Bun/browsers/edge",
5
5
  "keywords": [
6
6
  "logging",