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 { LogLevel, getLevelName, getLevelByName } from "./levels.ts";
|
|
|
|
import { BaseHandler } from "./handlers.ts";
|
|
|
|
|
|
|
|
class TestHandler extends BaseHandler {
|
|
|
|
public messages: string[] = [];
|
|
|
|
|
|
|
|
public log(str: string): void {
|
|
|
|
this.messages.push(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test(function simpleHandler() {
|
|
|
|
const cases = new Map<number, string[]>([
|
2019-01-28 11:17:00 -05:00
|
|
|
[
|
|
|
|
LogLevel.DEBUG,
|
|
|
|
[
|
|
|
|
"DEBUG debug-test",
|
|
|
|
"INFO info-test",
|
|
|
|
"WARNING warning-test",
|
|
|
|
"ERROR error-test",
|
|
|
|
"CRITICAL critical-test"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
LogLevel.INFO,
|
|
|
|
[
|
|
|
|
"INFO info-test",
|
|
|
|
"WARNING warning-test",
|
|
|
|
"ERROR error-test",
|
|
|
|
"CRITICAL critical-test"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
LogLevel.WARNING,
|
|
|
|
["WARNING warning-test", "ERROR error-test", "CRITICAL critical-test"]
|
|
|
|
],
|
|
|
|
[LogLevel.ERROR, ["ERROR error-test", "CRITICAL critical-test"]],
|
|
|
|
[LogLevel.CRITICAL, ["CRITICAL critical-test"]]
|
2019-01-27 10:21:00 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
for (const [testCase, messages] of cases.entries()) {
|
|
|
|
const testLevel = getLevelName(testCase);
|
|
|
|
const handler = new TestHandler(testLevel);
|
|
|
|
|
|
|
|
for (const levelName in LogLevel) {
|
|
|
|
const level = getLevelByName(levelName);
|
|
|
|
handler.handle({
|
|
|
|
msg: `${levelName.toLowerCase()}-test`,
|
|
|
|
args: [],
|
|
|
|
datetime: new Date(),
|
|
|
|
level: level,
|
|
|
|
levelName: levelName
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.level, testCase);
|
|
|
|
assertEquals(handler.levelName, testLevel);
|
|
|
|
assertEquals(handler.messages, messages);
|
2019-01-27 10:21:00 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function testFormatterAsString() {
|
|
|
|
const handler = new TestHandler("DEBUG", {
|
|
|
|
formatter: "test {levelName} {msg}"
|
|
|
|
});
|
|
|
|
|
|
|
|
handler.handle({
|
|
|
|
msg: "Hello, world!",
|
|
|
|
args: [],
|
|
|
|
datetime: new Date(),
|
|
|
|
level: LogLevel.DEBUG,
|
|
|
|
levelName: "DEBUG"
|
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, ["test DEBUG Hello, world!"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test(function testFormatterAsFunction() {
|
|
|
|
const handler = new TestHandler("DEBUG", {
|
|
|
|
formatter: logRecord =>
|
|
|
|
`fn formmatter ${logRecord.levelName} ${logRecord.msg}`
|
|
|
|
});
|
|
|
|
|
|
|
|
handler.handle({
|
|
|
|
msg: "Hello, world!",
|
|
|
|
args: [],
|
|
|
|
datetime: new Date(),
|
|
|
|
level: LogLevel.ERROR,
|
|
|
|
levelName: "ERROR"
|
|
|
|
});
|
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(handler.messages, ["fn formmatter ERROR Hello, world!"]);
|
2019-01-27 10:21:00 -05:00
|
|
|
});
|