2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-04-25 05:13:26 -04:00
|
|
|
import {
|
|
|
|
LogLevels,
|
|
|
|
getLevelByName,
|
|
|
|
getLevelName,
|
|
|
|
LevelName,
|
|
|
|
} from "./levels.ts";
|
2019-01-02 09:12:48 -05:00
|
|
|
import { BaseHandler } from "./handlers.ts";
|
|
|
|
|
2020-04-09 07:45:24 -04:00
|
|
|
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());
|
|
|
|
}
|
2019-01-06 14:19:15 -05:00
|
|
|
}
|
2018-12-19 13:16:45 -05:00
|
|
|
|
|
|
|
export class Logger {
|
|
|
|
level: number;
|
2020-04-25 05:13:26 -04:00
|
|
|
levelName: LevelName;
|
2019-03-04 19:53:35 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2018-12-19 13:16:45 -05:00
|
|
|
handlers: any[];
|
|
|
|
|
2020-04-25 05:13:26 -04:00
|
|
|
constructor(levelName: LevelName, handlers?: BaseHandler[]) {
|
2018-12-19 13:16:45 -05:00
|
|
|
this.level = getLevelByName(levelName);
|
|
|
|
this.levelName = levelName;
|
2019-01-06 14:19:15 -05:00
|
|
|
|
2019-01-02 09:12:48 -05:00
|
|
|
this.handlers = handlers || [];
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
|
2019-06-06 12:56:33 -04:00
|
|
|
_log(level: number, msg: string, ...args: unknown[]): void {
|
2019-01-02 09:12:48 -05:00
|
|
|
if (this.level > level) return;
|
|
|
|
|
2020-04-09 07:45:24 -04:00
|
|
|
const record: LogRecord = new LogRecord(msg, args, level);
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
this.handlers.forEach((handler): void => {
|
|
|
|
handler.handle(record);
|
|
|
|
});
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
|
2019-06-06 12:56:33 -04:00
|
|
|
debug(msg: string, ...args: unknown[]): void {
|
2020-04-25 05:13:26 -04:00
|
|
|
this._log(LogLevels.DEBUG, msg, ...args);
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
|
2019-06-06 12:56:33 -04:00
|
|
|
info(msg: string, ...args: unknown[]): void {
|
2020-04-25 05:13:26 -04:00
|
|
|
this._log(LogLevels.INFO, msg, ...args);
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
|
2019-06-06 12:56:33 -04:00
|
|
|
warning(msg: string, ...args: unknown[]): void {
|
2020-04-25 05:13:26 -04:00
|
|
|
this._log(LogLevels.WARNING, msg, ...args);
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
|
2019-06-06 12:56:33 -04:00
|
|
|
error(msg: string, ...args: unknown[]): void {
|
2020-04-25 05:13:26 -04:00
|
|
|
this._log(LogLevels.ERROR, msg, ...args);
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
|
2019-06-06 12:56:33 -04:00
|
|
|
critical(msg: string, ...args: unknown[]): void {
|
2020-04-25 05:13:26 -04:00
|
|
|
this._log(LogLevels.CRITICAL, msg, ...args);
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
}
|