2024-01-25 07:22:35 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2024-04-24 08:02:32 +00:00
|
|
|
import * as log from "./mod.ts";
|
|
|
|
import { TestHandler } from "./_test_handler.ts";
|
2024-01-25 07:22:35 +00:00
|
|
|
|
2024-02-27 20:06:29 +00:00
|
|
|
Deno.test("BaseHandler handles default setup", function () {
|
2024-04-24 08:02:32 +00:00
|
|
|
const cases = new Map<log.LogLevel, string[]>([
|
2024-01-25 07:22:35 +00:00
|
|
|
[
|
2024-04-24 08:02:32 +00:00
|
|
|
log.LogLevels.DEBUG,
|
2024-01-25 07:22:35 +00:00
|
|
|
[
|
|
|
|
"DEBUG debug-test",
|
|
|
|
"INFO info-test",
|
2024-02-02 05:57:14 +00:00
|
|
|
"WARN warn-test",
|
2024-01-25 07:22:35 +00:00
|
|
|
"ERROR error-test",
|
|
|
|
"CRITICAL critical-test",
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
2024-04-24 08:02:32 +00:00
|
|
|
log.LogLevels.INFO,
|
2024-01-25 07:22:35 +00:00
|
|
|
[
|
|
|
|
"INFO info-test",
|
2024-02-02 05:57:14 +00:00
|
|
|
"WARN warn-test",
|
2024-01-25 07:22:35 +00:00
|
|
|
"ERROR error-test",
|
|
|
|
"CRITICAL critical-test",
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
2024-04-24 08:02:32 +00:00
|
|
|
log.LogLevels.WARN,
|
2024-02-02 05:57:14 +00:00
|
|
|
["WARN warn-test", "ERROR error-test", "CRITICAL critical-test"],
|
2024-01-25 07:22:35 +00:00
|
|
|
],
|
2024-04-24 08:02:32 +00:00
|
|
|
[log.LogLevels.ERROR, ["ERROR error-test", "CRITICAL critical-test"]],
|
|
|
|
[log.LogLevels.CRITICAL, ["CRITICAL critical-test"]],
|
2024-01-25 07:22:35 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
for (const [testCase, messages] of cases.entries()) {
|
2024-04-24 08:02:32 +00:00
|
|
|
const testLevel = log.getLevelName(testCase);
|
2024-01-25 07:22:35 +00:00
|
|
|
const handler = new TestHandler(testLevel);
|
|
|
|
|
2024-04-24 08:02:32 +00:00
|
|
|
for (const levelName of log.LogLevelNames) {
|
|
|
|
const level = log.getLevelByName(levelName);
|
2024-01-25 07:22:35 +00:00
|
|
|
handler.handle(
|
2024-04-24 08:02:32 +00:00
|
|
|
new log.LogRecord({
|
2024-01-25 07:22:35 +00:00
|
|
|
msg: `${levelName.toLowerCase()}-test`,
|
|
|
|
args: [],
|
|
|
|
level: level,
|
|
|
|
loggerName: "default",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(handler.level, testCase);
|
|
|
|
assertEquals(handler.levelName, testLevel);
|
|
|
|
assertEquals(handler.messages, messages);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-02-27 20:06:29 +00:00
|
|
|
Deno.test("BaseHandler handles formatter with empty msg", function () {
|
2024-01-25 07:22:35 +00:00
|
|
|
const handler = new TestHandler("DEBUG", {
|
2024-01-25 08:03:19 +00:00
|
|
|
formatter: ({ levelName, msg }) => `test ${levelName} ${msg}`,
|
2024-01-25 07:22:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
handler.handle(
|
2024-04-24 08:02:32 +00:00
|
|
|
new log.LogRecord({
|
2024-01-25 07:22:35 +00:00
|
|
|
msg: "",
|
|
|
|
args: [],
|
2024-04-24 08:02:32 +00:00
|
|
|
level: log.LogLevels.DEBUG,
|
2024-01-25 07:22:35 +00:00
|
|
|
loggerName: "default",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(handler.messages, ["test DEBUG "]);
|
|
|
|
});
|
|
|
|
|
2024-02-27 20:06:29 +00:00
|
|
|
Deno.test("BaseHandler handles formatter", function () {
|
2024-01-25 07:22:35 +00:00
|
|
|
const handler = new TestHandler("DEBUG", {
|
|
|
|
formatter: (logRecord): string =>
|
|
|
|
`fn formatter ${logRecord.levelName} ${logRecord.msg}`,
|
|
|
|
});
|
|
|
|
|
|
|
|
handler.handle(
|
2024-04-24 08:02:32 +00:00
|
|
|
new log.LogRecord({
|
2024-01-25 07:22:35 +00:00
|
|
|
msg: "Hello, world!",
|
|
|
|
args: [],
|
2024-04-24 08:02:32 +00:00
|
|
|
level: log.LogLevels.ERROR,
|
2024-01-25 07:22:35 +00:00
|
|
|
loggerName: "default",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(handler.messages, ["fn formatter ERROR Hello, world!"]);
|
|
|
|
});
|