mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
// 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 "@std/fmt/colors";
|
|
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;
|
|
}
|
|
|
|
log(msg: string) {
|
|
// deno-lint-ignore no-console
|
|
console.log(msg);
|
|
}
|
|
}
|