From 42b1038514b50c537a66d66718ed94aabcea0c5a Mon Sep 17 00:00:00 2001 From: Hong Minhee Date: Sun, 21 Apr 2024 17:41:04 +0900 Subject: [PATCH] getConfig() function --- CHANGES.md | 1 + logtape/config.test.ts | 31 ++++++++++++++++++++++++------- logtape/config.ts | 18 +++++++++++++----- logtape/mod.ts | 1 + 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ed9dcbf..7c7e18d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ To be released. - Added `parseLogLevel()` function. - Added `isLogLevel()` function. + - Added `getConfig()` function. Version 0.2.2 diff --git a/logtape/config.test.ts b/logtape/config.test.ts index 8e44802..a726d1f 100644 --- a/logtape/config.test.ts +++ b/logtape/config.test.ts @@ -1,6 +1,13 @@ import { assertEquals } from "@std/assert/assert-equals"; import { assertRejects } from "@std/assert/assert-rejects"; -import { ConfigError, configure, reset } from "./config.ts"; +import { assertStrictEquals } from "jsr:@std/assert/assert-strict-equals"; +import { + type Config, + ConfigError, + configure, + getConfig, + reset, +} from "./config.ts"; import type { Filter } from "./filter.ts"; import { LoggerImpl } from "./logger.ts"; import type { LogRecord } from "./record.ts"; @@ -26,7 +33,7 @@ Deno.test("configure()", async (t) => { }; const y: Filter & Disposable = () => true; y[Symbol.dispose] = () => ++disposed; - await configure({ + const config: Config = { sinks: { a, b, c }, filters: { x, y, debug: "debug" }, loggers: [ @@ -47,7 +54,8 @@ Deno.test("configure()", async (t) => { level: "info", }, ], - }); + }; + await configure(config); const logger = LoggerImpl.getLogger("my-app"); assertEquals(logger.sinks, [a]); @@ -69,6 +77,7 @@ Deno.test("configure()", async (t) => { timestamp: cLogs[0].timestamp, }, ]); + assertStrictEquals(getConfig(), config); }); await t.step("reconfigure", async () => { @@ -85,17 +94,20 @@ Deno.test("configure()", async (t) => { assertEquals(disposed, 0); // No exception if reset is true: - await configure({ + const config = { sinks: {}, filters: {}, loggers: [{ category: "my-app" }], reset: true, - }); + }; + await configure(config); assertEquals(disposed, 4); + assertStrictEquals(getConfig(), config); }); await t.step("tear down", async () => { await reset(); + assertStrictEquals(getConfig(), null); }); await t.step("misconfiguration", async () => { @@ -116,6 +128,7 @@ Deno.test("configure()", async (t) => { ConfigError, "Sink not found: invalid", ); + assertStrictEquals(getConfig(), null); await assertRejects( () => @@ -134,6 +147,7 @@ Deno.test("configure()", async (t) => { ConfigError, "Filter not found: invalid", ); + assertStrictEquals(getConfig(), null); }); const metaCategories = [[], ["logtape"], ["logtape", "meta"]]; @@ -141,7 +155,7 @@ Deno.test("configure()", async (t) => { await t.step( "meta configuration: " + JSON.stringify(metaCategory), async () => { - await configure({ + const config = { sinks: {}, filters: {}, loggers: [ @@ -151,14 +165,17 @@ Deno.test("configure()", async (t) => { filters: [], }, ], - }); + }; + await configure(config); assertEquals(LoggerImpl.getLogger(["logger", "meta"]).sinks, []); + assertStrictEquals(getConfig(), config); }, ); await t.step("tear down", async () => { await reset(); + assertStrictEquals(getConfig(), null); }); } }); diff --git a/logtape/config.ts b/logtape/config.ts index 85d4093..acd983c 100644 --- a/logtape/config.ts +++ b/logtape/config.ts @@ -60,9 +60,9 @@ export interface LoggerConfig< } /** - * Whether the loggers are configured. + * The current configuration, if any. Otherwise, `null`. */ -let configured = false; +let currentConfig: Config | null = null; /** * Strong references to the loggers. @@ -124,13 +124,13 @@ export async function configure< TSinkId extends string, TFilterId extends string, >(config: Config): Promise { - if (configured && !config.reset) { + if (currentConfig != null && !config.reset) { throw new ConfigError( "Already configured; if you want to reset, turn on the reset flag.", ); } await reset(); - configured = true; + currentConfig = config; let metaConfigured = false; @@ -204,6 +204,14 @@ export async function configure< ); } +/** + * Get the current configuration, if any. Otherwise, `null`. + * @returns The current configuration, if any. Otherwise, `null`. + */ +export function getConfig(): Config | null { + return currentConfig; +} + /** * Reset the configuration. Mostly for testing purposes. */ @@ -211,7 +219,7 @@ export async function reset(): Promise { await dispose(); LoggerImpl.getLogger([]).resetDescendants(); strongRefs.clear(); - configured = false; + currentConfig = null; } /** diff --git a/logtape/mod.ts b/logtape/mod.ts index 44af7d1..4616b70 100644 --- a/logtape/mod.ts +++ b/logtape/mod.ts @@ -3,6 +3,7 @@ export { ConfigError, configure, dispose, + getConfig, type LoggerConfig, reset, } from "./config.ts";