deno.land / std@0.224.0 / log / console_handler.ts
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859// 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); }}
Version Info