deno.land / std@0.224.0 / log / setup.ts

View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
import type { BaseHandler } from "./base_handler.ts";import { DEFAULT_CONFIG, DEFAULT_LEVEL } from "./_config.ts";import { type LogConfig, Logger } from "./logger.ts";import { state } from "./_state.ts";
/** Setup logger config. */export function setup(config: LogConfig) { state.config = { handlers: { ...DEFAULT_CONFIG.handlers, ...config.handlers }, loggers: { ...DEFAULT_CONFIG.loggers, ...config.loggers }, };
// tear down existing handlers state.handlers.forEach((handler) => { handler.destroy(); }); state.handlers.clear();
// setup handlers const handlers = state.config.handlers || {};
for (const [handlerName, handler] of Object.entries(handlers)) { handler.setup(); state.handlers.set(handlerName, handler); }
// remove existing loggers state.loggers.clear();
// setup loggers const loggers = state.config.loggers || {}; for (const [loggerName, loggerConfig] of Object.entries(loggers)) { const handlerNames = loggerConfig.handlers || []; const handlers: BaseHandler[] = [];
handlerNames.forEach((handlerName) => { const handler = state.handlers.get(handlerName); if (handler) { handlers.push(handler); } });
const levelName = loggerConfig.level || DEFAULT_LEVEL; const logger = new Logger(loggerName, levelName, { handlers: handlers }); state.loggers.set(loggerName, logger); }}
setup(DEFAULT_CONFIG);
std

Version Info

Tagged at
6 months ago