From 940f8e8433ae5ec74b2642438849089a0433e512 Mon Sep 17 00:00:00 2001 From: Chris Knight Date: Thu, 18 Jun 2020 11:50:18 +0100 Subject: feat(std/log): expose logger name to LogRecord (#6316) --- std/log/mod_test.ts | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'std/log/mod_test.ts') diff --git a/std/log/mod_test.ts b/std/log/mod_test.ts index 98ac093c8..9de5d76af 100644 --- a/std/log/mod_test.ts +++ b/std/log/mod_test.ts @@ -1,7 +1,24 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { assert, assertEquals } from "../testing/asserts.ts"; -import { getLogger, debug, info, warning, error, critical } from "./mod.ts"; +import { + getLogger, + debug, + info, + warning, + error, + critical, + setup, +} from "./mod.ts"; import { Logger } from "./logger.ts"; +import { BaseHandler } from "./handlers.ts"; + +class TestHandler extends BaseHandler { + public messages: string[] = []; + + public log(str: string): void { + this.messages.push(str); + } +} let logger: Logger | null = null; try { @@ -39,3 +56,36 @@ Deno.test("default loggers work as expected", function (): void { assertEquals(criticalData, "foo"); assertEquals(criticalResolver, "bar"); }); + +Deno.test({ + name: "Logging config works as expected with logger names", + async fn() { + const consoleHandler = new TestHandler("DEBUG"); + const anotherConsoleHandler = new TestHandler("DEBUG", { + formatter: "[{loggerName}] {levelName} {msg}", + }); + await setup({ + handlers: { + console: consoleHandler, + anotherConsole: anotherConsoleHandler, + }, + + loggers: { + // configure default logger available via short-hand methods above + default: { + level: "DEBUG", + handlers: ["console"], + }, + + tasks: { + level: "ERROR", + handlers: ["anotherConsole"], + }, + }, + }); + getLogger().debug("hello"); + getLogger("tasks").error("world"); + assertEquals(consoleHandler.messages[0], "DEBUG hello"); + assertEquals(anotherConsoleHandler.messages[0], "[tasks] ERROR world"); + }, +}); -- cgit v1.2.3