2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 16:39:50 -05:00
|
|
|
import { test } from "../testing/mod.ts";
|
2019-03-06 19:42:24 -05:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2019-01-27 10:21:00 -05:00
|
|
|
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-05-30 08:59:30 -04:00
|
|
|
this.records.push({ ...record });
|
2019-01-27 10:21:00 -05:00
|
|
|
super.handle(record);
|
|
|
|
}
|
|
|
|
|
|
|
|
public log(str: string): void {
|
|
|
|
this.messages.push(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function simpleLogger(): void {
|
2019-01-27 10:21:00 -05:00
|
|
|
const handler = new TestHandler("DEBUG");
|
|
|
|
let logger = new Logger("DEBUG");
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(logger.level, LogLevel.DEBUG);
|
|
|
|
assertEquals(logger.levelName, "DEBUG");
|
|
|
|
assertEquals(logger.handlers, []);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
|
|
|
logger = new Logger("DEBUG", [handler]);
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(logger.handlers, [handler]);
|
2019-01-27 10:21:00 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function customHandler(): void {
|
2019-01-27 10:21:00 -05:00
|
|
|
const handler = new TestHandler("DEBUG");
|
|
|
|
const logger = new Logger("DEBUG", [handler]);
|
|
|
|
|
|
|
|
logger.debug("foo", 1, 2);
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
const record = handler.records[0];
|
|
|
|
assertEquals(record.msg, "foo");
|
|
|
|
assertEquals(record.args, [1, 2]);
|
|
|
|
assertEquals(record.level, LogLevel.DEBUG);
|
|
|
|
assertEquals(record.levelName, "DEBUG");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, ["DEBUG foo"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function logFunctions(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const doLog = (level: string): TestHandler => {
|
|
|
|
const handler = new TestHandler(level);
|
2019-01-27 10:21:00 -05:00
|
|
|
let logger = new Logger(level, [handler]);
|
|
|
|
logger.debug("foo");
|
|
|
|
logger.info("bar");
|
|
|
|
logger.warning("baz");
|
|
|
|
logger.error("boo");
|
|
|
|
logger.critical("doo");
|
2019-05-30 08:59:30 -04:00
|
|
|
return handler;
|
2019-01-27 10:21:00 -05:00
|
|
|
};
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
let handler: TestHandler;
|
|
|
|
handler = doLog("DEBUG");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, [
|
2019-01-27 10:21:00 -05:00
|
|
|
"DEBUG foo",
|
|
|
|
"INFO bar",
|
|
|
|
"WARNING baz",
|
|
|
|
"ERROR boo",
|
|
|
|
"CRITICAL doo"
|
|
|
|
]);
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
handler = doLog("INFO");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, [
|
2019-01-27 10:21:00 -05:00
|
|
|
"INFO bar",
|
|
|
|
"WARNING baz",
|
|
|
|
"ERROR boo",
|
|
|
|
"CRITICAL doo"
|
|
|
|
]);
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
handler = doLog("WARNING");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, ["WARNING baz", "ERROR boo", "CRITICAL doo"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
handler = doLog("ERROR");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, ["ERROR boo", "CRITICAL doo"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
handler = doLog("CRITICAL");
|
2019-01-27 10:21:00 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, ["CRITICAL doo"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
});
|