deno.land / std@0.166.0 / log / test.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertThrows } from "../testing/asserts.ts";import * as log from "./mod.ts";import { getLevelByName, getLevelName, LevelName, LogLevelNames,} from "./levels.ts";
class TestHandler extends log.handlers.BaseHandler { public messages: string[] = [];
override log(msg: string) { this.messages.push(msg); }}
Deno.test("defaultHandlers", async function () { const loggers: { [key: string]: (msg: string, ...args: unknown[]) => void; } = { DEBUG: log.debug, INFO: log.info, WARNING: log.warning, ERROR: log.error, CRITICAL: log.critical, };
for (const levelName of LogLevelNames) { if (levelName === "NOTSET") { continue; }
const logger = loggers[levelName]; const handler = new TestHandler(levelName as LevelName);
await log.setup({ handlers: { default: handler, }, loggers: { default: { level: levelName as LevelName, handlers: ["default"], }, }, });
logger("foo"); logger("bar", 1, 2);
assertEquals(handler.messages, [`${levelName} foo`, `${levelName} bar`]); }});
Deno.test("getLogger", async function () { const handler = new TestHandler("DEBUG");
await log.setup({ handlers: { default: handler, }, loggers: { default: { level: "DEBUG", handlers: ["default"], }, }, });
const logger = log.getLogger();
assertEquals(logger.levelName, "DEBUG"); assertEquals(logger.handlers, [handler]);});
Deno.test("getLoggerWithName", async function () { const fooHandler = new TestHandler("DEBUG");
await log.setup({ handlers: { foo: fooHandler, }, loggers: { bar: { level: "INFO", handlers: ["foo"], }, }, });
const logger = log.getLogger("bar");
assertEquals(logger.levelName, "INFO"); assertEquals(logger.handlers, [fooHandler]);});
Deno.test("getLoggerUnknown", async function () { await log.setup({ handlers: {}, loggers: {}, });
const logger = log.getLogger("nonexistent");
assertEquals(logger.levelName, "NOTSET"); assertEquals(logger.handlers, []);});
Deno.test("getInvalidLoggerLevels", function () { assertThrows(() => getLevelByName("FAKE_LOG_LEVEL" as LevelName)); assertThrows(() => getLevelName(5000));});
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2448

Version Info

Tagged at
2 months ago