-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
83 lines (67 loc) · 2.05 KB
/
index.ts
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
import pino, {
stdSerializers,
Bindings,
Logger as PinoLogger,
LoggerOptions as PinoLoggerOptions,
DestinationStream,
LevelWithSilent,
} from "pino";
export interface LoggerOptions extends PinoLoggerOptions {
destination?: DestinationStream;
logger?: PinoLogger;
}
const serializers = {
error: stdSerializers.err,
request: stdSerializers.req,
response: stdSerializers.res,
};
export class Logger {
readonly #logger: PinoLogger;
public constructor({ logger, destination, ...options }: LoggerOptions = {}) {
this.#logger = destination
? pino({ level: Logger.getLevel(), serializers, ...options }, destination)
: logger ?? pino({ level: Logger.getLevel(), serializers, ...options });
}
public child(params: Bindings = {}): Logger {
return new Logger({ logger: this.#logger.child(params) });
}
public fatal(message: string, extra: Record<string, unknown> = {}): void {
this.#logger.fatal(extra, message);
}
public error(message: string, extra: Record<string, unknown> = {}): void {
this.#logger.error(extra, message);
}
public warn(message: string, extra: Record<string, unknown> = {}): void {
this.#logger.warn(extra, message);
}
public info(message: string, extra: Record<string, unknown> = {}): void {
this.#logger.info(extra, message);
}
public debug(message: string, extra: Record<string, unknown> = {}): void {
this.#logger.debug(extra, message);
}
public trace(message: string, extra: Record<string, unknown> = {}): void {
this.#logger.trace(extra, message);
}
public static get ENV_VARIABLE_NAME(): string {
return "BINDEN_LOG_LEVEL";
}
public static getLevel(env_name = this.ENV_VARIABLE_NAME): LevelWithSilent {
const {
env: { [env_name]: LEVEL },
} = process;
const level = LEVEL?.trim().toLowerCase();
switch (level) {
case "trace":
case "debug":
case "info":
case "warn":
case "error":
case "fatal":
return level;
default:
return "silent";
}
}
}
export default new Logger();