2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-02-11 11:24:27 -05:00
|
|
|
const { test } = Deno;
|
2019-03-06 19:42:24 -05:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2019-01-12 16:50:04 -05:00
|
|
|
import * as log from "./mod.ts";
|
2019-01-27 10:21:00 -05:00
|
|
|
import { LogLevel } 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function defaultHandlers(): 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,
|
|
|
|
CRITICAL: log.critical
|
|
|
|
};
|
2018-12-19 13:16:45 -05:00
|
|
|
|
2019-01-27 10:21:00 -05:00
|
|
|
for (const levelName in LogLevel) {
|
|
|
|
if (levelName === "NOTSET") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const logger = loggers[levelName];
|
2019-05-30 08:59:30 -04:00
|
|
|
const handler = new TestHandler(levelName);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
|
|
|
await log.setup({
|
|
|
|
handlers: {
|
|
|
|
default: handler
|
|
|
|
},
|
|
|
|
loggers: {
|
|
|
|
default: {
|
|
|
|
level: levelName,
|
|
|
|
handlers: ["default"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function getLogger(): 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: {
|
2019-01-28 11:17:00 -05: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",
|
2019-01-27 10:21:00 -05:00
|
|
|
handlers: ["default"]
|
2019-01-21 10:35:07 -05:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function getLoggerWithName(): 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: {
|
2019-01-27 10:21:00 -05:00
|
|
|
foo: fooHandler
|
2019-01-19 13:46:46 -05:00
|
|
|
},
|
|
|
|
loggers: {
|
2019-01-27 10:21:00 -05:00
|
|
|
bar: {
|
|
|
|
level: "INFO",
|
|
|
|
handlers: ["foo"]
|
2019-01-21 10:35:07 -05:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function getLoggerUnknown(): Promise<void> {
|
2019-01-19 13:46:46 -05:00
|
|
|
await log.setup({
|
2019-01-28 11:17:00 -05:00
|
|
|
handlers: {},
|
|
|
|
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
|
|
|
});
|