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-09-27 03:31:58 +00:00
|
|
|
import {
|
|
|
|
getLevelByName,
|
|
|
|
getLevelName,
|
|
|
|
type LogLevel,
|
|
|
|
LogLevelNames,
|
|
|
|
LogLevels,
|
|
|
|
} from "./levels.ts";
|
|
|
|
import { LogRecord } from "./logger.ts";
|
2024-04-24 08:02:32 +00:00
|
|
|
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-09-27 03:31:58 +00:00
|
|
|
const cases = new Map<LogLevel, string[]>([
|
2024-01-25 07:22:35 +00:00
|
|
|
[
|
2024-09-27 03:31:58 +00:00
|
|
|
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-09-27 03:31:58 +00:00
|
|
|
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-09-27 03:31:58 +00:00
|
|
|
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-09-27 03:31:58 +00:00
|
|
|
[LogLevels.ERROR, ["ERROR error-test", "CRITICAL critical-test"]],
|
|
|
|
[LogLevels.CRITICAL, ["CRITICAL critical-test"]],
|
2024-01-25 07:22:35 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
for (const [testCase, messages] of cases.entries()) {
|
2024-09-27 03:31:58 +00:00
|
|
|
const testLevel = getLevelName(testCase);
|
2024-01-25 07:22:35 +00:00
|
|
|
const handler = new TestHandler(testLevel);
|
|
|
|
|
2024-09-27 03:31:58 +00:00
|
|
|
for (const levelName of LogLevelNames) {
|
|
|
|
const level = getLevelByName(levelName);
|
2024-01-25 07:22:35 +00:00
|
|
|
handler.handle(
|
2024-09-27 03:31:58 +00:00
|
|
|
new 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-09-27 03:31:58 +00:00
|
|
|
new LogRecord({
|
2024-01-25 07:22:35 +00:00
|
|
|
msg: "",
|
|
|
|
args: [],
|
2024-09-27 03:31:58 +00:00
|
|
|
level: 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-09-27 03:31:58 +00:00
|
|
|
new LogRecord({
|
2024-01-25 07:22:35 +00:00
|
|
|
msg: "Hello, world!",
|
|
|
|
args: [],
|
2024-09-27 03:31:58 +00:00
|
|
|
level: LogLevels.ERROR,
|
2024-01-25 07:22:35 +00:00
|
|
|
loggerName: "default",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(handler.messages, ["fn formatter ERROR Hello, world!"]);
|
|
|
|
});
|