1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
// 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 interface LogRecordOptions {
msg: string;
args: unknown[];
level: number;
loggerName: string;
}
export class LogRecord {
readonly msg: string;
#args: unknown[];
#datetime: Date;
readonly level: number;
readonly levelName: string;
readonly loggerName: string;
constructor(options: LogRecordOptions) {
this.msg = options.msg;
this.#args = [...options.args];
this.level = options.level;
this.loggerName = options.loggerName;
this.#datetime = new Date();
this.levelName = getLevelName(options.level);
}
get args(): unknown[] {
return [...this.#args];
}
get datetime(): Date {
return new Date(this.#datetime.getTime());
}
}
export interface LoggerOptions {
handlers?: BaseHandler[];
}
export class Logger {
level: number;
levelName: LevelName;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handlers: any[];
loggerName: string;
constructor(
loggerName: string,
levelName: LevelName,
options: LoggerOptions = {}
) {
this.loggerName = loggerName;
this.level = getLevelByName(levelName);
this.levelName = levelName;
this.handlers = options.handlers || [];
}
/** If the level of the logger is greater than the level to log, then nothing
* 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.
*/
_log<T>(
level: number,
msg: (T extends Function ? never : T) | (() => T),
...args: unknown[]
): T | undefined {
if (this.level > level) {
return msg instanceof Function ? undefined : msg;
}
let fnResult: T | undefined;
let logMessage: string;
if (msg instanceof Function) {
fnResult = msg();
logMessage = this.asString(fnResult);
} else {
logMessage = this.asString(msg);
}
const record: LogRecord = new LogRecord({
msg: logMessage,
args: args,
level: level,
loggerName: this.loggerName,
});
this.handlers.forEach((handler): void => {
handler.handle(record);
});
return msg instanceof Function ? fnResult : msg;
}
asString(data: unknown): string {
if (typeof data === "string") {
return data;
} else if (
data === null ||
typeof data === "number" ||
typeof data === "bigint" ||
typeof data === "boolean" ||
typeof data === "undefined" ||
typeof data === "symbol"
) {
return String(data);
} else if (typeof data === "object") {
return JSON.stringify(data);
}
return "undefined";
}
debug<T>(msg: () => T, ...args: unknown[]): T | undefined;
debug<T>(msg: T extends Function ? never : T, ...args: unknown[]): T;
debug<T>(
msg: (T extends Function ? never : T) | (() => T),
...args: unknown[]
): T | undefined {
return this._log(LogLevels.DEBUG, msg, ...args);
}
info<T>(msg: () => T, ...args: unknown[]): T | undefined;
info<T>(msg: T extends Function ? never : T, ...args: unknown[]): T;
info<T>(
msg: (T extends Function ? never : T) | (() => T),
...args: unknown[]
): T | undefined {
return this._log(LogLevels.INFO, msg, ...args);
}
warning<T>(msg: () => T, ...args: unknown[]): T | undefined;
warning<T>(msg: T extends Function ? never : T, ...args: unknown[]): T;
warning<T>(
msg: (T extends Function ? never : T) | (() => T),
...args: unknown[]
): T | undefined {
return this._log(LogLevels.WARNING, msg, ...args);
}
error<T>(msg: () => T, ...args: unknown[]): T | undefined;
error<T>(msg: T extends Function ? never : T, ...args: unknown[]): T;
error<T>(
msg: (T extends Function ? never : T) | (() => T),
...args: unknown[]
): T | undefined {
return this._log(LogLevels.ERROR, msg, ...args);
}
critical<T>(msg: () => T, ...args: unknown[]): T | undefined;
critical<T>(msg: T extends Function ? never : T, ...args: unknown[]): T;
critical<T>(
msg: (T extends Function ? never : T) | (() => T),
...args: unknown[]
): T | undefined {
return this._log(LogLevels.CRITICAL, msg, ...args);
}
}
|