mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
70 lines
1.5 KiB
TypeScript
70 lines
1.5 KiB
TypeScript
// 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,
|
|
options?: BaseHandlerOptions,
|
|
) {
|
|
const { formatter = DEFAULT_FORMATTER } = options ?? {};
|
|
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();
|
|
}
|
|
}
|