2024-01-12 08:17:25 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2024-02-27 21:57:25 +00:00
|
|
|
import { type LevelName, LogLevels } from "./levels.ts";
|
2024-01-12 08:17:25 +00:00
|
|
|
import type { LogRecord } from "./logger.ts";
|
2024-04-29 02:57:30 +00:00
|
|
|
import { blue, bold, red, yellow } from "@std/fmt/colors";
|
2024-01-12 08:17:25 +00:00
|
|
|
import { BaseHandler, type BaseHandlerOptions } from "./base_handler.ts";
|
|
|
|
|
2024-10-02 07:46:14 +00:00
|
|
|
/** Options for {@linkcode ConsoleHandler}. */
|
2024-01-12 08:17:25 +00:00
|
|
|
export interface ConsoleHandlerOptions extends BaseHandlerOptions {
|
2024-10-02 07:46:14 +00:00
|
|
|
/**
|
|
|
|
* Whether to use colors in the output.
|
|
|
|
*
|
|
|
|
* @default {true}
|
|
|
|
*/
|
2024-01-12 08:17:25 +00:00
|
|
|
useColors?: boolean;
|
|
|
|
}
|
|
|
|
|
2024-09-29 06:37:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-12 08:17:25 +00:00
|
|
|
/**
|
2024-10-02 07:46:14 +00:00
|
|
|
* 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!"
|
|
|
|
* ```
|
2024-01-12 08:17:25 +00:00
|
|
|
*/
|
|
|
|
export class ConsoleHandler extends BaseHandler {
|
|
|
|
#useColors?: boolean;
|
|
|
|
|
2024-10-02 07:46:14 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new instance.
|
|
|
|
*
|
|
|
|
* @param levelName The level name to log messages at.
|
|
|
|
* @param options Options for the handler.
|
|
|
|
*/
|
2024-01-12 08:17:25 +00:00
|
|
|
constructor(levelName: LevelName, options: ConsoleHandlerOptions = {}) {
|
|
|
|
super(levelName, options);
|
|
|
|
this.#useColors = options.useColors ?? true;
|
|
|
|
}
|
|
|
|
|
2024-10-02 07:46:14 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-01-12 08:17:25 +00:00
|
|
|
override format(logRecord: LogRecord): string {
|
|
|
|
let msg = super.format(logRecord);
|
|
|
|
|
|
|
|
if (this.#useColors) {
|
2024-09-29 06:37:19 +00:00
|
|
|
msg = applyColors(msg, logRecord.level);
|
2024-01-12 08:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2024-10-02 07:46:14 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-08-22 06:46:53 +00:00
|
|
|
log(msg: string) {
|
2024-09-13 05:43:13 +00:00
|
|
|
// deno-lint-ignore no-console
|
2024-01-12 08:17:25 +00:00
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
}
|