@logtape/logtape 1.0.0-dev.206 → 1.0.0-dev.208

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/config.test.ts CHANGED
@@ -60,7 +60,6 @@ test("configure()", async () => {
60
60
  category: ["my-app", "bar"],
61
61
  sinks: ["c"],
62
62
  filters: ["debug"],
63
- level: "info", // deprecated
64
63
  lowestLevel: "info",
65
64
  },
66
65
  ],
@@ -322,7 +321,6 @@ test("configureSync()", async () => {
322
321
  category: ["my-app", "bar"],
323
322
  sinks: ["c"],
324
323
  filters: ["debug"],
325
- level: "info", // deprecated
326
324
  lowestLevel: "info",
327
325
  },
328
326
  ],
package/config.ts CHANGED
@@ -70,14 +70,6 @@ export interface LoggerConfig<
70
70
  */
71
71
  filters?: TFilterId[];
72
72
 
73
- /**
74
- * The log level to filter by. If `null`, the logger will reject all
75
- * records.
76
- * @deprecated Use `filters` instead for backward compatibility, or use
77
- * `lowestLevel` for less-misleading behavior.
78
- */
79
- level?: LogLevel | null;
80
-
81
73
  /**
82
74
  * The lowest log level to accept. If `null`, the logger will reject all
83
75
  * records.
@@ -241,7 +233,6 @@ function configureInternal<
241
233
  currentConfig = config;
242
234
 
243
235
  let metaConfigured = false;
244
- let levelUsed = false;
245
236
  const configuredCategories = new Set<string>();
246
237
 
247
238
  for (const cfg of config.loggers) {
@@ -273,10 +264,6 @@ function configureInternal<
273
264
  if (cfg.lowestLevel !== undefined) {
274
265
  logger.lowestLevel = cfg.lowestLevel;
275
266
  }
276
- if (cfg.level !== undefined) {
277
- levelUsed = true;
278
- logger.filters.push(toFilter(cfg.level));
279
- }
280
267
  for (const filterId of cfg.filters ?? []) {
281
268
  const filter = config.filters?.[filterId];
282
269
  if (filter === undefined) {
@@ -339,14 +326,6 @@ function configureInternal<
339
326
  "<https://logtape.org/manual/categories#meta-logger>.",
340
327
  { metaLoggerCategory: ["logtape", "meta"], dismissLevel: "info" },
341
328
  );
342
-
343
- if (levelUsed) {
344
- meta.warn(
345
- "The level option is deprecated in favor of lowestLevel option. " +
346
- "Please update your configuration. See also " +
347
- "<https://logtape.org/manual/levels#configuring-severity-levels>.",
348
- );
349
- }
350
329
  }
351
330
 
352
331
  /**
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/logtape",
3
- "version": "1.0.0-dev.206+52e07555",
3
+ "version": "1.0.0-dev.208+b3f9fea2",
4
4
  "license": "MIT",
5
5
  "exports": "./mod.ts",
6
6
  "imports": {
package/dist/config.cjs CHANGED
@@ -123,7 +123,6 @@ function configureSync(config) {
123
123
  function configureInternal(config, allowAsync) {
124
124
  currentConfig = config;
125
125
  let metaConfigured = false;
126
- let levelUsed = false;
127
126
  const configuredCategories = /* @__PURE__ */ new Set();
128
127
  for (const cfg of config.loggers) {
129
128
  if (isLoggerConfigMeta(cfg)) metaConfigured = true;
@@ -138,10 +137,6 @@ function configureInternal(config, allowAsync) {
138
137
  }
139
138
  logger.parentSinks = cfg.parentSinks ?? "inherit";
140
139
  if (cfg.lowestLevel !== void 0) logger.lowestLevel = cfg.lowestLevel;
141
- if (cfg.level !== void 0) {
142
- levelUsed = true;
143
- logger.filters.push(require_filter.toFilter(cfg.level));
144
- }
145
140
  for (const filterId of cfg.filters ?? []) {
146
141
  const filter = config.filters?.[filterId];
147
142
  if (filter === void 0) throw new ConfigError(`Filter not found: ${filterId}.`);
@@ -169,7 +164,6 @@ function configureInternal(config, allowAsync) {
169
164
  metaLoggerCategory: ["logtape", "meta"],
170
165
  dismissLevel: "info"
171
166
  });
172
- if (levelUsed) meta.warn("The level option is deprecated in favor of lowestLevel option. Please update your configuration. See also <https://logtape.org/manual/levels#configuring-severity-levels>.");
173
167
  }
174
168
  /**
175
169
  * Get the current configuration, if any. Otherwise, `null`.
package/dist/config.d.cts CHANGED
@@ -60,13 +60,6 @@ interface LoggerConfig<TSinkId extends string, TFilterId extends string> {
60
60
  * The filter identifiers to use.
61
61
  */
62
62
  filters?: TFilterId[];
63
- /**
64
- * The log level to filter by. If `null`, the logger will reject all
65
- * records.
66
- * @deprecated Use `filters` instead for backward compatibility, or use
67
- * `lowestLevel` for less-misleading behavior.
68
- */
69
- level?: LogLevel | null;
70
63
  /**
71
64
  * The lowest log level to accept. If `null`, the logger will reject all
72
65
  * records.
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.cts","names":[],"sources":["../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;;;;EA4CL,mBAAA,CAAA,EAvDA,mBAuDA,CAvDoB,MAuDpB,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;EA6EF;;;EAGE,KAAE,CAAA,EAAA,OAAA;;;AAAoB;AAgD9C;AAA6B,UA5KZ,YA4KY,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;AACb;EA6IA,QAAA,EAAA,MAAS,GAAA,MAAI,EAAA;EAOP;AAUtB;AAgBA;EAegB,KAAA,CAAA,EA7VN,OA6ViB,EAAA;EAQd;;;;;;;;;;;;;YArVD;;;;;;;UAQF;;;;;;gBAOM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6EM,oEAGZ,OAAO,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgDvB,wEACN,OAAO,SAAS;;;;;iBA6IV,SAAA,CAAA,GAAa;;;;iBAOP,KAAA,CAAA,GAAS;;;;;;iBAUf,SAAA,CAAA;;;;iBAg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ends string, TFilterId extends string> {
60
60
  * The filter identifiers to use.
61
61
  */
62
62
  filters?: TFilterId[];
63
- /**
64
- * The log level to filter by. If `null`, the logger will reject all
65
- * records.
66
- * @deprecated Use `filters` instead for backward compatibility, or use
67
- * `lowestLevel` for less-misleading behavior.
68
- */
69
- level?: LogLevel | null;
70
63
  /**
71
64
  * The lowest log level to accept. If `null`, the logger will reject all
72
65
  * records.
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","names":[],"sources":["../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;;;;EA4CL,mBAAA,CAAA,EAvDA,mBAuDA,CAvDoB,MAuDpB,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;EA6EF;;;EAGE,KAAE,CAAA,EAAA,OAAA;;;AAAoB;AAgD9C;AAA6B,UA5KZ,YA4KY,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;AACb;EA6IA,QAAA,EAAA,MAAS,GAAA,MAAI,EAAA;EAOP;AAUtB;AAgBA;EAegB,KAAA,CAAA,EA7VN,OA6ViB,EAAA;EAQd;;;;;;;;;;;;;YArVD;;;;;;;UAQF;;;;;;gBAOM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6EM,oEAGZ,OAAO,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgDvB,wEACN,OAAO,SAAS;;;;;iBA6IV,SAAA,CAAA,GAAa;;;;iB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dist/config.js CHANGED
@@ -123,7 +123,6 @@ function configureSync(config) {
123
123
  function configureInternal(config, allowAsync) {
124
124
  currentConfig = config;
125
125
  let metaConfigured = false;
126
- let levelUsed = false;
127
126
  const configuredCategories = /* @__PURE__ */ new Set();
128
127
  for (const cfg of config.loggers) {
129
128
  if (isLoggerConfigMeta(cfg)) metaConfigured = true;
@@ -138,10 +137,6 @@ function configureInternal(config, allowAsync) {
138
137
  }
139
138
  logger.parentSinks = cfg.parentSinks ?? "inherit";
140
139
  if (cfg.lowestLevel !== void 0) logger.lowestLevel = cfg.lowestLevel;
141
- if (cfg.level !== void 0) {
142
- levelUsed = true;
143
- logger.filters.push(toFilter(cfg.level));
144
- }
145
140
  for (const filterId of cfg.filters ?? []) {
146
141
  const filter = config.filters?.[filterId];
147
142
  if (filter === void 0) throw new ConfigError(`Filter not found: ${filterId}.`);
@@ -169,7 +164,6 @@ function configureInternal(config, allowAsync) {
169
164
  metaLoggerCategory: ["logtape", "meta"],
170
165
  dismissLevel: "info"
171
166
  });
172
- if (levelUsed) meta.warn("The level option is deprecated in favor of lowestLevel option. Please update your configuration. See also <https://logtape.org/manual/levels#configuring-severity-levels>.");
173
167
  }
174
168
  /**
175
169
  * Get the current configuration, if any. Otherwise, `null`.
@@ -1 +1 @@
1
- {"version":3,"file":"config.js","names":["currentConfig: Config<string, string> | null","strongRefs: Set<LoggerImpl>","disposables: Set<Disposable>","asyncDisposables: Set<AsyncDisposable>","cfg: LoggerConfig<TSinkId, TFilterId>","config: Config<TSinkId, TFilterId>","allowAsync: boolean","promises: PromiseLike<void>[]","message: string"],"sources":["../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 log level to filter by. If `null`, the logger will reject all\n * records.\n * @deprecated Use `filters` instead for backward compatibility, or use\n * `lowestLevel` for less-misleading behavior.\n */\n level?: LogLevel | null;\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 * Disposables to dispose when resetting the configuration.\n */\nconst disposables: Set<Disposable> = new Set();\n\n/**\n * Async disposables to dispose when resetting the configuration.\n */\nconst asyncDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Check if a config is for the meta logger.\n */\nfunction isLoggerConfigMeta<TSinkId extends string, TFilterId extends string>(\n cfg: LoggerConfig<TSinkId, TFilterId>,\n): boolean {\n return cfg.category.length === 0 ||\n (cfg.category.length === 1 && cfg.category[0] === \"logtape\") ||\n (cfg.category.length === 2 &&\n cfg.category[0] === \"logtape\" &&\n cfg.category[1] === \"meta\");\n}\n\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 (asyncDisposables.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 let levelUsed = 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 if (cfg.level !== undefined) {\n levelUsed = true;\n logger.filters.push(toFilter(cfg.level));\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) asyncDisposables.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) disposables.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) asyncDisposables.add(filter as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in filter) disposables.add(filter as Disposable);\n }\n\n if (\"process\" in globalThis && !(\"Deno\" in globalThis)) {\n // @ts-ignore: It's fine to use process in Node\n // deno-lint-ignore no-process-global\n process.on(\"exit\", allowAsync ? dispose : disposeSync);\n } else {\n // @ts-ignore: It's fine to addEventListener() on the browser/Deno\n addEventListener(\"unload\", allowAsync ? dispose : disposeSync);\n }\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 \" +\n \"purposes to log internal errors such as sink exceptions. If you \" +\n \"are seeing this message, the meta logger is automatically configured. \" +\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 if (levelUsed) {\n meta.warn(\n \"The level option is deprecated in favor of lowestLevel option. \" +\n \"Please update your configuration. See also \" +\n \"<https://logtape.org/manual/levels#configuring-severity-levels>.\",\n );\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 disposeSync();\n const promises: PromiseLike<void>[] = [];\n for (const disposable of asyncDisposables) {\n promises.push(disposable[Symbol.asyncDispose]());\n asyncDisposables.delete(disposable);\n }\n await Promise.all(promises);\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 for (const disposable of disposables) disposable[Symbol.dispose]();\n disposables.clear();\n}\n\n/**\n * A configuration error.\n */\nexport class ConfigError extends Error {\n /**\n * Constructs a new configuration error.\n * @param message The error message.\n */\n constructor(message: string) {\n super(message);\n this.name = \"ConfigureError\";\n }\n}\n"],"mappings":";;;;;;;;AA2FA,IAAIA,gBAA+C;;;;;;AAOnD,MAAMC,6BAA8B,IAAI;;;;AAKxC,MAAMC,8BAA+B,IAAI;;;;AAKzC,MAAMC,mCAAyC,IAAI;;;;AAKnD,SAAS,mBACPC,KACS;AACT,QAAO,IAAI,SAAS,WAAW,KAC5B,IAAI,SAAS,WAAW,KAAK,IAAI,SAAS,OAAO,aACjD,IAAI,SAAS,WAAW,KACvB,IAAI,SAAS,OAAO,aACpB,IAAI,SAAS,OAAO;AACz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SAAS;AACf,gBAAe;AAChB;;;;;;AAOD,SAAgB,YAAkB;AAChC,cAAa;AACb,gBAAe;AAChB;AAED,SAAS,gBAAsB;CAC7B,MAAM,aAAa,WAAW,UAAU,CAAE,EAAC;AAC3C,YAAW,kBAAkB;AAC7B,QAAO,WAAW;AAClB,YAAW,OAAO;AAClB,iBAAgB;AACjB;;;;AAKD,eAAsB,UAAyB;AAC7C,cAAa;CACb,MAAMC,WAAgC,CAAE;AACxC,MAAK,MAAM,cAAc,kBAAkB;AACzC,WAAS,KAAK,WAAW,OAAO,eAAe,CAAC;AAChD,mBAAiB,OAAO,WAAW;CACpC;AACD,OAAM,QAAQ,IAAI,SAAS;AAC5B;;;;;;AAOD,SAAgB,cAAoB;AAClC,MAAK,MAAM,cAAc,YAAa,YAAW,OAAO,UAAU;AAClE,aAAY,OAAO;AACpB;;;;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>","disposables: Set<Disposable>","asyncDisposables: Set<AsyncDisposable>","cfg: LoggerConfig<TSinkId, TFilterId>","config: Config<TSinkId, TFilterId>","allowAsync: boolean","promises: PromiseLike<void>[]","message: string"],"sources":["../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 * Disposables to dispose when resetting the configuration.\n */\nconst disposables: Set<Disposable> = new Set();\n\n/**\n * Async disposables to dispose when resetting the configuration.\n */\nconst asyncDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Check if a config is for the meta logger.\n */\nfunction isLoggerConfigMeta<TSinkId extends string, TFilterId extends string>(\n cfg: LoggerConfig<TSinkId, TFilterId>,\n): boolean {\n return cfg.category.length === 0 ||\n (cfg.category.length === 1 && cfg.category[0] === \"logtape\") ||\n (cfg.category.length === 2 &&\n cfg.category[0] === \"logtape\" &&\n cfg.category[1] === \"meta\");\n}\n\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 (asyncDisposables.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) asyncDisposables.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) disposables.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) asyncDisposables.add(filter as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in filter) disposables.add(filter as Disposable);\n }\n\n if (\"process\" in globalThis && !(\"Deno\" in globalThis)) {\n // @ts-ignore: It's fine to use process in Node\n // deno-lint-ignore no-process-global\n process.on(\"exit\", allowAsync ? dispose : disposeSync);\n } else {\n // @ts-ignore: It's fine to addEventListener() on the browser/Deno\n addEventListener(\"unload\", allowAsync ? dispose : disposeSync);\n }\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 \" +\n \"purposes to log internal errors such as sink exceptions. If you \" +\n \"are seeing this message, the meta logger is automatically configured. \" +\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 disposeSync();\n const promises: PromiseLike<void>[] = [];\n for (const disposable of asyncDisposables) {\n promises.push(disposable[Symbol.asyncDispose]());\n asyncDisposables.delete(disposable);\n }\n await Promise.all(promises);\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 for (const disposable of disposables) disposable[Symbol.dispose]();\n disposables.clear();\n}\n\n/**\n * A configuration error.\n */\nexport class ConfigError extends Error {\n /**\n * Constructs a new configuration error.\n * @param message The error message.\n */\n constructor(message: string) {\n super(message);\n this.name = \"ConfigureError\";\n }\n}\n"],"mappings":";;;;;;;;AAmFA,IAAIA,gBAA+C;;;;;;AAOnD,MAAMC,6BAA8B,IAAI;;;;AAKxC,MAAMC,8BAA+B,IAAI;;;;AAKzC,MAAMC,mCAAyC,IAAI;;;;AAKnD,SAAS,mBACPC,KACS;AACT,QAAO,IAAI,SAAS,WAAW,KAC5B,IAAI,SAAS,WAAW,KAAK,IAAI,SAAS,OAAO,aACjD,IAAI,SAAS,WAAW,KACvB,IAAI,SAAS,OAAO,aACpB,IAAI,SAAS,OAAO;AACzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCD,eAAsB,UAGpBC,QAAmD;AACnD,KAAI,iBAAiB,SAAS,OAAO,MACnC,OAAM,IAAI,YACR;AAGJ,OAAM,OAAO;AACb,KAAI;AACF,oBAAkB,QAAQ,KAAK;CAChC,SAAQ,GAAG;AACV,MAAI,aAAa,YAAa,OAAM,OAAO;AAC3C,QAAM;CACP;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCD,SAAgB,cACdA,QACM;AACN,KAAI,iBAAiB,SAAS,OAAO,MACnC,OAAM,IAAI,YACR;AAGJ,KAAI,iBAAiB,OAAO,EAC1B,OAAM,IAAI,YACR;AAIJ,YAAW;AACX,KAAI;AACF,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AAK,OAAO;CACb;AACF"}
package/dist/mod.cjs CHANGED
@@ -30,5 +30,6 @@ exports.parseLogLevel = require_level.parseLogLevel;
30
30
  exports.reset = require_config.reset;
31
31
  exports.resetSync = require_config.resetSync;
32
32
  exports.toFilter = require_filter.toFilter;
33
+ exports.withBuffer = require_sink.withBuffer;
33
34
  exports.withContext = require_context.withContext;
34
35
  exports.withFilter = require_sink.withFilter;
package/dist/mod.d.cts CHANGED
@@ -3,7 +3,7 @@ import { LogLevel, compareLogLevel, getLogLevels, isLogLevel, parseLogLevel } fr
3
3
  import { LogRecord } from "./record.cjs";
4
4
  import { Filter, FilterLike, getLevelFilter, toFilter } from "./filter.cjs";
5
5
  import { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, ConsoleFormatter, FormattedValues, JsonLinesFormatterOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, defaultConsoleFormatter, defaultTextFormatter, getAnsiColorFormatter, getJsonLinesFormatter, getTextFormatter, jsonLinesFormatter } from "./formatter.cjs";
6
- import { ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withFilter } from "./sink.cjs";
6
+ import { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withBuffer, withFilter } from "./sink.cjs";
7
7
  import { Config, ConfigError, LoggerConfig, configure, configureSync, dispose, disposeSync, getConfig, reset, resetSync } from "./config.cjs";
8
8
  import { LogMethod, Logger, getLogger } from "./logger.cjs";
9
- export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withContext, withFilter };
9
+ export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, BufferSinkOptions, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withBuffer, withContext, withFilter };
package/dist/mod.d.ts CHANGED
@@ -3,7 +3,7 @@ import { LogLevel, compareLogLevel, getLogLevels, isLogLevel, parseLogLevel } fr
3
3
  import { LogRecord } from "./record.js";
4
4
  import { Filter, FilterLike, getLevelFilter, toFilter } from "./filter.js";
5
5
  import { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, ConsoleFormatter, FormattedValues, JsonLinesFormatterOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, defaultConsoleFormatter, defaultTextFormatter, getAnsiColorFormatter, getJsonLinesFormatter, getTextFormatter, jsonLinesFormatter } from "./formatter.js";
6
- import { ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withFilter } from "./sink.js";
6
+ import { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withBuffer, withFilter } from "./sink.js";
7
7
  import { Config, ConfigError, LoggerConfig, configure, configureSync, dispose, disposeSync, getConfig, reset, resetSync } from "./config.js";
8
8
  import { LogMethod, Logger, getLogger } from "./logger.js";
9
- export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withContext, withFilter };
9
+ export { AnsiColor, AnsiColorFormatterOptions, AnsiStyle, BufferSinkOptions, Config, ConfigError, ConsoleFormatter, ConsoleSinkOptions, ContextLocalStorage, Filter, FilterLike, FormattedValues, JsonLinesFormatterOptions, LogLevel, LogMethod, LogRecord, Logger, LoggerConfig, Sink, StreamSinkOptions, TextFormatter, TextFormatterOptions, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withBuffer, withContext, withFilter };
package/dist/mod.js CHANGED
@@ -2,8 +2,8 @@ import { getLevelFilter, toFilter } from "./filter.js";
2
2
  import { compareLogLevel, getLogLevels, isLogLevel, parseLogLevel } from "./level.js";
3
3
  import { getLogger } from "./logger.js";
4
4
  import { ansiColorFormatter, defaultConsoleFormatter, defaultTextFormatter, getAnsiColorFormatter, getJsonLinesFormatter, getTextFormatter, jsonLinesFormatter } from "./formatter.js";
5
- import { getConsoleSink, getStreamSink, withFilter } from "./sink.js";
5
+ import { getConsoleSink, getStreamSink, withBuffer, withFilter } from "./sink.js";
6
6
  import { ConfigError, configure, configureSync, dispose, disposeSync, getConfig, reset, resetSync } from "./config.js";
7
7
  import { withContext } from "./context.js";
8
8
 
9
- export { ConfigError, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withContext, withFilter };
9
+ export { ConfigError, ansiColorFormatter, compareLogLevel, configure, configureSync, defaultConsoleFormatter, defaultTextFormatter, dispose, disposeSync, getAnsiColorFormatter, getConfig, getConsoleSink, getJsonLinesFormatter, getLevelFilter, getLogLevels, getLogger, getStreamSink, getTextFormatter, isLogLevel, jsonLinesFormatter, parseLogLevel, reset, resetSync, toFilter, withBuffer, withContext, withFilter };
package/dist/sink.cjs CHANGED
@@ -23,6 +23,68 @@ function withFilter(sink, filter) {
23
23
  };
24
24
  }
25
25
  /**
26
+ * Turns a sink into a buffered sink. The returned sink buffers log records
27
+ * in memory and flushes them to the underlying sink when the buffer is full
28
+ * or after a specified time interval.
29
+ *
30
+ * @example Buffer a console sink with custom options
31
+ * ```typescript
32
+ * const sink = withBuffer(getConsoleSink(), {
33
+ * bufferSize: 5,
34
+ * flushInterval: 1000
35
+ * });
36
+ * ```
37
+ *
38
+ * @param sink A sink to be buffered.
39
+ * @param options Options for the buffered sink.
40
+ * @returns A buffered sink that flushes records periodically.
41
+ * @since 1.0.0
42
+ */
43
+ function withBuffer(sink, options = {}) {
44
+ const bufferSize = options.bufferSize ?? 10;
45
+ const flushInterval = options.flushInterval ?? 5e3;
46
+ const buffer = [];
47
+ let flushTimer = null;
48
+ let disposed = false;
49
+ function flush() {
50
+ if (buffer.length === 0) return;
51
+ const records = buffer.splice(0);
52
+ for (const record of records) try {
53
+ sink(record);
54
+ } catch (error) {
55
+ throw error;
56
+ }
57
+ if (flushTimer !== null) {
58
+ clearTimeout(flushTimer);
59
+ flushTimer = null;
60
+ }
61
+ }
62
+ function scheduleFlush() {
63
+ if (flushInterval <= 0 || flushTimer !== null || disposed) return;
64
+ flushTimer = setTimeout(() => {
65
+ flushTimer = null;
66
+ flush();
67
+ }, flushInterval);
68
+ }
69
+ const bufferedSink = (record) => {
70
+ if (disposed) return;
71
+ buffer.push(record);
72
+ if (buffer.length >= bufferSize) flush();
73
+ else scheduleFlush();
74
+ };
75
+ bufferedSink[Symbol.asyncDispose] = async () => {
76
+ disposed = true;
77
+ if (flushTimer !== null) {
78
+ clearTimeout(flushTimer);
79
+ flushTimer = null;
80
+ }
81
+ flush();
82
+ if (Symbol.asyncDispose in sink) await sink[Symbol.asyncDispose]();
83
+ else if (Symbol.dispose in sink) sink[Symbol.dispose]();
84
+ };
85
+ return bufferedSink;
86
+ }
87
+ /**
26
88
  * A factory that returns a sink that writes to a {@link WritableStream}.
27
89
  *
28
90
  * Note that the `stream` is of Web Streams API, which is different from
@@ -93,4 +155,5 @@ function getConsoleSink(options = {}) {
93
155
  //#endregion
94
156
  exports.getConsoleSink = getConsoleSink;
95
157
  exports.getStreamSink = getStreamSink;
158
+ exports.withBuffer = withBuffer;
96
159
  exports.withFilter = withFilter;
package/dist/sink.d.cts CHANGED
@@ -30,6 +30,44 @@ type Sink = (record: LogRecord) => void;
30
30
  * @returns A sink that only logs records that pass the filter.
31
31
  */
32
32
  declare function withFilter(sink: Sink, filter: FilterLike): Sink;
33
+ /**
34
+ * Options for the {@link withBuffer} function.
35
+ * @since 1.0.0
36
+ */
37
+ interface BufferSinkOptions {
38
+ /**
39
+ * The maximum number of log records to buffer before flushing to the
40
+ * underlying sink.
41
+ * @default `10`
42
+ */
43
+ bufferSize?: number;
44
+ /**
45
+ * The maximum time in milliseconds to wait before flushing buffered records
46
+ * to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
47
+ * negative to disable time-based flushing.
48
+ * @default `5000`
49
+ */
50
+ flushInterval?: number;
51
+ }
52
+ /**
53
+ * Turns a sink into a buffered sink. The returned sink buffers log records
54
+ * in memory and flushes them to the underlying sink when the buffer is full
55
+ * or after a specified time interval.
56
+ *
57
+ * @example Buffer a console sink with custom options
58
+ * ```typescript
59
+ * const sink = withBuffer(getConsoleSink(), {
60
+ * bufferSize: 5,
61
+ * flushInterval: 1000
62
+ * });
63
+ * ```
64
+ *
65
+ * @param sink A sink to be buffered.
66
+ * @param options Options for the buffered sink.
67
+ * @returns A buffered sink that flushes records periodically.
68
+ * @since 1.0.0
69
+ */
70
+ declare function withBuffer(sink: Sink, options?: BufferSinkOptions): Sink & AsyncDisposable;
33
71
  /**
34
72
  * Options for the {@link getStreamSink} function.
35
73
  */
@@ -109,5 +147,5 @@ interface ConsoleSinkOptions {
109
147
  */
110
148
  declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
111
149
  //#endregion
112
- export { ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withFilter };
150
+ export { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withBuffer, withFilter };
113
151
  //# sourceMappingURL=sink.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sink.d.cts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAgBA;;;;;AAAgE;AAU/C,KA1BL,IAAA,GA0BK,CAAA,MAAiB,EA1BN,SA0BM,EAAA,GAAA,IAAA;;;;AASY;AA2B9C;;;;;;AAGyB;AAgBxB;AAOD;;AAKc,iBA7EE,UAAA,CA6EF,IAAA,EA7EmB,IA6EnB,EAAA,MAAA,EA7EiC,UA6EjC,CAAA,EA7E8C,IA6E9C;;;;AAiBD,UApFI,iBAAA,CAoFJ;EAAM;AAKA;AASnB;EAA8B,SAAA,CAAA,EA9FhB,aA8FgB;EAAA;;AAAwC;;0BAzFlC;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2BpB,aAAA,SACN,0BACC,oBACR,OAAO;KAkBL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;iBASI,cAAA,WAAwB,qBAA0B"}
1
+ {"version":3,"file":"sink.d.cts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAgBA;;;;;AAAgE;AAW/C,KA3BL,IAAA,GA2BK,CAAA,MAAiB,EA3BN,SA2BM,EAAA,GAAA,IAAA;AAmClC;;;;;;AAGyB;AAsEzB;;;;AAS8C;AA2B9C;;AACU,iBA5JM,UAAA,CA4JN,IAAA,EA5JuB,IA4JvB,EAAA,MAAA,EA5JqC,UA4JrC,CAAA,EA5JkD,IA4JlD;;;;AAEe;AAkBpB,UArKY,iBAAA,CAqKC;EAKD;;;;;EAsBW,UAAE,CAAA,EAAA,MAAA;EAAa;;AAKxB;AASnB;;;EAA+D,aAAG,CAAA,EAAA,MAAA;AAAI;;;;;;;;;;;;;;;;;;;iBA3KtD,UAAA,OACR,gBACG,oBACR,OAAO;;;;UAsEO,iBAAA;;;;cAIH;;;;;0BAKsB;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2BpB,aAAA,SACN,0BACC,oBACR,OAAO;KAkBL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;iBASI,cAAA,WAAwB,qBAA0B"}
package/dist/sink.d.ts CHANGED
@@ -30,6 +30,44 @@ type Sink = (record: LogRecord) => void;
30
30
  * @returns A sink that only logs records that pass the filter.
31
31
  */
32
32
  declare function withFilter(sink: Sink, filter: FilterLike): Sink;
33
+ /**
34
+ * Options for the {@link withBuffer} function.
35
+ * @since 1.0.0
36
+ */
37
+ interface BufferSinkOptions {
38
+ /**
39
+ * The maximum number of log records to buffer before flushing to the
40
+ * underlying sink.
41
+ * @default `10`
42
+ */
43
+ bufferSize?: number;
44
+ /**
45
+ * The maximum time in milliseconds to wait before flushing buffered records
46
+ * to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
47
+ * negative to disable time-based flushing.
48
+ * @default `5000`
49
+ */
50
+ flushInterval?: number;
51
+ }
52
+ /**
53
+ * Turns a sink into a buffered sink. The returned sink buffers log records
54
+ * in memory and flushes them to the underlying sink when the buffer is full
55
+ * or after a specified time interval.
56
+ *
57
+ * @example Buffer a console sink with custom options
58
+ * ```typescript
59
+ * const sink = withBuffer(getConsoleSink(), {
60
+ * bufferSize: 5,
61
+ * flushInterval: 1000
62
+ * });
63
+ * ```
64
+ *
65
+ * @param sink A sink to be buffered.
66
+ * @param options Options for the buffered sink.
67
+ * @returns A buffered sink that flushes records periodically.
68
+ * @since 1.0.0
69
+ */
70
+ declare function withBuffer(sink: Sink, options?: BufferSinkOptions): Sink & AsyncDisposable;
33
71
  /**
34
72
  * Options for the {@link getStreamSink} function.
35
73
  */
@@ -109,5 +147,5 @@ interface ConsoleSinkOptions {
109
147
  */
110
148
  declare function getConsoleSink(options?: ConsoleSinkOptions): Sink;
111
149
  //#endregion
112
- export { ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withFilter };
150
+ export { BufferSinkOptions, ConsoleSinkOptions, Sink, StreamSinkOptions, getConsoleSink, getStreamSink, withBuffer, withFilter };
113
151
  //# sourceMappingURL=sink.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sink.d.ts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAgBA;;;;;AAAgE;AAU/C,KA1BL,IAAA,GA0BK,CAAA,MAAiB,EA1BN,SA0BM,EAAA,GAAA,IAAA;;;;AASY;AA2B9C;;;;;;AAGyB;AAgBxB;AAOD;;AAKc,iBA7EE,UAAA,CA6EF,IAAA,EA7EmB,IA6EnB,EAAA,MAAA,EA7EiC,UA6EjC,CAAA,EA7E8C,IA6E9C;;;;AAiBD,UApFI,iBAAA,CAoFJ;EAAM;AAKA;AASnB;EAA8B,SAAA,CAAA,EA9FhB,aA8FgB;EAAA;;AAAwC;;0BAzFlC;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2BpB,aAAA,SACN,0BACC,oBACR,OAAO;KAkBL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;iBASI,cAAA,WAAwB,qBAA0B"}
1
+ {"version":3,"file":"sink.d.ts","names":[],"sources":["../sink.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAmBA;AAgBA;;;;;AAAgE;AAW/C,KA3BL,IAAA,GA2BK,CAAA,MAAiB,EA3BN,SA2BM,EAAA,GAAA,IAAA;AAmClC;;;;;;AAGyB;AAsEzB;;;;AAS8C;AA2B9C;;AACU,iBA5JM,UAAA,CA4JN,IAAA,EA5JuB,IA4JvB,EAAA,MAAA,EA5JqC,UA4JrC,CAAA,EA5JkD,IA4JlD;;;;AAEe;AAkBpB,UArKY,iBAAA,CAqKC;EAKD;;;;;EAsBW,UAAE,CAAA,EAAA,MAAA;EAAa;;AAKxB;AASnB;;;EAA+D,aAAG,CAAA,EAAA,MAAA;AAAI;;;;;;;;;;;;;;;;;;;iBA3KtD,UAAA,OACR,gBACG,oBACR,OAAO;;;;UAsEO,iBAAA;;;;cAIH;;;;;0BAKsB;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2BpB,aAAA,SACN,0BACC,oBACR,OAAO;KAkBL,aAAA;;;;UAKY,kBAAA;;;;;cAKH,mBAAmB;;;;;;;;;;;;;;;;aAiBpB,OAAO,UAAU;;;;YAKlB;;;;;;;;iBASI,cAAA,WAAwB,qBAA0B"}
package/dist/sink.js CHANGED
@@ -23,6 +23,68 @@ function withFilter(sink, filter) {
23
23
  };
24
24
  }
25
25
  /**
26
+ * Turns a sink into a buffered sink. The returned sink buffers log records
27
+ * in memory and flushes them to the underlying sink when the buffer is full
28
+ * or after a specified time interval.
29
+ *
30
+ * @example Buffer a console sink with custom options
31
+ * ```typescript
32
+ * const sink = withBuffer(getConsoleSink(), {
33
+ * bufferSize: 5,
34
+ * flushInterval: 1000
35
+ * });
36
+ * ```
37
+ *
38
+ * @param sink A sink to be buffered.
39
+ * @param options Options for the buffered sink.
40
+ * @returns A buffered sink that flushes records periodically.
41
+ * @since 1.0.0
42
+ */
43
+ function withBuffer(sink, options = {}) {
44
+ const bufferSize = options.bufferSize ?? 10;
45
+ const flushInterval = options.flushInterval ?? 5e3;
46
+ const buffer = [];
47
+ let flushTimer = null;
48
+ let disposed = false;
49
+ function flush() {
50
+ if (buffer.length === 0) return;
51
+ const records = buffer.splice(0);
52
+ for (const record of records) try {
53
+ sink(record);
54
+ } catch (error) {
55
+ throw error;
56
+ }
57
+ if (flushTimer !== null) {
58
+ clearTimeout(flushTimer);
59
+ flushTimer = null;
60
+ }
61
+ }
62
+ function scheduleFlush() {
63
+ if (flushInterval <= 0 || flushTimer !== null || disposed) return;
64
+ flushTimer = setTimeout(() => {
65
+ flushTimer = null;
66
+ flush();
67
+ }, flushInterval);
68
+ }
69
+ const bufferedSink = (record) => {
70
+ if (disposed) return;
71
+ buffer.push(record);
72
+ if (buffer.length >= bufferSize) flush();
73
+ else scheduleFlush();
74
+ };
75
+ bufferedSink[Symbol.asyncDispose] = async () => {
76
+ disposed = true;
77
+ if (flushTimer !== null) {
78
+ clearTimeout(flushTimer);
79
+ flushTimer = null;
80
+ }
81
+ flush();
82
+ if (Symbol.asyncDispose in sink) await sink[Symbol.asyncDispose]();
83
+ else if (Symbol.dispose in sink) sink[Symbol.dispose]();
84
+ };
85
+ return bufferedSink;
86
+ }
87
+ /**
26
88
  * A factory that returns a sink that writes to a {@link WritableStream}.
27
89
  *
28
90
  * Note that the `stream` is of Web Streams API, which is different from
@@ -91,5 +153,5 @@ function getConsoleSink(options = {}) {
91
153
  }
92
154
 
93
155
  //#endregion
94
- export { getConsoleSink, getStreamSink, withFilter };
156
+ export { getConsoleSink, getStreamSink, withBuffer, withFilter };
95
157
  //# sourceMappingURL=sink.js.map
package/dist/sink.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"sink.js","names":["sink: Sink","filter: FilterLike","record: LogRecord","stream: WritableStream","options: StreamSinkOptions","sink: Sink & AsyncDisposable","options: ConsoleSinkOptions","levelMap: Record<LogLevel, ConsoleMethod>"],"sources":["../sink.ts"],"sourcesContent":["import { type FilterLike, toFilter } from \"./filter.ts\";\nimport {\n type ConsoleFormatter,\n defaultConsoleFormatter,\n defaultTextFormatter,\n type TextFormatter,\n} from \"./formatter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport type { LogRecord } from \"./record.ts\";\n\n/**\n * A sink is a function that accepts a log record and prints it somewhere.\n * Thrown exceptions will be suppressed and then logged to the meta logger,\n * a {@link Logger} with the category `[\"logtape\", \"meta\"]`. (In that case,\n * the meta log record will not be passed to the sink to avoid infinite\n * recursion.)\n *\n * @param record The log record to sink.\n */\nexport type Sink = (record: LogRecord) => void;\n\n/**\n * Turns a sink into a filtered sink. The returned sink only logs records that\n * pass the filter.\n *\n * @example Filter a console sink to only log records with the info level\n * ```typescript\n * const sink = withFilter(getConsoleSink(), \"info\");\n * ```\n *\n * @param sink A sink to be filtered.\n * @param filter A filter to apply to the sink. It can be either a filter\n * function or a {@link LogLevel} string.\n * @returns A sink that only logs records that pass the filter.\n */\nexport function withFilter(sink: Sink, filter: FilterLike): Sink {\n const filterFunc = toFilter(filter);\n return (record: LogRecord) => {\n if (filterFunc(record)) sink(record);\n };\n}\n\n/**\n * Options for the {@link getStreamSink} function.\n */\nexport interface StreamSinkOptions {\n /**\n * The text formatter to use. Defaults to {@link defaultTextFormatter}.\n */\n formatter?: TextFormatter;\n\n /**\n * The text encoder to use. Defaults to an instance of {@link TextEncoder}.\n */\n encoder?: { encode(text: string): Uint8Array };\n}\n\n/**\n * A factory that returns a sink that writes to a {@link WritableStream}.\n *\n * Note that the `stream` is of Web Streams API, which is different from\n * Node.js streams. You can convert a Node.js stream to a Web Streams API\n * stream using [`stream.Writable.toWeb()`] method.\n *\n * [`stream.Writable.toWeb()`]: https://nodejs.org/api/stream.html#streamwritabletowebstreamwritable\n *\n * @example Sink to the standard error in Deno\n * ```typescript\n * const stderrSink = getStreamSink(Deno.stderr.writable);\n * ```\n *\n * @example Sink to the standard error in Node.js\n * ```typescript\n * import stream from \"node:stream\";\n * const stderrSink = getStreamSink(stream.Writable.toWeb(process.stderr));\n * ```\n *\n * @param stream The stream to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the stream.\n */\nexport function getStreamSink(\n stream: WritableStream,\n options: StreamSinkOptions = {},\n): Sink & AsyncDisposable {\n const formatter = options.formatter ?? defaultTextFormatter;\n const encoder = options.encoder ?? new TextEncoder();\n const writer = stream.getWriter();\n let lastPromise = Promise.resolve();\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n const bytes = encoder.encode(formatter(record));\n lastPromise = lastPromise\n .then(() => writer.ready)\n .then(() => writer.write(bytes));\n };\n sink[Symbol.asyncDispose] = async () => {\n await lastPromise;\n await writer.close();\n };\n return sink;\n}\n\ntype ConsoleMethod = \"debug\" | \"info\" | \"log\" | \"warn\" | \"error\";\n\n/**\n * Options for the {@link getConsoleSink} function.\n */\nexport interface ConsoleSinkOptions {\n /**\n * The console formatter or text formatter to use.\n * Defaults to {@link defaultConsoleFormatter}.\n */\n formatter?: ConsoleFormatter | TextFormatter;\n\n /**\n * The mapping from log levels to console methods. Defaults to:\n *\n * ```typescript\n * {\n * trace: \"trace\",\n * debug: \"debug\",\n * info: \"info\",\n * warning: \"warn\",\n * error: \"error\",\n * fatal: \"error\",\n * }\n * ```\n * @since 0.9.0\n */\n levelMap?: Record<LogLevel, ConsoleMethod>;\n\n /**\n * The console to log to. Defaults to {@link console}.\n */\n console?: Console;\n}\n\n/**\n * A console sink factory that returns a sink that logs to the console.\n *\n * @param options The options for the sink.\n * @returns A sink that logs to the console.\n */\nexport function getConsoleSink(options: ConsoleSinkOptions = {}): Sink {\n const formatter = options.formatter ?? defaultConsoleFormatter;\n const levelMap: Record<LogLevel, ConsoleMethod> = {\n trace: \"debug\",\n debug: \"debug\",\n info: \"info\",\n warning: \"warn\",\n error: \"error\",\n fatal: \"error\",\n ...(options.levelMap ?? {}),\n };\n const console = options.console ?? globalThis.console;\n return (record: LogRecord) => {\n const args = formatter(record);\n const method = levelMap[record.level];\n if (method === undefined) {\n throw new TypeError(`Invalid log level: ${record.level}.`);\n }\n if (typeof args === \"string\") {\n const msg = args.replace(/\\r?\\n$/, \"\");\n console[method](msg);\n } else {\n console[method](...args);\n }\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmCA,SAAgB,WAAWA,MAAYC,QAA0B;CAC/D,MAAM,aAAa,SAAS,OAAO;AACnC,QAAO,CAACC,WAAsB;AAC5B,MAAI,WAAW,OAAO,CAAE,MAAK,OAAO;CACrC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;AAyCD,SAAgB,cACdC,QACAC,UAA6B,CAAE,GACP;CACxB,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,UAAU,QAAQ,WAAW,IAAI;CACvC,MAAM,SAAS,OAAO,WAAW;CACjC,IAAI,cAAc,QAAQ,SAAS;CACnC,MAAMC,OAA+B,CAACH,WAAsB;EAC1D,MAAM,QAAQ,QAAQ,OAAO,UAAU,OAAO,CAAC;AAC/C,gBAAc,YACX,KAAK,MAAM,OAAO,MAAM,CACxB,KAAK,MAAM,OAAO,MAAM,MAAM,CAAC;CACnC;AACD,MAAK,OAAO,gBAAgB,YAAY;AACtC,QAAM;AACN,QAAM,OAAO,OAAO;CACrB;AACD,QAAO;AACR;;;;;;;AA2CD,SAAgB,eAAeI,UAA8B,CAAE,GAAQ;CACrE,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAMC,WAA4C;EAChD,OAAO;EACP,OAAO;EACP,MAAM;EACN,SAAS;EACT,OAAO;EACP,OAAO;EACP,GAAI,QAAQ,YAAY,CAAE;CAC3B;CACD,MAAM,UAAU,QAAQ,WAAW,WAAW;AAC9C,QAAO,CAACL,WAAsB;EAC5B,MAAM,OAAO,UAAU,OAAO;EAC9B,MAAM,SAAS,SAAS,OAAO;AAC/B,MAAI,kBACF,OAAM,IAAI,WAAW,qBAAqB,OAAO,MAAM;AAEzD,aAAW,SAAS,UAAU;GAC5B,MAAM,MAAM,KAAK,QAAQ,UAAU,GAAG;AACtC,WAAQ,QAAQ,IAAI;EACrB,MACC,SAAQ,QAAQ,GAAG,KAAK;CAE3B;AACF"}
1
+ {"version":3,"file":"sink.js","names":["sink: Sink","filter: FilterLike","record: LogRecord","options: BufferSinkOptions","buffer: LogRecord[]","flushTimer: ReturnType<typeof setTimeout> | null","bufferedSink: Sink & AsyncDisposable","stream: WritableStream","options: StreamSinkOptions","sink: Sink & AsyncDisposable","options: ConsoleSinkOptions","levelMap: Record<LogLevel, ConsoleMethod>"],"sources":["../sink.ts"],"sourcesContent":["import { type FilterLike, toFilter } from \"./filter.ts\";\nimport {\n type ConsoleFormatter,\n defaultConsoleFormatter,\n defaultTextFormatter,\n type TextFormatter,\n} from \"./formatter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport type { LogRecord } from \"./record.ts\";\n\n/**\n * A sink is a function that accepts a log record and prints it somewhere.\n * Thrown exceptions will be suppressed and then logged to the meta logger,\n * a {@link Logger} with the category `[\"logtape\", \"meta\"]`. (In that case,\n * the meta log record will not be passed to the sink to avoid infinite\n * recursion.)\n *\n * @param record The log record to sink.\n */\nexport type Sink = (record: LogRecord) => void;\n\n/**\n * Turns a sink into a filtered sink. The returned sink only logs records that\n * pass the filter.\n *\n * @example Filter a console sink to only log records with the info level\n * ```typescript\n * const sink = withFilter(getConsoleSink(), \"info\");\n * ```\n *\n * @param sink A sink to be filtered.\n * @param filter A filter to apply to the sink. It can be either a filter\n * function or a {@link LogLevel} string.\n * @returns A sink that only logs records that pass the filter.\n */\nexport function withFilter(sink: Sink, filter: FilterLike): Sink {\n const filterFunc = toFilter(filter);\n return (record: LogRecord) => {\n if (filterFunc(record)) sink(record);\n };\n}\n\n/**\n * Options for the {@link withBuffer} function.\n * @since 1.0.0\n */\nexport interface BufferSinkOptions {\n /**\n * The maximum number of log records to buffer before flushing to the\n * underlying sink.\n * @default `10`\n */\n bufferSize?: number;\n\n /**\n * The maximum time in milliseconds to wait before flushing buffered records\n * to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or\n * negative to disable time-based flushing.\n * @default `5000`\n */\n flushInterval?: number;\n}\n\n/**\n * Turns a sink into a buffered sink. The returned sink buffers log records\n * in memory and flushes them to the underlying sink when the buffer is full\n * or after a specified time interval.\n *\n * @example Buffer a console sink with custom options\n * ```typescript\n * const sink = withBuffer(getConsoleSink(), {\n * bufferSize: 5,\n * flushInterval: 1000\n * });\n * ```\n *\n * @param sink A sink to be buffered.\n * @param options Options for the buffered sink.\n * @returns A buffered sink that flushes records periodically.\n * @since 1.0.0\n */\nexport function withBuffer(\n sink: Sink,\n options: BufferSinkOptions = {},\n): Sink & AsyncDisposable {\n const bufferSize = options.bufferSize ?? 10;\n const flushInterval = options.flushInterval ?? 5000;\n\n const buffer: LogRecord[] = [];\n let flushTimer: ReturnType<typeof setTimeout> | null = null;\n let disposed = false;\n\n function flush(): void {\n if (buffer.length === 0) return;\n\n const records = buffer.splice(0);\n for (const record of records) {\n try {\n sink(record);\n } catch (error) {\n // Errors are handled by the sink infrastructure\n throw error;\n }\n }\n\n if (flushTimer !== null) {\n clearTimeout(flushTimer);\n flushTimer = null;\n }\n }\n\n function scheduleFlush(): void {\n if (flushInterval <= 0 || flushTimer !== null || disposed) return;\n\n flushTimer = setTimeout(() => {\n flushTimer = null;\n flush();\n }, flushInterval);\n }\n\n const bufferedSink: Sink & AsyncDisposable = (record: LogRecord) => {\n if (disposed) return;\n\n buffer.push(record);\n\n if (buffer.length >= bufferSize) {\n flush();\n } else {\n scheduleFlush();\n }\n };\n\n bufferedSink[Symbol.asyncDispose] = async () => {\n disposed = true;\n if (flushTimer !== null) {\n clearTimeout(flushTimer);\n flushTimer = null;\n }\n flush();\n\n // Dispose the underlying sink if it's disposable\n if (Symbol.asyncDispose in sink) {\n await (sink as AsyncDisposable)[Symbol.asyncDispose]();\n } else if (Symbol.dispose in sink) {\n (sink as Disposable)[Symbol.dispose]();\n }\n };\n\n return bufferedSink;\n}\n\n/**\n * Options for the {@link getStreamSink} function.\n */\nexport interface StreamSinkOptions {\n /**\n * The text formatter to use. Defaults to {@link defaultTextFormatter}.\n */\n formatter?: TextFormatter;\n\n /**\n * The text encoder to use. Defaults to an instance of {@link TextEncoder}.\n */\n encoder?: { encode(text: string): Uint8Array };\n}\n\n/**\n * A factory that returns a sink that writes to a {@link WritableStream}.\n *\n * Note that the `stream` is of Web Streams API, which is different from\n * Node.js streams. You can convert a Node.js stream to a Web Streams API\n * stream using [`stream.Writable.toWeb()`] method.\n *\n * [`stream.Writable.toWeb()`]: https://nodejs.org/api/stream.html#streamwritabletowebstreamwritable\n *\n * @example Sink to the standard error in Deno\n * ```typescript\n * const stderrSink = getStreamSink(Deno.stderr.writable);\n * ```\n *\n * @example Sink to the standard error in Node.js\n * ```typescript\n * import stream from \"node:stream\";\n * const stderrSink = getStreamSink(stream.Writable.toWeb(process.stderr));\n * ```\n *\n * @param stream The stream to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the stream.\n */\nexport function getStreamSink(\n stream: WritableStream,\n options: StreamSinkOptions = {},\n): Sink & AsyncDisposable {\n const formatter = options.formatter ?? defaultTextFormatter;\n const encoder = options.encoder ?? new TextEncoder();\n const writer = stream.getWriter();\n let lastPromise = Promise.resolve();\n const sink: Sink & AsyncDisposable = (record: LogRecord) => {\n const bytes = encoder.encode(formatter(record));\n lastPromise = lastPromise\n .then(() => writer.ready)\n .then(() => writer.write(bytes));\n };\n sink[Symbol.asyncDispose] = async () => {\n await lastPromise;\n await writer.close();\n };\n return sink;\n}\n\ntype ConsoleMethod = \"debug\" | \"info\" | \"log\" | \"warn\" | \"error\";\n\n/**\n * Options for the {@link getConsoleSink} function.\n */\nexport interface ConsoleSinkOptions {\n /**\n * The console formatter or text formatter to use.\n * Defaults to {@link defaultConsoleFormatter}.\n */\n formatter?: ConsoleFormatter | TextFormatter;\n\n /**\n * The mapping from log levels to console methods. Defaults to:\n *\n * ```typescript\n * {\n * trace: \"trace\",\n * debug: \"debug\",\n * info: \"info\",\n * warning: \"warn\",\n * error: \"error\",\n * fatal: \"error\",\n * }\n * ```\n * @since 0.9.0\n */\n levelMap?: Record<LogLevel, ConsoleMethod>;\n\n /**\n * The console to log to. Defaults to {@link console}.\n */\n console?: Console;\n}\n\n/**\n * A console sink factory that returns a sink that logs to the console.\n *\n * @param options The options for the sink.\n * @returns A sink that logs to the console.\n */\nexport function getConsoleSink(options: ConsoleSinkOptions = {}): Sink {\n const formatter = options.formatter ?? defaultConsoleFormatter;\n const levelMap: Record<LogLevel, ConsoleMethod> = {\n trace: \"debug\",\n debug: \"debug\",\n info: \"info\",\n warning: \"warn\",\n error: \"error\",\n fatal: \"error\",\n ...(options.levelMap ?? {}),\n };\n const console = options.console ?? globalThis.console;\n return (record: LogRecord) => {\n const args = formatter(record);\n const method = levelMap[record.level];\n if (method === undefined) {\n throw new TypeError(`Invalid log level: ${record.level}.`);\n }\n if (typeof args === \"string\") {\n const msg = args.replace(/\\r?\\n$/, \"\");\n console[method](msg);\n } else {\n console[method](...args);\n }\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmCA,SAAgB,WAAWA,MAAYC,QAA0B;CAC/D,MAAM,aAAa,SAAS,OAAO;AACnC,QAAO,CAACC,WAAsB;AAC5B,MAAI,WAAW,OAAO,CAAE,MAAK,OAAO;CACrC;AACF;;;;;;;;;;;;;;;;;;;AAyCD,SAAgB,WACdF,MACAG,UAA6B,CAAE,GACP;CACxB,MAAM,aAAa,QAAQ,cAAc;CACzC,MAAM,gBAAgB,QAAQ,iBAAiB;CAE/C,MAAMC,SAAsB,CAAE;CAC9B,IAAIC,aAAmD;CACvD,IAAI,WAAW;CAEf,SAAS,QAAc;AACrB,MAAI,OAAO,WAAW,EAAG;EAEzB,MAAM,UAAU,O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,QAAQ,aAAa;CACvC,MAAMC,WAA4C;EAChD,OAAO;EACP,OAAO;EACP,MAAM;EACN,SAAS;EACT,OAAO;EACP,OAAO;EACP,GAAI,QAAQ,YAAY,CAAE;CAC3B;CACD,MAAM,UAAU,QAAQ,WAAW,WAAW;AAC9C,QAAO,CAACT,WAAsB;EAC5B,MAAM,OAAO,UAAU,OAAO;EAC9B,MAAM,SAAS,SAAS,OAAO;AAC/B,MAAI,kBACF,OAAM,IAAI,WAAW,qBAAqB,OAAO,MAAM;AAEzD,aAAW,SAAS,UAAU;GAC5B,MAAM,MAAM,KAAK,QAAQ,UAAU,GAAG;AACtC,WAAQ,QAAQ,IAAI;EACrB,MACC,SAAQ,QAAQ,GAAG,KAAK;CAE3B;AACF"}
package/mod.ts CHANGED
@@ -44,11 +44,13 @@ export {
44
44
  export { getLogger, type Logger, type LogMethod } from "./logger.ts";
45
45
  export type { LogRecord } from "./record.ts";
46
46
  export {
47
+ type BufferSinkOptions,
47
48
  type ConsoleSinkOptions,
48
49
  getConsoleSink,
49
50
  getStreamSink,
50
51
  type Sink,
51
52
  type StreamSinkOptions,
53
+ withBuffer,
52
54
  withFilter,
53
55
  } from "./sink.ts";
54
56
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/logtape",
3
- "version": "1.0.0-dev.206+52e07555",
3
+ "version": "1.0.0-dev.208+b3f9fea2",
4
4
  "description": "Simple logging library with zero dependencies for Deno/Node.js/Bun/browsers",
5
5
  "keywords": [
6
6
  "logging",
package/sink.test.ts CHANGED
@@ -1,12 +1,20 @@
1
1
  import { suite } from "@alinea/suite";
2
2
  import { assertEquals } from "@std/assert/equals";
3
+ import { assertInstanceOf } from "@std/assert/instance-of";
3
4
  import { assertThrows } from "@std/assert/throws";
5
+ import { delay } from "@std/async/delay";
4
6
  import makeConsoleMock from "consolemock";
5
7
  import { debug, error, fatal, info, trace, warning } from "./fixtures.ts";
6
8
  import { defaultTextFormatter } from "./formatter.ts";
7
9
  import type { LogLevel } from "./level.ts";
8
10
  import type { LogRecord } from "./record.ts";
9
- import { getConsoleSink, getStreamSink, withFilter } from "./sink.ts";
11
+ import {
12
+ getConsoleSink,
13
+ getStreamSink,
14
+ type Sink,
15
+ withBuffer,
16
+ withFilter,
17
+ } from "./sink.ts";
10
18
 
11
19
  const test = suite(import.meta);
12
20
 
@@ -246,3 +254,384 @@ test("getConsoleSink()", () => {
246
254
  },
247
255
  ]);
248
256
  });
257
+
258
+ test("withBuffer() - buffer size limit", async () => {
259
+ const buffer: LogRecord[] = [];
260
+ const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
261
+
262
+ // Add records one by one
263
+ sink(trace);
264
+ assertEquals(buffer.length, 0); // Not flushed yet
265
+
266
+ sink(debug);
267
+ assertEquals(buffer.length, 0); // Not flushed yet
268
+
269
+ sink(info);
270
+ assertEquals(buffer.length, 3); // Flushed when buffer is full
271
+ assertEquals(buffer, [trace, debug, info]);
272
+
273
+ // Add more records
274
+ sink(warning);
275
+ assertEquals(buffer.length, 3); // Previous records remain
276
+
277
+ sink(error);
278
+ assertEquals(buffer.length, 3); // Still not flushed
279
+
280
+ sink(fatal);
281
+ assertEquals(buffer.length, 6); // Flushed again
282
+ assertEquals(buffer, [trace, debug, info, warning, error, fatal]);
283
+
284
+ await sink[Symbol.asyncDispose]();
285
+ });
286
+
287
+ test("withBuffer() - flush interval", async () => {
288
+ const buffer: LogRecord[] = [];
289
+ const sink = withBuffer(buffer.push.bind(buffer), {
290
+ bufferSize: 10,
291
+ flushInterval: 100,
292
+ });
293
+
294
+ sink(trace);
295
+ assertEquals(buffer.length, 0); // Not flushed immediately
296
+
297
+ // Wait for flush interval
298
+ await delay(150);
299
+ assertEquals(buffer.length, 1); // Flushed after interval
300
+ assertEquals(buffer, [trace]);
301
+
302
+ await sink[Symbol.asyncDispose]();
303
+ });
304
+
305
+ test("withBuffer() - dispose flushes remaining records", async () => {
306
+ const buffer: LogRecord[] = [];
307
+ const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
308
+
309
+ sink(trace);
310
+ sink(debug);
311
+ assertEquals(buffer.length, 0); // Not flushed yet
312
+
313
+ await sink[Symbol.asyncDispose]();
314
+ assertEquals(buffer.length, 2); // Flushed on dispose
315
+ assertEquals(buffer, [trace, debug]);
316
+ });
317
+
318
+ test("withBuffer() - disabled flush interval", async () => {
319
+ const buffer: LogRecord[] = [];
320
+ const sink = withBuffer(buffer.push.bind(buffer), {
321
+ bufferSize: 10,
322
+ flushInterval: 0,
323
+ });
324
+
325
+ sink(trace);
326
+ assertEquals(buffer.length, 0); // Not flushed immediately
327
+
328
+ // Wait longer than normal flush interval
329
+ await delay(200);
330
+ assertEquals(buffer.length, 0); // Still not flushed due to disabled interval
331
+
332
+ await sink[Symbol.asyncDispose]();
333
+ assertEquals(buffer.length, 1); // Flushed only on dispose
334
+ assertEquals(buffer, [trace]);
335
+ });
336
+
337
+ test("withBuffer() - default options", async () => {
338
+ const buffer: LogRecord[] = [];
339
+ const sink = withBuffer(buffer.push.bind(buffer));
340
+
341
+ // Add 9 records (less than default buffer size of 10)
342
+ for (let i = 0; i < 9; i++) {
343
+ sink(trace);
344
+ }
345
+ assertEquals(buffer.length, 0); // Not flushed yet
346
+
347
+ // Add 10th record to trigger flush
348
+ sink(trace);
349
+ assertEquals(buffer.length, 10); // Flushed when buffer is full
350
+
351
+ await sink[Symbol.asyncDispose]();
352
+ });
353
+
354
+ test("withBuffer() - no operation after dispose", async () => {
355
+ const buffer: LogRecord[] = [];
356
+ const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
357
+
358
+ await sink[Symbol.asyncDispose]();
359
+
360
+ // Try to add records after dispose
361
+ sink(trace);
362
+ sink(debug);
363
+ assertEquals(buffer.length, 0); // No records added after dispose
364
+ });
365
+
366
+ test("withBuffer() - disposes underlying AsyncDisposable sink", async () => {
367
+ const buffer: LogRecord[] = [];
368
+ let disposed = false;
369
+
370
+ const disposableSink: Sink & AsyncDisposable = (record: LogRecord) => {
371
+ buffer.push(record);
372
+ };
373
+ disposableSink[Symbol.asyncDispose] = () => {
374
+ disposed = true;
375
+ return Promise.resolve();
376
+ };
377
+
378
+ const bufferedSink = withBuffer(disposableSink);
379
+
380
+ await bufferedSink[Symbol.asyncDispose]();
381
+
382
+ assertEquals(disposed, true); // Underlying sink should be disposed
383
+ });
384
+
385
+ test("withBuffer() - disposes underlying Disposable sink", async () => {
386
+ const buffer: LogRecord[] = [];
387
+ let disposed = false;
388
+
389
+ const disposableSink: Sink & Disposable = (record: LogRecord) => {
390
+ buffer.push(record);
391
+ };
392
+ disposableSink[Symbol.dispose] = () => {
393
+ disposed = true;
394
+ };
395
+
396
+ const bufferedSink = withBuffer(disposableSink);
397
+
398
+ await bufferedSink[Symbol.asyncDispose]();
399
+
400
+ assertEquals(disposed, true); // Underlying sink should be disposed
401
+ });
402
+
403
+ test("withBuffer() - handles non-disposable sink gracefully", async () => {
404
+ const buffer: LogRecord[] = [];
405
+ const regularSink: Sink = (record: LogRecord) => {
406
+ buffer.push(record);
407
+ };
408
+
409
+ const bufferedSink = withBuffer(regularSink);
410
+
411
+ // Should not throw when disposing non-disposable sink
412
+ await bufferedSink[Symbol.asyncDispose]();
413
+
414
+ // This test passes if no error is thrown
415
+ assertEquals(true, true);
416
+ });
417
+
418
+ test("withBuffer() - edge case: bufferSize 1", async () => {
419
+ const buffer: LogRecord[] = [];
420
+ const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 1 });
421
+
422
+ // Every record should flush immediately
423
+ sink(trace);
424
+ assertEquals(buffer.length, 1);
425
+ assertEquals(buffer, [trace]);
426
+
427
+ sink(debug);
428
+ assertEquals(buffer.length, 2);
429
+ assertEquals(buffer, [trace, debug]);
430
+
431
+ await sink[Symbol.asyncDispose]();
432
+ });
433
+
434
+ test("withBuffer() - edge case: bufferSize 0 or negative", async () => {
435
+ const buffer: LogRecord[] = [];
436
+ const sink1 = withBuffer(buffer.push.bind(buffer), { bufferSize: 0 });
437
+ const sink2 = withBuffer(buffer.push.bind(buffer), { bufferSize: -5 });
438
+
439
+ // Should still work, but behavior may vary
440
+ sink1(trace);
441
+ sink2(debug);
442
+
443
+ await sink1[Symbol.asyncDispose]();
444
+ await sink2[Symbol.asyncDispose]();
445
+
446
+ assertEquals(buffer.length, 2);
447
+ assertEquals(buffer, [trace, debug]);
448
+ });
449
+
450
+ test("withBuffer() - edge case: very large bufferSize", async () => {
451
+ const buffer: LogRecord[] = [];
452
+ const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10000 });
453
+
454
+ // Add many records without hitting buffer limit
455
+ for (let i = 0; i < 100; i++) {
456
+ sink(trace);
457
+ }
458
+ assertEquals(buffer.length, 0); // Not flushed yet
459
+
460
+ await sink[Symbol.asyncDispose]();
461
+ assertEquals(buffer.length, 100); // All flushed on dispose
462
+ });
463
+
464
+ test("withBuffer() - edge case: rapid successive calls", async () => {
465
+ const buffer: LogRecord[] = [];
466
+ const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 3 });
467
+
468
+ // Add records in rapid succession
469
+ sink(trace);
470
+ sink(debug);
471
+ sink(info); // This should trigger flush
472
+ sink(warning);
473
+ sink(error);
474
+ sink(fatal); // This should trigger another flush
475
+
476
+ assertEquals(buffer.length, 6);
477
+ assertEquals(buffer, [trace, debug, info, warning, error, fatal]);
478
+
479
+ await sink[Symbol.asyncDispose]();
480
+ });
481
+
482
+ test("withBuffer() - edge case: multiple timer flushes", async () => {
483
+ const buffer: LogRecord[] = [];
484
+ const sink = withBuffer(buffer.push.bind(buffer), {
485
+ bufferSize: 10,
486
+ flushInterval: 50,
487
+ });
488
+
489
+ sink(trace);
490
+ await delay(60);
491
+ assertEquals(buffer.length, 1); // First flush
492
+
493
+ sink(debug);
494
+ await delay(60);
495
+ assertEquals(buffer.length, 2); // Second flush
496
+
497
+ sink(info);
498
+ await delay(60);
499
+ assertEquals(buffer.length, 3); // Third flush
500
+
501
+ await sink[Symbol.asyncDispose]();
502
+ });
503
+
504
+ test("withBuffer() - edge case: timer and buffer size interaction", async () => {
505
+ const buffer: LogRecord[] = [];
506
+ const sink = withBuffer(buffer.push.bind(buffer), {
507
+ bufferSize: 3,
508
+ flushInterval: 100,
509
+ });
510
+
511
+ // Add 2 records (less than bufferSize)
512
+ sink(trace);
513
+ sink(debug);
514
+ assertEquals(buffer.length, 0);
515
+
516
+ // Wait for timer flush
517
+ await delay(120);
518
+ assertEquals(buffer.length, 2); // Timer flush
519
+
520
+ // Add 3 more records to trigger buffer flush
521
+ sink(info);
522
+ sink(warning);
523
+ sink(error); // Should trigger immediate flush
524
+ assertEquals(buffer.length, 5);
525
+
526
+ await sink[Symbol.asyncDispose]();
527
+ });
528
+
529
+ test("withBuffer() - edge case: dispose called multiple times", async () => {
530
+ const buffer: LogRecord[] = [];
531
+ const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
532
+
533
+ sink(trace);
534
+ sink(debug);
535
+
536
+ // First dispose
537
+ await sink[Symbol.asyncDispose]();
538
+ assertEquals(buffer.length, 2);
539
+
540
+ // Second dispose - should not cause errors or duplicate records
541
+ await sink[Symbol.asyncDispose]();
542
+ assertEquals(buffer.length, 2); // Should remain the same
543
+
544
+ // Third dispose
545
+ await sink[Symbol.asyncDispose]();
546
+ assertEquals(buffer.length, 2); // Should remain the same
547
+ });
548
+
549
+ test("withBuffer() - edge case: underlying sink throws error", async () => {
550
+ let errorCount = 0;
551
+ const errorSink: Sink = () => {
552
+ errorCount++;
553
+ throw new Error("Sink error");
554
+ };
555
+
556
+ const bufferedSink = withBuffer(errorSink, { bufferSize: 2 });
557
+
558
+ // First record goes to buffer
559
+ bufferedSink(trace);
560
+
561
+ // Second record should trigger flush and throw error
562
+ try {
563
+ bufferedSink(debug);
564
+ // Should not reach here
565
+ assertEquals(true, false, "Expected error to be thrown");
566
+ } catch (error) {
567
+ assertInstanceOf(error, Error);
568
+ assertEquals(error.message, "Sink error");
569
+ assertEquals(errorCount, 1); // Only first record processed before error
570
+ }
571
+
572
+ await bufferedSink[Symbol.asyncDispose]();
573
+ });
574
+
575
+ test("withBuffer() - edge case: underlying AsyncDisposable throws error", async () => {
576
+ const buffer: LogRecord[] = [];
577
+ let disposed = false;
578
+
579
+ const errorDisposableSink: Sink & AsyncDisposable = (record: LogRecord) => {
580
+ buffer.push(record);
581
+ };
582
+ errorDisposableSink[Symbol.asyncDispose] = () => {
583
+ disposed = true;
584
+ throw new Error("Dispose error");
585
+ };
586
+
587
+ const bufferedSink = withBuffer(errorDisposableSink);
588
+
589
+ bufferedSink(trace);
590
+
591
+ try {
592
+ await bufferedSink[Symbol.asyncDispose]();
593
+ // Should not reach here
594
+ assertEquals(true, false, "Expected dispose error to be thrown");
595
+ } catch (error) {
596
+ assertInstanceOf(error, Error);
597
+ assertEquals(error.message, "Dispose error");
598
+ assertEquals(disposed, true); // Should still be disposed
599
+ assertEquals(buffer.length, 1); // Buffer should have been flushed before dispose error
600
+ }
601
+ });
602
+
603
+ test("withBuffer() - edge case: negative flushInterval", async () => {
604
+ const buffer: LogRecord[] = [];
605
+ const sink = withBuffer(buffer.push.bind(buffer), {
606
+ bufferSize: 10,
607
+ flushInterval: -1000,
608
+ });
609
+
610
+ sink(trace);
611
+ assertEquals(buffer.length, 0);
612
+
613
+ // Wait longer than a normal flush interval
614
+ await delay(200);
615
+ assertEquals(buffer.length, 0); // Should not flush due to negative interval
616
+
617
+ await sink[Symbol.asyncDispose]();
618
+ assertEquals(buffer.length, 1); // Should only flush on dispose
619
+ });
620
+
621
+ test("withBuffer() - edge case: concurrent dispose and log calls", async () => {
622
+ const buffer: LogRecord[] = [];
623
+ const sink = withBuffer(buffer.push.bind(buffer), { bufferSize: 10 });
624
+
625
+ sink(trace);
626
+
627
+ // Start dispose and immediately try to log more
628
+ const disposePromise = sink[Symbol.asyncDispose]();
629
+ sink(debug); // This should be ignored since dispose is in progress
630
+ sink(info); // This should be ignored since dispose is in progress
631
+
632
+ await disposePromise;
633
+
634
+ // Only the first record should be in buffer
635
+ assertEquals(buffer.length, 1);
636
+ assertEquals(buffer, [trace]);
637
+ });
package/sink.ts CHANGED
@@ -40,6 +40,115 @@ export function withFilter(sink: Sink, filter: FilterLike): Sink {
40
40
  };
41
41
  }
42
42
 
43
+ /**
44
+ * Options for the {@link withBuffer} function.
45
+ * @since 1.0.0
46
+ */
47
+ export interface BufferSinkOptions {
48
+ /**
49
+ * The maximum number of log records to buffer before flushing to the
50
+ * underlying sink.
51
+ * @default `10`
52
+ */
53
+ bufferSize?: number;
54
+
55
+ /**
56
+ * The maximum time in milliseconds to wait before flushing buffered records
57
+ * to the underlying sink. Defaults to 5000 (5 seconds). Set to 0 or
58
+ * negative to disable time-based flushing.
59
+ * @default `5000`
60
+ */
61
+ flushInterval?: number;
62
+ }
63
+
64
+ /**
65
+ * Turns a sink into a buffered sink. The returned sink buffers log records
66
+ * in memory and flushes them to the underlying sink when the buffer is full
67
+ * or after a specified time interval.
68
+ *
69
+ * @example Buffer a console sink with custom options
70
+ * ```typescript
71
+ * const sink = withBuffer(getConsoleSink(), {
72
+ * bufferSize: 5,
73
+ * flushInterval: 1000
74
+ * });
75
+ * ```
76
+ *
77
+ * @param sink A sink to be buffered.
78
+ * @param options Options for the buffered sink.
79
+ * @returns A buffered sink that flushes records periodically.
80
+ * @since 1.0.0
81
+ */
82
+ export function withBuffer(
83
+ sink: Sink,
84
+ options: BufferSinkOptions = {},
85
+ ): Sink & AsyncDisposable {
86
+ const bufferSize = options.bufferSize ?? 10;
87
+ const flushInterval = options.flushInterval ?? 5000;
88
+
89
+ const buffer: LogRecord[] = [];
90
+ let flushTimer: ReturnType<typeof setTimeout> | null = null;
91
+ let disposed = false;
92
+
93
+ function flush(): void {
94
+ if (buffer.length === 0) return;
95
+
96
+ const records = buffer.splice(0);
97
+ for (const record of records) {
98
+ try {
99
+ sink(record);
100
+ } catch (error) {
101
+ // Errors are handled by the sink infrastructure
102
+ throw error;
103
+ }
104
+ }
105
+
106
+ if (flushTimer !== null) {
107
+ clearTimeout(flushTimer);
108
+ flushTimer = null;
109
+ }
110
+ }
111
+
112
+ function scheduleFlush(): void {
113
+ if (flushInterval <= 0 || flushTimer !== null || disposed) return;
114
+
115
+ flushTimer = setTimeout(() => {
116
+ flushTimer = null;
117
+ flush();
118
+ }, flushInterval);
119
+ }
120
+
121
+ const bufferedSink: Sink & AsyncDisposable = (record: LogRecord) => {
122
+ if (disposed) return;
123
+
124
+ buffer.push(record);
125
+
126
+ if (buffer.length >= bufferSize) {
127
+ flush();
128
+ } else {
129
+ scheduleFlush();
130
+ }
131
+ };
132
+
133
+ bufferedSink[Symbol.asyncDispose] = async () => {
134
+ disposed = true;
135
+ if (flushTimer !== null) {
136
+ clearTimeout(flushTimer);
137
+ flushTimer = null;
138
+ }
139
+ flush();
140
+
141
+ // Dispose the underlying sink if it's disposable
142
+ if (Symbol.asyncDispose in sink) {
143
+ await (sink as AsyncDisposable)[Symbol.asyncDispose]();
144
+ } else if (Symbol.dispose in sink) {
145
+ (sink as Disposable)[Symbol.dispose]();
146
+ }
147
+ };
148
+
149
+ return bufferedSink;
150
+ }
151
+
43
152
  /**
44
153
  * Options for the {@link getStreamSink} function.
45
154
  */