All files / log / console_handler.ts

100.00% Branches 7/7
100.00% Lines 39/39
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
116
117
118
 
 
x38
 
x38
x38
 
 
 
 
 
 
 
 
 
 
 
x74
x74
x74
x85
x85
x74
x80
x80
x74
x81
x81
x74
x80
x80
x74
x80
x74
 
x74
x74
 
 
 
 
 
 
 
 
 
 
 
 
 
x38
x38
 
 
 
 
 
 
 
x38
x89
x89
x89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x38
x78
 
x78
x114
x114
 
x78
x78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x38
 
x79
x79
x38




















































































































// Copyright 2018-2025 the Deno authors. 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 "@std/fmt/colors";
import { BaseHandler, type BaseHandlerOptions } from "./base_handler.ts";

/** Options for {@linkcode ConsoleHandler}. */
export interface ConsoleHandlerOptions extends BaseHandlerOptions {
  /**
   * Whether to use colors in the output.
   *
   * @default {true}
   */
  useColors?: boolean;
}

function 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;
}

/**
 * Default logger that outputs log messages to the console via
 * {@linkcode console.log}.
 *
 * @example Usage
 * ```ts no-assert
 * import { ConsoleHandler } from "@std/log/console-handler";
 *
 * const handler = new ConsoleHandler("INFO");
 * handler.log("Hello, world!"); // Prints "Hello, world!"
 * ```
 */
export class ConsoleHandler extends BaseHandler {
  #useColors?: boolean;

  /**
   * Constructs a new instance.
   *
   * @param levelName The level name to log messages at.
   * @param options Options for the handler.
   */
  constructor(levelName: LevelName, options: ConsoleHandlerOptions = {}) {
    super(levelName, options);
    this.#useColors = options.useColors ?? true;
  }

  /**
   * Formats a log record into a string.
   *
   * @example Usage
   * ```ts
   * import { ConsoleHandler } from "@std/log/console-handler";
   * import { LogRecord } from "@std/log/logger";
   * import { LogLevels } from "@std/log/levels";
   * import { assertEquals } from "@std/assert/equals";
   * import { blue } from "@std/fmt/colors";
   *
   * const handler = new ConsoleHandler("INFO");
   * const logRecord = new LogRecord({
   *   msg: "Hello, world!",
   *   args: ["foo", "bar"],
   *   level: LogLevels.INFO,
   *   loggerName: "my-logger",
   * });
   * const result = handler.format(logRecord);
   *
   * assertEquals(result, blue("INFO Hello, world!"));
   * ```
   *
   * @param logRecord The log record to format.
   * @returns The formatted log record.
   */
  override format(logRecord: LogRecord): string {
    let msg = super.format(logRecord);

    if (this.#useColors) {
      msg = applyColors(msg, logRecord.level);
    }

    return msg;
  }

  /**
   * Logs a message to the console.
   *
   * @example Usage
   * ```ts no-assert
   * import { ConsoleHandler } from "@std/log/console-handler";
   *
   * const handler = new ConsoleHandler("INFO");
   * handler.log("Hello, world!"); // Prints "Hello, world!"
   * ```
   *
   * @param msg The message to log.
   */
  log(msg: string) {
    // deno-lint-ignore no-console
    console.log(msg);
  }
}