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

console_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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.import { type LevelName, LogLevels } from "./levels.ts";import type { LogRecord } from "./logger.ts";import { blue, bold, red, yellow } from "../fmt/colors.ts";import { BaseHandler, type BaseHandlerOptions } from "./base_handler.ts";
export interface ConsoleHandlerOptions extends BaseHandlerOptions { useColors?: boolean;}
/** * This is the default logger. It will output color coded log messages to the * console via `console.log()`. */export class ConsoleHandler extends BaseHandler { #useColors?: boolean;
constructor(levelName: LevelName, options: ConsoleHandlerOptions = {}) { super(levelName, options); this.#useColors = options.useColors ?? true; }
override format(logRecord: LogRecord): string { let msg = super.format(logRecord);
if (this.#useColors) { msg = this.applyColors(msg, logRecord.level); }
return msg; }
applyColors(msg: string, level: number): string { switch (level) { case LogLevels.INFO: msg = blue(msg); break; case LogLevels.WARN: msg = yellow(msg); break; case LogLevels.ERROR: msg = red(msg); break; case LogLevels.CRITICAL: msg = bold(red(msg)); break; default: break; }
return msg; }
override log(msg: string) { console.log(msg); }}
std

Version Info

Tagged at
6 months ago