Skip to content

Commit

Permalink
Fix unwanted filters behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
KROT47 committed Oct 9, 2024
1 parent 5663087 commit 4e61a2f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
32 changes: 28 additions & 4 deletions logtape/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getConfig,
reset,
} from "./config.ts";
import type { Filter, FilterLike } from "./filter.ts";
import { type Filter, type FilterLike, toFilter } from "./filter.ts";
import { LoggerImpl } from "./logger.ts";
import type { LogRecord } from "./record.ts";
import type { Sink } from "./sink.ts";
Expand All @@ -28,14 +28,16 @@ Deno.test("configure()", async (t) => {
b[Symbol.dispose] = () => ++disposed;
const cLogs: LogRecord[] = [];
const c: Sink = cLogs.push.bind(cLogs);
const dLogs: LogRecord[] = [];
const d: Sink = dLogs.push.bind(dLogs);
const x: Filter & AsyncDisposable = () => true;
x[Symbol.asyncDispose] = () => {
++disposed;
return Promise.resolve();
};
const y: Filter & Disposable = () => true;
y[Symbol.dispose] = () => ++disposed;
const sinks = { a, b, c };
const sinks = { a, b, c, d };
const filters: Record<string, FilterLike> = {
x,
y,
Expand Down Expand Up @@ -66,16 +68,27 @@ Deno.test("configure()", async (t) => {
filters: ["debug"],
level: "info",
},
{
category: ["my-app", "test"],
sinks: ["d"],
level: "debug",
filters: ["warning"],
},
{
category: ["my-app", "test", "no_level"],
},
],
};
await configure(config);
const getFiltersExpectedString = (filters: Filter[]) =>
[...filters, toFilter("debug")].toString();

const logger = LoggerImpl.getLogger("my-app");
assertEquals(logger.sinks, [a]);
assertEquals(logger.filters, [x]);
assertEquals(logger.filters.toString(), getFiltersExpectedString([x]));
const foo = LoggerImpl.getLogger(["my-app", "foo"]);
assertEquals(foo.sinks, [b]);
assertEquals(foo.filters, [y]);
assertEquals(foo.filters.toString(), getFiltersExpectedString([y]));
const bar = LoggerImpl.getLogger(["my-app", "bar"]);
assertEquals(bar.sinks, [c]);
bar.debug("ignored");
Expand Down Expand Up @@ -126,6 +139,17 @@ Deno.test("configure()", async (t) => {
timestamp: cLogs[0].timestamp,
},
]);
const test = LoggerImpl.getLogger(["my-app", "test", "no_level"]);
assertEquals(test.filters.toString(), getFiltersExpectedString([]));
test.debug("logged");
assertEquals(dLogs, [{
level: "debug",
category: ["my-app", "test", "no_level"],
message: ["logged"],
rawMessage: "logged",
properties: {},
timestamp: dLogs[0].timestamp,
}]);
assertStrictEquals(getConfig(), config);
});

Expand Down
4 changes: 3 additions & 1 deletion logtape/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ export async function configure<
logger.sinks.push(sink);
}
logger.parentSinks = cfg.parentSinks ?? "inherit";
if (cfg.level !== undefined) logger.filters.push(toFilter(cfg.level));
logger.filters.push(
toFilter(cfg.level === undefined ? "debug" : cfg.level),
);
for (const filterId of cfg.filters ?? []) {
const filter = config.filters?.[filterId];
if (filter === undefined) {
Expand Down

0 comments on commit 4e61a2f

Please sign in to comment.