2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-04-25 05:13:26 -04:00
|
|
|
import { assertEquals, assertThrows } from "../testing/asserts.ts";
|
2019-01-12 16:50:04 -05:00
|
|
|
import * as log from "./mod.ts";
|
2020-04-25 05:13:26 -04:00
|
|
|
import {
|
|
|
|
getLevelByName,
|
|
|
|
getLevelName,
|
2020-09-27 06:22:32 -04:00
|
|
|
LevelName,
|
|
|
|
LogLevelNames,
|
2020-04-25 05:13:26 -04:00
|
|
|
} from "./levels.ts";
|
2018-12-19 13:16:45 -05:00
|
|
|
|
2019-01-02 09:12:48 -05:00
|
|
|
class TestHandler extends log.handlers.BaseHandler {
|
2019-01-27 10:21:00 -05:00
|
|
|
public messages: string[] = [];
|
2019-01-02 09:12:48 -05:00
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
log(msg: string): void {
|
2019-01-27 10:21:00 -05:00
|
|
|
this.messages.push(msg);
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("defaultHandlers", async function (): Promise<void> {
|
2019-06-06 12:56:33 -04:00
|
|
|
const loggers: {
|
|
|
|
[key: string]: (msg: string, ...args: unknown[]) => void;
|
|
|
|
} = {
|
2019-01-27 10:21:00 -05:00
|
|
|
DEBUG: log.debug,
|
|
|
|
INFO: log.info,
|
|
|
|
WARNING: log.warning,
|
|
|
|
ERROR: log.error,
|
2020-03-28 13:03:49 -04:00
|
|
|
CRITICAL: log.critical,
|
2019-01-27 10:21:00 -05:00
|
|
|
};
|
2018-12-19 13:16:45 -05:00
|
|
|
|
2020-04-25 05:13:26 -04:00
|
|
|
for (const levelName of LogLevelNames) {
|
2019-01-27 10:21:00 -05:00
|
|
|
if (levelName === "NOTSET") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const logger = loggers[levelName];
|
2020-04-25 05:13:26 -04:00
|
|
|
const handler = new TestHandler(levelName as LevelName);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
|
|
|
await log.setup({
|
|
|
|
handlers: {
|
2020-03-28 13:03:49 -04:00
|
|
|
default: handler,
|
2019-01-27 10:21:00 -05:00
|
|
|
},
|
|
|
|
loggers: {
|
|
|
|
default: {
|
2020-04-25 05:13:26 -04:00
|
|
|
level: levelName as LevelName,
|
2020-03-28 13:03:49 -04:00
|
|
|
handlers: ["default"],
|
|
|
|
},
|
|
|
|
},
|
2019-01-27 10:21:00 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
logger("foo");
|
|
|
|
logger("bar", 1, 2);
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, [`${levelName} foo`, `${levelName} bar`]);
|
2019-01-27 10:21:00 -05:00
|
|
|
}
|
2019-01-02 09:12:48 -05:00
|
|
|
});
|
2019-01-19 13:46:46 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("getLogger", async function (): Promise<void> {
|
2019-01-27 10:21:00 -05:00
|
|
|
const handler = new TestHandler("DEBUG");
|
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
await log.setup({
|
|
|
|
handlers: {
|
2020-03-28 13:03:49 -04:00
|
|
|
default: handler,
|
2019-01-19 13:46:46 -05:00
|
|
|
},
|
|
|
|
loggers: {
|
2019-01-27 10:21:00 -05:00
|
|
|
default: {
|
2019-01-19 13:46:46 -05:00
|
|
|
level: "DEBUG",
|
2020-03-28 13:03:49 -04:00
|
|
|
handlers: ["default"],
|
|
|
|
},
|
|
|
|
},
|
2019-01-19 13:46:46 -05:00
|
|
|
});
|
|
|
|
|
2019-01-27 10:21:00 -05:00
|
|
|
const logger = log.getLogger();
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(logger.levelName, "DEBUG");
|
|
|
|
assertEquals(logger.handlers, [handler]);
|
2019-01-19 13:46:46 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("getLoggerWithName", async function (): Promise<void> {
|
2019-01-27 10:21:00 -05:00
|
|
|
const fooHandler = new TestHandler("DEBUG");
|
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
await log.setup({
|
|
|
|
handlers: {
|
2020-03-28 13:03:49 -04:00
|
|
|
foo: fooHandler,
|
2019-01-19 13:46:46 -05:00
|
|
|
},
|
|
|
|
loggers: {
|
2019-01-27 10:21:00 -05:00
|
|
|
bar: {
|
|
|
|
level: "INFO",
|
2020-03-28 13:03:49 -04:00
|
|
|
handlers: ["foo"],
|
|
|
|
},
|
|
|
|
},
|
2019-01-19 13:46:46 -05:00
|
|
|
});
|
|
|
|
|
2019-01-27 10:21:00 -05:00
|
|
|
const logger = log.getLogger("bar");
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(logger.levelName, "INFO");
|
|
|
|
assertEquals(logger.handlers, [fooHandler]);
|
2019-01-19 13:46:46 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("getLoggerUnknown", async function (): Promise<void> {
|
2019-01-19 13:46:46 -05:00
|
|
|
await log.setup({
|
2019-01-28 11:17:00 -05:00
|
|
|
handlers: {},
|
2020-03-28 13:03:49 -04:00
|
|
|
loggers: {},
|
2019-01-19 13:46:46 -05:00
|
|
|
});
|
|
|
|
|
2019-01-27 10:21:00 -05:00
|
|
|
const logger = log.getLogger("nonexistent");
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(logger.levelName, "NOTSET");
|
|
|
|
assertEquals(logger.handlers, []);
|
2019-01-21 10:35:07 -05:00
|
|
|
});
|
2020-04-25 05:13:26 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("getInvalidLoggerLevels", function (): void {
|
2020-04-25 05:13:26 -04:00
|
|
|
assertThrows(() => getLevelByName("FAKE_LOG_LEVEL" as LevelName));
|
|
|
|
assertThrows(() => getLevelName(5000));
|
|
|
|
});
|