2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-27 10:22:32 +00:00
|
|
|
import { getLevelByName, getLevelName, LogLevels } from "./levels.ts";
|
2023-11-27 19:26:21 +00:00
|
|
|
import type { LevelName, LogLevel } from "./levels.ts";
|
2024-01-17 02:55:06 +00:00
|
|
|
import type { BaseHandler } from "./base_handler.ts";
|
2019-01-02 14:12:48 +00:00
|
|
|
|
2020-11-03 15:19:29 +00:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-08-24 23:43:54 +00:00
|
|
|
export type GenericFunction = (...args: any[]) => any;
|
|
|
|
|
2020-06-18 10:50:18 +00:00
|
|
|
export interface LogRecordOptions {
|
|
|
|
msg: string;
|
|
|
|
args: unknown[];
|
2024-01-09 01:43:19 +00:00
|
|
|
level: LogLevel;
|
2020-06-18 10:50:18 +00:00
|
|
|
loggerName: string;
|
|
|
|
}
|
|
|
|
|
2024-01-17 02:55:06 +00:00
|
|
|
export class LoggerConfig {
|
|
|
|
level?: LevelName;
|
|
|
|
handlers?: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface LogConfig {
|
|
|
|
handlers?: {
|
|
|
|
[name: string]: BaseHandler;
|
|
|
|
};
|
|
|
|
loggers?: {
|
|
|
|
[name: string]: LoggerConfig;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
|
|
|
* An object that encapsulates provided message and arguments as well some
|
|
|
|
* metadata that can be later used when formatting a message.
|
|
|
|
*/
|
2020-04-09 11:45:24 +00:00
|
|
|
export class LogRecord {
|
|
|
|
readonly msg: string;
|
|
|
|
#args: unknown[];
|
|
|
|
#datetime: Date;
|
|
|
|
readonly level: number;
|
|
|
|
readonly levelName: string;
|
2020-06-18 10:50:18 +00:00
|
|
|
readonly loggerName: string;
|
2020-04-09 11:45:24 +00:00
|
|
|
|
2020-06-18 10:50:18 +00:00
|
|
|
constructor(options: LogRecordOptions) {
|
|
|
|
this.msg = options.msg;
|
|
|
|
this.#args = [...options.args];
|
|
|
|
this.level = options.level;
|
|
|
|
this.loggerName = options.loggerName;
|
2020-04-09 11:45:24 +00:00
|
|
|
this.#datetime = new Date();
|
2020-06-18 10:50:18 +00:00
|
|
|
this.levelName = getLevelName(options.level);
|
2020-04-09 11:45:24 +00:00
|
|
|
}
|
|
|
|
get args(): unknown[] {
|
|
|
|
return [...this.#args];
|
|
|
|
}
|
|
|
|
get datetime(): Date {
|
|
|
|
return new Date(this.#datetime.getTime());
|
|
|
|
}
|
2019-01-06 19:19:15 +00:00
|
|
|
}
|
2018-12-19 18:16:45 +00:00
|
|
|
|
2020-06-18 10:50:18 +00:00
|
|
|
export interface LoggerOptions {
|
|
|
|
handlers?: BaseHandler[];
|
|
|
|
}
|
|
|
|
|
2018-12-19 18:16:45 +00:00
|
|
|
export class Logger {
|
2023-11-27 19:26:21 +00:00
|
|
|
#level: LogLevel;
|
2024-01-04 22:57:26 +00:00
|
|
|
handlers: BaseHandler[];
|
2020-07-06 03:57:31 +00:00
|
|
|
readonly #loggerName: string;
|
2018-12-19 18:16:45 +00:00
|
|
|
|
2020-06-18 10:50:18 +00:00
|
|
|
constructor(
|
|
|
|
loggerName: string,
|
|
|
|
levelName: LevelName,
|
2020-07-14 19:24:17 +00:00
|
|
|
options: LoggerOptions = {},
|
2020-06-18 10:50:18 +00:00
|
|
|
) {
|
2020-07-06 03:57:31 +00:00
|
|
|
this.#loggerName = loggerName;
|
2024-01-09 01:43:19 +00:00
|
|
|
this.#level = getLevelByName(levelName);
|
2024-01-04 22:57:26 +00:00
|
|
|
this.handlers = options.handlers || [];
|
2020-07-06 03:57:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 01:43:19 +00:00
|
|
|
/** Use this to retrieve the current numeric log level. */
|
|
|
|
get level(): LogLevel {
|
2020-07-06 03:57:31 +00:00
|
|
|
return this.#level;
|
|
|
|
}
|
2023-11-27 19:26:21 +00:00
|
|
|
|
2024-01-09 01:43:19 +00:00
|
|
|
/** Use this to set the numeric log level. */
|
|
|
|
set level(level: LogLevel) {
|
2023-11-27 19:26:21 +00:00
|
|
|
try {
|
2024-01-09 01:43:19 +00:00
|
|
|
this.#level = getLevelByName(getLevelName(level));
|
2023-11-27 19:26:21 +00:00
|
|
|
} catch (_) {
|
|
|
|
throw new TypeError(`Invalid log level: ${level}`);
|
|
|
|
}
|
2020-07-06 03:57:31 +00:00
|
|
|
}
|
2019-01-06 19:19:15 +00:00
|
|
|
|
2020-07-06 03:57:31 +00:00
|
|
|
get levelName(): LevelName {
|
|
|
|
return getLevelName(this.#level);
|
|
|
|
}
|
|
|
|
set levelName(levelName: LevelName) {
|
2024-01-09 01:43:19 +00:00
|
|
|
this.#level = getLevelByName(levelName);
|
2020-07-06 03:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get loggerName(): string {
|
|
|
|
return this.#loggerName;
|
|
|
|
}
|
|
|
|
|
2023-11-27 19:26:21 +00:00
|
|
|
/**
|
|
|
|
* If the level of the logger is greater than the level to log, then nothing
|
2020-06-12 13:27:41 +00:00
|
|
|
* is logged, otherwise a log record is passed to each log handler. `msg` data
|
|
|
|
* passed in is returned. If a function is passed in, it is only evaluated
|
|
|
|
* if the msg will be logged and the return value will be the result of the
|
|
|
|
* function, not the function itself, unless the function isn't called, in which
|
|
|
|
* case undefined is returned. All types are coerced to strings for logging.
|
|
|
|
*/
|
2024-01-27 16:28:51 +00:00
|
|
|
#log<T>(
|
2023-11-27 19:26:21 +00:00
|
|
|
level: LogLevel,
|
2020-08-24 23:43:54 +00:00
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 13:27:41 +00:00
|
|
|
...args: unknown[]
|
|
|
|
): T | undefined {
|
|
|
|
if (this.level > level) {
|
|
|
|
return msg instanceof Function ? undefined : msg;
|
|
|
|
}
|
2019-01-02 14:12:48 +00:00
|
|
|
|
2020-06-12 13:27:41 +00:00
|
|
|
let fnResult: T | undefined;
|
|
|
|
let logMessage: string;
|
|
|
|
if (msg instanceof Function) {
|
|
|
|
fnResult = msg();
|
|
|
|
logMessage = this.asString(fnResult);
|
|
|
|
} else {
|
|
|
|
logMessage = this.asString(msg);
|
|
|
|
}
|
2020-06-18 10:50:18 +00:00
|
|
|
const record: LogRecord = new LogRecord({
|
|
|
|
msg: logMessage,
|
|
|
|
args: args,
|
|
|
|
level: level,
|
|
|
|
loggerName: this.loggerName,
|
|
|
|
});
|
2020-04-09 11:45:24 +00:00
|
|
|
|
2024-01-04 22:57:26 +00:00
|
|
|
this.handlers.forEach((handler) => {
|
2019-11-13 18:42:34 +00:00
|
|
|
handler.handle(record);
|
|
|
|
});
|
2020-06-12 13:27:41 +00:00
|
|
|
|
|
|
|
return msg instanceof Function ? fnResult : msg;
|
|
|
|
}
|
|
|
|
|
2023-08-21 04:26:58 +00:00
|
|
|
asString(data: unknown, isProperty = false): string {
|
2020-06-12 13:27:41 +00:00
|
|
|
if (typeof data === "string") {
|
2023-08-21 04:26:58 +00:00
|
|
|
if (isProperty) return `"${data}"`;
|
2020-06-12 13:27:41 +00:00
|
|
|
return data;
|
|
|
|
} else if (
|
|
|
|
data === null ||
|
|
|
|
typeof data === "number" ||
|
|
|
|
typeof data === "bigint" ||
|
|
|
|
typeof data === "boolean" ||
|
|
|
|
typeof data === "undefined" ||
|
|
|
|
typeof data === "symbol"
|
|
|
|
) {
|
|
|
|
return String(data);
|
2020-11-19 11:58:53 +00:00
|
|
|
} else if (data instanceof Error) {
|
|
|
|
return data.stack!;
|
2020-06-12 13:27:41 +00:00
|
|
|
} else if (typeof data === "object") {
|
2023-08-21 04:26:58 +00:00
|
|
|
return `{${
|
|
|
|
Object.entries(data)
|
|
|
|
.map(([k, v]) => `"${k}":${this.asString(v, true)}`)
|
|
|
|
.join(",")
|
|
|
|
}}`;
|
2020-06-12 13:27:41 +00:00
|
|
|
}
|
|
|
|
return "undefined";
|
2018-12-19 18:16:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 13:27:41 +00:00
|
|
|
debug<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
2020-08-24 23:43:54 +00:00
|
|
|
debug<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;
|
2020-06-12 13:27:41 +00:00
|
|
|
debug<T>(
|
2020-08-24 23:43:54 +00:00
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 13:27:41 +00:00
|
|
|
...args: unknown[]
|
|
|
|
): T | undefined {
|
2024-01-27 16:28:51 +00:00
|
|
|
return this.#log(LogLevels.DEBUG, msg, ...args);
|
2018-12-19 18:16:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 13:27:41 +00:00
|
|
|
info<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
2020-08-24 23:43:54 +00:00
|
|
|
info<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;
|
2020-06-12 13:27:41 +00:00
|
|
|
info<T>(
|
2020-08-24 23:43:54 +00:00
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 13:27:41 +00:00
|
|
|
...args: unknown[]
|
|
|
|
): T | undefined {
|
2024-01-27 16:28:51 +00:00
|
|
|
return this.#log(LogLevels.INFO, msg, ...args);
|
2018-12-19 18:16:45 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 01:36:15 +00:00
|
|
|
/** @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead. */
|
2020-06-12 13:27:41 +00:00
|
|
|
warning<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
2024-01-11 01:36:15 +00:00
|
|
|
/** @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead. */
|
2020-08-24 23:43:54 +00:00
|
|
|
warning<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;
|
2020-06-12 13:27:41 +00:00
|
|
|
warning<T>(
|
2020-08-24 23:43:54 +00:00
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 13:27:41 +00:00
|
|
|
...args: unknown[]
|
2024-01-11 01:36:15 +00:00
|
|
|
): T | undefined {
|
|
|
|
return this.warn(msg as () => T, ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
warn<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
|
|
|
warn<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;
|
|
|
|
warn<T>(
|
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
|
|
|
...args: unknown[]
|
2020-06-12 13:27:41 +00:00
|
|
|
): T | undefined {
|
2024-01-27 16:28:51 +00:00
|
|
|
return this.#log(LogLevels.WARNING, msg, ...args);
|
2018-12-19 18:16:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 13:27:41 +00:00
|
|
|
error<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
2020-08-24 23:43:54 +00:00
|
|
|
error<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;
|
2020-06-12 13:27:41 +00:00
|
|
|
error<T>(
|
2020-08-24 23:43:54 +00:00
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 13:27:41 +00:00
|
|
|
...args: unknown[]
|
|
|
|
): T | undefined {
|
2024-01-27 16:28:51 +00:00
|
|
|
return this.#log(LogLevels.ERROR, msg, ...args);
|
2018-12-19 18:16:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 13:27:41 +00:00
|
|
|
critical<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
|
|
|
critical<T>(
|
2020-08-24 23:43:54 +00:00
|
|
|
msg: T extends GenericFunction ? never : T,
|
|
|
|
...args: unknown[]
|
|
|
|
): T;
|
|
|
|
critical<T>(
|
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 13:27:41 +00:00
|
|
|
...args: unknown[]
|
|
|
|
): T | undefined {
|
2024-01-27 16:28:51 +00:00
|
|
|
return this.#log(LogLevels.CRITICAL, msg, ...args);
|
2018-12-19 18:16:45 +00:00
|
|
|
}
|
|
|
|
}
|