2024-01-01 21:11:32 +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";
|
|
|
|
|
|
|
|
Deno.test("setup() handles default handlers", async function () {
|
|
|
|
const loggers: {
|
|
|
|
[key: string]: (msg: string, ...args: unknown[]) => void;
|
|
|
|
} = {
|
|
|
|
DEBUG: log.debug,
|
|
|
|
INFO: log.info,
|
|
|
|
WARN: log.warn,
|
|
|
|
ERROR: log.error,
|
|
|
|
CRITICAL: log.critical,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const levelName of log.LogLevelNames) {
|
|
|
|
if (levelName === "NOTSET") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const logger = loggers[levelName]!;
|
|
|
|
const handler = new TestHandler(levelName);
|
|
|
|
|
|
|
|
await log.setup({
|
|
|
|
handlers: {
|
|
|
|
default: handler,
|
|
|
|
},
|
|
|
|
loggers: {
|
|
|
|
default: {
|
|
|
|
level: levelName as log.LevelName,
|
|
|
|
handlers: ["default"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
logger("foo");
|
|
|
|
logger("bar", 1, 2);
|
2020-06-12 13:27:41 +00:00
|
|
|
|
2024-04-24 08:02:32 +00:00
|
|
|
assertEquals(handler.messages, [`${levelName} foo`, `${levelName} bar`]);
|
|
|
|
}
|
2020-06-12 13:27:41 +00:00
|
|
|
});
|
2020-06-18 10:50:18 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2024-02-27 20:06:29 +00:00
|
|
|
name: "setup() logging config works as expected with logger names",
|
2020-06-18 10:50:18 +00:00
|
|
|
async fn() {
|
|
|
|
const consoleHandler = new TestHandler("DEBUG");
|
|
|
|
const anotherConsoleHandler = new TestHandler("DEBUG", {
|
2024-01-25 08:03:19 +00:00
|
|
|
formatter: ({ loggerName, levelName, msg }) =>
|
|
|
|
`[${loggerName}] ${levelName} ${msg}`,
|
2020-06-18 10:50:18 +00:00
|
|
|
});
|
2024-04-24 08:02:32 +00:00
|
|
|
await log.setup({
|
2020-06-18 10:50:18 +00:00
|
|
|
handlers: {
|
|
|
|
console: consoleHandler,
|
|
|
|
anotherConsole: anotherConsoleHandler,
|
|
|
|
},
|
|
|
|
|
|
|
|
loggers: {
|
|
|
|
// configure default logger available via short-hand methods above
|
|
|
|
default: {
|
|
|
|
level: "DEBUG",
|
|
|
|
handlers: ["console"],
|
|
|
|
},
|
|
|
|
|
|
|
|
tasks: {
|
|
|
|
level: "ERROR",
|
|
|
|
handlers: ["anotherConsole"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-04-24 08:02:32 +00:00
|
|
|
log.getLogger().debug("hello");
|
|
|
|
log.getLogger("tasks").error("world");
|
2020-06-18 10:50:18 +00:00
|
|
|
assertEquals(consoleHandler.messages[0], "DEBUG hello");
|
|
|
|
assertEquals(anotherConsoleHandler.messages[0], "[tasks] ERROR world");
|
|
|
|
},
|
|
|
|
});
|
2020-07-06 03:57:31 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2024-02-27 20:06:29 +00:00
|
|
|
name: "setup() loggers have level and levelName to get and set loglevels",
|
2020-07-06 03:57:31 +00:00
|
|
|
async fn() {
|
|
|
|
const testHandler = new TestHandler("DEBUG");
|
2024-04-24 08:02:32 +00:00
|
|
|
await log.setup({
|
2020-07-06 03:57:31 +00:00
|
|
|
handlers: {
|
|
|
|
test: testHandler,
|
|
|
|
},
|
|
|
|
|
|
|
|
loggers: {
|
|
|
|
// configure default logger available via short-hand methods above
|
|
|
|
default: {
|
|
|
|
level: "DEBUG",
|
|
|
|
handlers: ["test"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-04-24 08:02:32 +00:00
|
|
|
const logger: log.Logger = log.getLogger();
|
2020-07-06 03:57:31 +00:00
|
|
|
assertEquals(logger.levelName, "DEBUG");
|
2024-04-24 08:02:32 +00:00
|
|
|
assertEquals(logger.level, log.LogLevels.DEBUG);
|
2020-07-06 03:57:31 +00:00
|
|
|
|
|
|
|
logger.debug("debug");
|
|
|
|
logger.error("error");
|
|
|
|
logger.critical("critical");
|
|
|
|
assertEquals(testHandler.messages.length, 3);
|
|
|
|
assertEquals(testHandler.messages[0], "DEBUG debug");
|
|
|
|
assertEquals(testHandler.messages[1], "ERROR error");
|
|
|
|
assertEquals(testHandler.messages[2], "CRITICAL critical");
|
|
|
|
|
|
|
|
testHandler.messages = [];
|
2024-04-24 08:02:32 +00:00
|
|
|
logger.level = log.LogLevels.WARN;
|
2024-02-02 05:57:14 +00:00
|
|
|
assertEquals(logger.levelName, "WARN");
|
2024-04-24 08:02:32 +00:00
|
|
|
assertEquals(logger.level, log.LogLevels.WARN);
|
2020-07-06 03:57:31 +00:00
|
|
|
|
|
|
|
logger.debug("debug2");
|
|
|
|
logger.error("error2");
|
|
|
|
logger.critical("critical2");
|
|
|
|
assertEquals(testHandler.messages.length, 2);
|
|
|
|
assertEquals(testHandler.messages[0], "ERROR error2");
|
|
|
|
assertEquals(testHandler.messages[1], "CRITICAL critical2");
|
|
|
|
|
|
|
|
testHandler.messages = [];
|
2024-04-24 08:02:32 +00:00
|
|
|
const setLevelName: log.LevelName = "CRITICAL";
|
2020-07-06 03:57:31 +00:00
|
|
|
logger.levelName = setLevelName;
|
|
|
|
assertEquals(logger.levelName, "CRITICAL");
|
2024-04-24 08:02:32 +00:00
|
|
|
assertEquals(logger.level, log.LogLevels.CRITICAL);
|
2020-07-06 03:57:31 +00:00
|
|
|
|
|
|
|
logger.debug("debug3");
|
|
|
|
logger.error("error3");
|
|
|
|
logger.critical("critical3");
|
|
|
|
assertEquals(testHandler.messages.length, 1);
|
|
|
|
assertEquals(testHandler.messages[0], "CRITICAL critical3");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-27 20:06:29 +00:00
|
|
|
name: "setup() checks if logger has mutable handlers",
|
2020-07-06 03:57:31 +00:00
|
|
|
async fn() {
|
|
|
|
const testHandlerA = new TestHandler("DEBUG");
|
|
|
|
const testHandlerB = new TestHandler("DEBUG");
|
2024-04-24 08:02:32 +00:00
|
|
|
await log.setup({
|
2020-07-06 03:57:31 +00:00
|
|
|
handlers: {
|
|
|
|
testA: testHandlerA,
|
|
|
|
testB: testHandlerB,
|
|
|
|
},
|
|
|
|
|
|
|
|
loggers: {
|
|
|
|
default: {
|
|
|
|
level: "DEBUG",
|
|
|
|
handlers: ["testA"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-04-24 08:02:32 +00:00
|
|
|
const logger: log.Logger = log.getLogger();
|
2020-07-06 03:57:31 +00:00
|
|
|
logger.info("msg1");
|
|
|
|
assertEquals(testHandlerA.messages.length, 1);
|
|
|
|
assertEquals(testHandlerA.messages[0], "INFO msg1");
|
|
|
|
assertEquals(testHandlerB.messages.length, 0);
|
|
|
|
|
|
|
|
logger.handlers = [testHandlerA, testHandlerB];
|
|
|
|
|
|
|
|
logger.info("msg2");
|
|
|
|
assertEquals(testHandlerA.messages.length, 2);
|
|
|
|
assertEquals(testHandlerA.messages[1], "INFO msg2");
|
|
|
|
assertEquals(testHandlerB.messages.length, 1);
|
|
|
|
assertEquals(testHandlerB.messages[0], "INFO msg2");
|
|
|
|
|
|
|
|
logger.handlers = [testHandlerB];
|
|
|
|
|
|
|
|
logger.info("msg3");
|
|
|
|
assertEquals(testHandlerA.messages.length, 2);
|
|
|
|
assertEquals(testHandlerB.messages.length, 2);
|
|
|
|
assertEquals(testHandlerB.messages[1], "INFO msg3");
|
|
|
|
|
|
|
|
logger.handlers = [];
|
|
|
|
logger.info("msg4");
|
|
|
|
assertEquals(testHandlerA.messages.length, 2);
|
|
|
|
assertEquals(testHandlerB.messages.length, 2);
|
|
|
|
},
|
|
|
|
});
|
2024-04-24 08:02:32 +00:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "setup() checks loggerName of loggers",
|
|
|
|
async fn() {
|
|
|
|
const testHandler = new TestHandler("DEBUG");
|
|
|
|
await log.setup({
|
|
|
|
handlers: {
|
|
|
|
test: testHandler,
|
|
|
|
},
|
|
|
|
|
|
|
|
loggers: {
|
|
|
|
namedA: {
|
|
|
|
level: "DEBUG",
|
|
|
|
handlers: ["test"],
|
|
|
|
},
|
|
|
|
namedB: {
|
|
|
|
level: "DEBUG",
|
|
|
|
handlers: ["test"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(log.getLogger("namedA").loggerName, "namedA");
|
|
|
|
assertEquals(log.getLogger("namedB").loggerName, "namedB");
|
|
|
|
assertEquals(log.getLogger().loggerName, "default");
|
|
|
|
assertEquals(log.getLogger("nonsetupname").loggerName, "nonsetupname");
|
|
|
|
},
|
|
|
|
});
|