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

base_handler.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.import { getLevelByName, getLevelName, type LevelName, type LogLevel,} from "./levels.ts";import type { LogRecord } from "./logger.ts";
export type FormatterFunction = (logRecord: LogRecord) => string;const DEFAULT_FORMATTER: FormatterFunction = ({ levelName, msg }) => `${levelName} ${msg}`;
export interface BaseHandlerOptions { formatter?: FormatterFunction;}
export class BaseHandler { #levelName: LevelName; #level: LogLevel; formatter: FormatterFunction;
constructor( levelName: LevelName, { formatter = DEFAULT_FORMATTER }: BaseHandlerOptions = {}, ) { this.#levelName = levelName; this.#level = getLevelByName(levelName); this.formatter = formatter; }
get level(): LogLevel { return this.#level; }
set level(level: LogLevel) { this.#level = level; this.#levelName = getLevelName(level); }
get levelName(): LevelName { return this.#levelName; } set levelName(levelName: LevelName) { this.#levelName = levelName; this.#level = getLevelByName(levelName); }
handle(logRecord: LogRecord) { if (this.level > logRecord.level) return;
const msg = this.format(logRecord); this.log(msg); }
format(logRecord: LogRecord): string { return this.formatter(logRecord); }
log(_msg: string) {} setup() {} destroy() {}
[Symbol.dispose]() { this.destroy(); }}
std

Version Info

Tagged at
6 months ago