deno.land / std@0.224.0 / log / base_handler.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869// Copyright 2018-2024 the Deno authors. All rights reserved. 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 FormatterFunction = (logRecord: LogRecord) => string;const DEFAULT_FORMATTER: FormatterFunction = ({ levelName, msg }) => `${levelName} ${msg}`;
export interface BaseHandlerOptions { formatter?: FormatterFunction;}
export class BaseHandler { #levelName: LevelName; #level: LogLevel; formatter: FormatterFunction;
constructor( levelName: LevelName, { formatter = DEFAULT_FORMATTER }: BaseHandlerOptions = {}, ) { this.#levelName = levelName; this.#level = getLevelByName(levelName); this.formatter = formatter; }
get level(): LogLevel { return this.#level; }
set level(level: LogLevel) { this.#level = level; this.#levelName = getLevelName(level); }
get levelName(): LevelName { return this.#levelName; } set levelName(levelName: LevelName) { this.#levelName = levelName; this.#level = getLevelByName(levelName); }
handle(logRecord: LogRecord) { if (this.level > logRecord.level) return;
const msg = this.format(logRecord); this.log(msg); }
format(logRecord: LogRecord): string { return this.formatter(logRecord); }
log(_msg: string) {} setup() {} destroy() {}
[Symbol.dispose]() { this.destroy(); }}
Version Info