All files / log / base_handler.ts

100.00% Branches 3/3
100.00% Lines 48/48
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
 
 
x60
x60
x60
 
 
x60
 
 
 
 
 
 
 
 
 
 
 
 
x485
x485
x485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x60
x60
x178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x60
 
 
 
 
 
 
 
x60
x60
x60
x60
x178
x178
x178
x178
x178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x60
x515
x515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x60
x61
x61
x61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x60
x67
x67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x60
x61
x61
x61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x60
x508
 
x941
x941
x508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x60
x495
x495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x120
x66
x66
x60

























































































































































































































































































































































































// Copyright 2018-2025 the Deno authors. 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 { LevelName, LogLevel, LogRecord };

/**
 * A function type that defines the structure of a formatter function.
 *
 * @param logRecord The log record that needs to be formatted.
 * @returns A string representation of the log record.
 */
export type FormatterFunction = (logRecord: LogRecord) => string;
// deno-lint-ignore deno-style-guide/naming-convention
function DEFAULT_FORMATTER(logRecord: LogRecord): string {
  return `${logRecord.levelName} ${logRecord.msg}`;
}

/** Options for {@linkcode BaseHandler}. */
export interface BaseHandlerOptions {
  /** A function that formats log records. */
  formatter?: FormatterFunction;
}

/**
 * A base class for all log handlers.
 *
 * This class is abstract and should not be instantiated directly. Instead, it
 * should be extended by other classes that implement the `log` method.
 *
 * @example Usage
 * ```ts
 * import { BaseHandler } from "@std/log/base-handler";
 * import { assertInstanceOf } from "@std/assert/instance-of";
 *
 * class MyHandler extends BaseHandler {
 *   log(msg: string) {
 *     console.log(msg);
 *   }
 * }
 *
 * const handler = new MyHandler("INFO");
 * assertInstanceOf(handler, BaseHandler);
 * ```
 */
export abstract class BaseHandler {
  #levelName: LevelName;
  #level: LogLevel;
  /**
   * The function that formats log records.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { LogRecord } from "@std/log/logger";
   * import { LogLevels } from "@std/log/levels";
   * import { assertEquals } from "@std/assert/equals";
   *
   * class MyHandler extends BaseHandler {
   *  log(msg: string) {
   *   console.log(msg);
   * }
   * }
   *
   * const handler = new MyHandler("INFO");
   * const record = new LogRecord({
   *   msg: "Hello, world!",
   *   args: ["foo", "bar"],
   *   level: LogLevels.INFO,
   *   loggerName: "example",
   * });
   * const formatted = handler.formatter(record);
   * assertEquals(formatted, "INFO Hello, world!");
   * ```
   */
  formatter: FormatterFunction;

  /**
   * Constructs a new instance.
   *
   * @param levelName The name of the log level to handle.
   * @param options Options for the handler.
   */
  constructor(
    levelName: LevelName,
    options?: BaseHandlerOptions,
  ) {
    const { formatter = DEFAULT_FORMATTER } = options ?? {};
    this.#levelName = levelName;
    this.#level = getLevelByName(levelName);
    this.formatter = formatter;
  }

  /**
   * Getter for the log level that this handler will handle.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { LogLevels } from "@std/log/levels";
   * import { assertEquals } from "@std/assert/equals";
   *
   * class MyHandler extends BaseHandler {
   *   log(msg: string) {
   *     console.log(msg);
   *   }
   * }
   *
   * const handler = new MyHandler("INFO");
   * assertEquals(handler.level, LogLevels.INFO);
   * ```
   *
   * @returns The log level to handle.
   */
  get level(): LogLevel {
    return this.#level;
  }

  /**
   * Setter for the log level that this handler will handle.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { LogLevels } from "@std/log/levels";
   * import { assertEquals } from "@std/assert/equals";
   *
   * class MyHandler extends BaseHandler {
   *   log(msg: string) {
   *     console.log(msg);
   *   }
   * }
   *
   * const handler = new MyHandler("INFO");
   * handler.level = LogLevels.DEBUG;
   * assertEquals(handler.level, LogLevels.DEBUG);
   * ```
   *
   * @param level The log level to handle.
   */
  set level(level: LogLevel) {
    this.#level = level;
    this.#levelName = getLevelName(level);
  }

  /**
   * Getter for the name of the log level that this handler will handle.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { assertEquals } from "@std/assert/equals";
   *
   * class MyHandler extends BaseHandler {
   *   log(msg: string) {
   *     console.log(msg);
   *   }
   * }
   *
   * const handler = new MyHandler("INFO");
   * assertEquals(handler.levelName, "INFO");
   * ```
   *
   * @returns The name of the log level to handle.
   */
  get levelName(): LevelName {
    return this.#levelName;
  }

  /**
   * Setter for the name of the log level that this handler will handle.
   *
   * @param levelName The name of the log level to handle.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { assertEquals } from "@std/assert/equals";
   *
   * class MyHandler extends BaseHandler {
   *   log(msg: string) {
   *     console.log(msg);
   *   }
   * }
   *
   * const handler = new MyHandler("INFO");
   * handler.levelName = "DEBUG";
   * assertEquals(handler.levelName, "DEBUG");
   * ```
   */
  set levelName(levelName: LevelName) {
    this.#levelName = levelName;
    this.#level = getLevelByName(levelName);
  }

  /**
   * Handles a log record.
   *
   * @param logRecord The log record to handle.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { LogRecord } from "@std/log/logger";
   * import { LogLevels } from "@std/log/levels";
   * import { assertInstanceOf } from "@std/assert/instance-of";
   *
   * class MyHandler extends BaseHandler {
   *   log(msg: string) {
   *     console.log(msg);
   *   }
   * }
   *
   * const handler = new MyHandler("INFO");
   * const record = new LogRecord({
   *   msg: "Hello, world!",
   *   args: ["foo", "bar"],
   *   level: LogLevels.INFO,
   *   loggerName: "example",
   * });
   * handler.handle(record);
   *
   * assertInstanceOf(handler, BaseHandler);
   * ```
   */
  handle(logRecord: LogRecord) {
    if (this.level > logRecord.level) return;

    const msg = this.format(logRecord);
    this.log(msg);
  }

  /**
   * Formats a log record.
   *
   * @param logRecord The log record to format.
   * @returns A string representation of the log record.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { LogRecord } from "@std/log/logger";
   * import { LogLevels } from "@std/log/levels";
   * import { assertEquals } from "@std/assert/equals";
   *
   * class MyHandler extends BaseHandler {
   *   log(msg: string) {
   *     console.log(msg);
   *   }
   * }
   *
   * const handler = new MyHandler("INFO");
   * const record = new LogRecord({
   *   msg: "Hello, world!",
   *   args: ["foo", "bar"],
   *   level: LogLevels.INFO,
   *   loggerName: "example",
   * });
   * const formatted = handler.format(record);
   * assertEquals(formatted, "INFO Hello, world!");
   * ```
   */
  format(logRecord: LogRecord): string {
    return this.formatter(logRecord);
  }

  /**
   * Logs a message.
   *
   * This method should be implemented by subclasses to handle the log record.
   *
   * @param msg The message to log.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { assertInstanceOf } from "@std/assert/instance-of";
   *
   * class MyHandler extends BaseHandler {
   *   log(msg: string) {
   *     console.log(msg);
   *   }
   * }
   *
   * const handler = new MyHandler("INFO");
   * handler.log("Hello, world!"); // Prints "Hello, world!"
   *
   * assertInstanceOf(handler, BaseHandler);
   * ```
   */
  abstract log(msg: string): void;

  /**
   * Initializes the handler.
   *
   * This method is called when the handler is added to a logger. It can be
   * used to perform any setup that is required by the handler.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { assertInstanceOf } from "@std/assert/instance-of";
   *
   * class MyHandler extends BaseHandler {
   *   log(msg: string) {
   *     console.log(msg);
   *   }
   *
   *   override setup() {
   *     console.log("Handler setup!");
   *   }
   * }
   *
   * const handler = new MyHandler("INFO");
   * handler.setup(); // Prints "Handler setup!"
   *
   * assertInstanceOf(handler, BaseHandler);
   * ```
   */
  setup() {}

  /**
   * Destroys the handler, performing any cleanup that is required.
   *
   * This method is called when the handler is removed from a logger. It can be
   * used to perform any cleanup that is required by the handler.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { assertInstanceOf } from "@std/assert/instance-of";
   *
   * class MyHandler extends BaseHandler {
   *   log(msg: string) {
   *     console.log(msg);
   *   }
   *
   *   override destroy() {
   *     console.log("Handler destroyed!");
   *   }
   * }
   *
   * const handler = new MyHandler("INFO");
   * handler.destroy(); // Prints "Handler destroyed!"
   * assertInstanceOf(handler, BaseHandler);
   * ```
   */
  destroy() {}

  /**
   * Automatically disposes of the handler when instantiated with the `using`
   * keyword by calling the {@linkcode BaseHandler.destroy} method.
   *
   * @example Usage
   * ```ts
   * import { BaseHandler } from "@std/log/base-handler";
   * import { LogRecord } from "@std/log/logger";
   * import { assertInstanceOf } from "@std/assert/instance-of";
   *
   * class MyHandler extends BaseHandler {
   *   log(msg: string) {
   *     console.log(msg);
   *   }
   * }
   *
   * using handler = new MyHandler("INFO");
   * assertInstanceOf(handler, BaseHandler);
   * ```
   */
  [Symbol.dispose]() {
    this.destroy();
  }
}