1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-29 02:29:06 -05:00
denoland-deno/std/log/logger.ts
2020-04-25 11:13:26 +02:00

74 lines
1.8 KiB
TypeScript

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
LogLevels,
getLevelByName,
getLevelName,
LevelName,
} from "./levels.ts";
import { BaseHandler } from "./handlers.ts";
export class LogRecord {
readonly msg: string;
#args: unknown[];
#datetime: Date;
readonly level: number;
readonly levelName: string;
constructor(msg: string, args: unknown[], level: number) {
this.msg = msg;
this.#args = [...args];
this.level = level;
this.#datetime = new Date();
this.levelName = getLevelName(level);
}
get args(): unknown[] {
return [...this.#args];
}
get datetime(): Date {
return new Date(this.#datetime.getTime());
}
}
export class Logger {
level: number;
levelName: LevelName;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handlers: any[];
constructor(levelName: LevelName, handlers?: BaseHandler[]) {
this.level = getLevelByName(levelName);
this.levelName = levelName;
this.handlers = handlers || [];
}
_log(level: number, msg: string, ...args: unknown[]): void {
if (this.level > level) return;
const record: LogRecord = new LogRecord(msg, args, level);
this.handlers.forEach((handler): void => {
handler.handle(record);
});
}
debug(msg: string, ...args: unknown[]): void {
this._log(LogLevels.DEBUG, msg, ...args);
}
info(msg: string, ...args: unknown[]): void {
this._log(LogLevels.INFO, msg, ...args);
}
warning(msg: string, ...args: unknown[]): void {
this._log(LogLevels.WARNING, msg, ...args);
}
error(msg: string, ...args: unknown[]): void {
this._log(LogLevels.ERROR, msg, ...args);
}
critical(msg: string, ...args: unknown[]): void {
this._log(LogLevels.CRITICAL, msg, ...args);
}
}