1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 17:49:08 -05:00
denoland-deno/log/logger.ts

74 lines
2 KiB
TypeScript
Raw Normal View History

2019-02-07 11:45:47 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
2018-12-19 13:16:45 -05:00
import { LogLevel, getLevelByName, getLevelName } from "./levels.ts";
2019-01-02 09:12:48 -05:00
import { BaseHandler } from "./handlers.ts";
export interface LogRecord {
msg: string;
2019-03-04 19:53:35 -05:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2019-01-02 09:12:48 -05:00
args: any[];
datetime: Date;
level: number;
levelName: string;
2019-01-06 14:19:15 -05:00
}
2018-12-19 13:16:45 -05:00
export class Logger {
level: number;
levelName: string;
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[];
2019-01-02 09:12:48 -05:00
constructor(levelName: string, 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-03-04 19:53:35 -05:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_log(level: number, msg: string, ...args: any[]): void {
2019-01-02 09:12:48 -05:00
if (this.level > level) return;
2019-01-06 14:19:15 -05:00
// TODO: it'd be a good idea to make it immutable, so
2019-01-02 09:12:48 -05:00
// no handler mangles it by mistake
// TODO: iterpolate msg with values
const record: LogRecord = {
msg: msg,
args: args,
datetime: new Date(),
level: level,
2019-01-06 14:19:15 -05:00
levelName: getLevelName(level)
};
2019-01-02 09:12:48 -05:00
2019-04-24 07:41:23 -04:00
this.handlers.forEach(
(handler): void => {
handler.handle(record);
}
);
2018-12-19 13:16:45 -05:00
}
2019-03-04 19:53:35 -05:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
debug(msg: string, ...args: any[]): void {
this._log(LogLevel.DEBUG, msg, ...args);
2018-12-19 13:16:45 -05:00
}
2019-03-04 19:53:35 -05:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
info(msg: string, ...args: any[]): void {
this._log(LogLevel.INFO, msg, ...args);
2018-12-19 13:16:45 -05:00
}
2019-03-04 19:53:35 -05:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
warning(msg: string, ...args: any[]): void {
this._log(LogLevel.WARNING, msg, ...args);
2018-12-19 13:16:45 -05:00
}
2019-03-04 19:53:35 -05:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
error(msg: string, ...args: any[]): void {
this._log(LogLevel.ERROR, msg, ...args);
2018-12-19 13:16:45 -05:00
}
2019-03-04 19:53:35 -05:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
critical(msg: string, ...args: any[]): void {
this._log(LogLevel.CRITICAL, msg, ...args);
2018-12-19 13:16:45 -05:00
}
}