// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { test } from "../testing/mod.ts"; import { assertEquals } from "../testing/asserts.ts"; import * as log from "./mod.ts"; import { LogLevel } from "./levels.ts"; import "./handlers_test.ts"; import "./logger_test.ts"; class TestHandler extends log.handlers.BaseHandler { public messages: string[] = []; log(msg: string): void { this.messages.push(msg); } } test(async function defaultHandlers(): Promise { const loggers = { DEBUG: log.debug, INFO: log.info, WARNING: log.warning, ERROR: log.error, CRITICAL: log.critical }; for (const levelName in LogLevel) { if (levelName === "NOTSET") { continue; } const level = LogLevel[levelName]; const logger = loggers[levelName]; const handler = new TestHandler(level); await log.setup({ handlers: { default: handler }, loggers: { default: { level: levelName, handlers: ["default"] } } }); logger("foo"); logger("bar", 1, 2); assertEquals(handler.messages, [`${levelName} foo`, `${levelName} bar`]); } }); test(async function getLogger(): Promise { const handler = new TestHandler("DEBUG"); await log.setup({ handlers: { default: handler }, loggers: { default: { level: "DEBUG", handlers: ["default"] } } }); const logger = log.getLogger(); assertEquals(logger.levelName, "DEBUG"); assertEquals(logger.handlers, [handler]); }); test(async function getLoggerWithName(): Promise { const fooHandler = new TestHandler("DEBUG"); await log.setup({ handlers: { foo: fooHandler }, loggers: { bar: { level: "INFO", handlers: ["foo"] } } }); const logger = log.getLogger("bar"); assertEquals(logger.levelName, "INFO"); assertEquals(logger.handlers, [fooHandler]); }); test(async function getLoggerUnknown(): Promise { await log.setup({ handlers: {}, loggers: {} }); const logger = log.getLogger("nonexistent"); assertEquals(logger.levelName, "NOTSET"); assertEquals(logger.handlers, []); });