2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-01-27 10:21:00 -05:00
|
|
|
import { assertEqual, test } from "../testing/mod.ts";
|
|
|
|
import { LogRecord, Logger } from "./logger.ts";
|
|
|
|
import { LogLevel } from "./levels.ts";
|
|
|
|
import { BaseHandler } from "./handlers.ts";
|
|
|
|
|
|
|
|
class TestHandler extends BaseHandler {
|
|
|
|
public messages: string[] = [];
|
|
|
|
public records: LogRecord[] = [];
|
|
|
|
|
|
|
|
handle(record: LogRecord): void {
|
2019-01-28 11:17:00 -05:00
|
|
|
this.records.push({ ...record, datetime: null });
|
2019-01-27 10:21:00 -05:00
|
|
|
super.handle(record);
|
|
|
|
}
|
|
|
|
|
|
|
|
public log(str: string): void {
|
|
|
|
this.messages.push(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test(function simpleLogger() {
|
|
|
|
const handler = new TestHandler("DEBUG");
|
|
|
|
let logger = new Logger("DEBUG");
|
|
|
|
|
|
|
|
assertEqual(logger.level, LogLevel.DEBUG);
|
|
|
|
assertEqual(logger.levelName, "DEBUG");
|
|
|
|
assertEqual(logger.handlers, []);
|
|
|
|
|
|
|
|
logger = new Logger("DEBUG", [handler]);
|
|
|
|
|
|
|
|
assertEqual(logger.handlers, [handler]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function customHandler() {
|
|
|
|
const handler = new TestHandler("DEBUG");
|
|
|
|
const logger = new Logger("DEBUG", [handler]);
|
|
|
|
|
|
|
|
logger.debug("foo", 1, 2);
|
|
|
|
|
|
|
|
assertEqual(handler.records, [
|
|
|
|
{
|
|
|
|
msg: "foo",
|
|
|
|
args: [1, 2],
|
|
|
|
datetime: null,
|
|
|
|
level: LogLevel.DEBUG,
|
|
|
|
levelName: "DEBUG"
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
assertEqual(handler.messages, ["DEBUG foo"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function logFunctions() {
|
|
|
|
let handler: TestHandler;
|
|
|
|
|
|
|
|
const doLog = (level: string) => {
|
|
|
|
handler = new TestHandler(level);
|
|
|
|
let logger = new Logger(level, [handler]);
|
|
|
|
logger.debug("foo");
|
|
|
|
logger.info("bar");
|
|
|
|
logger.warning("baz");
|
|
|
|
logger.error("boo");
|
|
|
|
logger.critical("doo");
|
|
|
|
};
|
|
|
|
|
|
|
|
doLog("DEBUG");
|
|
|
|
|
|
|
|
assertEqual(handler.messages, [
|
|
|
|
"DEBUG foo",
|
|
|
|
"INFO bar",
|
|
|
|
"WARNING baz",
|
|
|
|
"ERROR boo",
|
|
|
|
"CRITICAL doo"
|
|
|
|
]);
|
|
|
|
|
|
|
|
doLog("INFO");
|
|
|
|
|
|
|
|
assertEqual(handler.messages, [
|
|
|
|
"INFO bar",
|
|
|
|
"WARNING baz",
|
|
|
|
"ERROR boo",
|
|
|
|
"CRITICAL doo"
|
|
|
|
]);
|
|
|
|
|
|
|
|
doLog("WARNING");
|
|
|
|
|
2019-01-28 11:17:00 -05:00
|
|
|
assertEqual(handler.messages, ["WARNING baz", "ERROR boo", "CRITICAL doo"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
|
|
|
doLog("ERROR");
|
|
|
|
|
2019-01-28 11:17:00 -05:00
|
|
|
assertEqual(handler.messages, ["ERROR boo", "CRITICAL doo"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
|
|
|
doLog("CRITICAL");
|
|
|
|
|
2019-01-28 11:17:00 -05:00
|
|
|
assertEqual(handler.messages, ["CRITICAL doo"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
});
|