Skip to content

Commit

Permalink
getConfig() function
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Apr 21, 2024
1 parent 743a76b commit 42b1038
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ To be released.

- Added `parseLogLevel()` function.
- Added `isLogLevel()` function.
- Added `getConfig()` function.


Version 0.2.2
Expand Down
31 changes: 24 additions & 7 deletions logtape/config.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -26,7 +33,7 @@ Deno.test("configure()", async (t) => {
};
const y: Filter & Disposable = () => true;
y[Symbol.dispose] = () => ++disposed;
await configure({
const config: Config<string, string> = {
sinks: { a, b, c },
filters: { x, y, debug: "debug" },
loggers: [
Expand All @@ -47,7 +54,8 @@ Deno.test("configure()", async (t) => {
level: "info",
},
],
});
};
await configure(config);

const logger = LoggerImpl.getLogger("my-app");
assertEquals(logger.sinks, [a]);
Expand All @@ -69,6 +77,7 @@ Deno.test("configure()", async (t) => {
timestamp: cLogs[0].timestamp,
},
]);
assertStrictEquals(getConfig(), config);
});

await t.step("reconfigure", async () => {
Expand All @@ -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 () => {
Expand All @@ -116,6 +128,7 @@ Deno.test("configure()", async (t) => {
ConfigError,
"Sink not found: invalid",
);
assertStrictEquals(getConfig(), null);

await assertRejects(
() =>
Expand All @@ -134,14 +147,15 @@ Deno.test("configure()", async (t) => {
ConfigError,
"Filter not found: invalid",
);
assertStrictEquals(getConfig(), null);
});

const metaCategories = [[], ["logtape"], ["logtape", "meta"]];
for (const metaCategory of metaCategories) {
await t.step(
"meta configuration: " + JSON.stringify(metaCategory),
async () => {
await configure({
const config = {
sinks: {},
filters: {},
loggers: [
Expand All @@ -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);
});
}
});
18 changes: 13 additions & 5 deletions logtape/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> | null = null;

/**
* Strong references to the loggers.
Expand Down Expand Up @@ -124,13 +124,13 @@ export async function configure<
TSinkId extends string,
TFilterId extends string,
>(config: Config<TSinkId, TFilterId>): Promise<void> {
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;

Expand Down Expand Up @@ -204,14 +204,22 @@ export async function configure<
);
}

/**
* Get the current configuration, if any. Otherwise, `null`.
* @returns The current configuration, if any. Otherwise, `null`.
*/
export function getConfig(): Config<string, string> | null {
return currentConfig;
}

/**
* Reset the configuration. Mostly for testing purposes.
*/
export async function reset(): Promise<void> {
await dispose();
LoggerImpl.getLogger([]).resetDescendants();
strongRefs.clear();
configured = false;
currentConfig = null;
}

/**
Expand Down
1 change: 1 addition & 0 deletions logtape/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
ConfigError,
configure,
dispose,
getConfig,
type LoggerConfig,
reset,
} from "./config.ts";
Expand Down

0 comments on commit 42b1038

Please sign in to comment.