-
Notifications
You must be signed in to change notification settings - Fork 98
/
PinoLogger.ts
106 lines (89 loc) · 2.82 KB
/
PinoLogger.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Injectable, Inject, Scope } from "@nestjs/common";
import * as pino from "pino";
import { getValue } from "express-ctx";
import { PARAMS_PROVIDER_TOKEN, LOGGER_KEY } from "./constants";
import { Params, isPassedLogger } from "./params";
interface PinoMethods
extends Pick<
pino.BaseLogger,
"trace" | "debug" | "info" | "warn" | "error" | "fatal"
> {}
type LoggerFn =
| ((msg: string, ...args: any[]) => void)
| ((obj: object, msg?: string, ...args: any[]) => void);
let outOfContext: pino.Logger;
export function __resetOutOfContextForTests() {
// only for tests
// @ts-ignore
outOfContext = undefined;
}
@Injectable({ scope: Scope.TRANSIENT })
export class PinoLogger implements PinoMethods {
private context = "";
private readonly contextName: string;
constructor(
@Inject(PARAMS_PROVIDER_TOKEN) { pinoHttp, renameContext }: Params
) {
if (!outOfContext) {
if (Array.isArray(pinoHttp)) {
outOfContext = pino(...pinoHttp);
} else if (isPassedLogger(pinoHttp)) {
outOfContext = pinoHttp.logger;
} else {
outOfContext = pino(pinoHttp);
}
}
this.contextName = renameContext || "context";
}
trace(msg: string, ...args: any[]): void;
trace(obj: object, msg?: string, ...args: any[]): void;
trace(...args: Parameters<LoggerFn>) {
this.call("trace", ...args);
}
debug(msg: string, ...args: any[]): void;
debug(obj: object, msg?: string, ...args: any[]): void;
debug(...args: Parameters<LoggerFn>) {
this.call("debug", ...args);
}
info(msg: string, ...args: any[]): void;
info(obj: object, msg?: string, ...args: any[]): void;
info(...args: Parameters<LoggerFn>) {
this.call("info", ...args);
}
warn(msg: string, ...args: any[]): void;
warn(obj: object, msg?: string, ...args: any[]): void;
warn(...args: Parameters<LoggerFn>) {
this.call("warn", ...args);
}
error(msg: string, ...args: any[]): void;
error(obj: object, msg?: string, ...args: any[]): void;
error(...args: Parameters<LoggerFn>) {
this.call("error", ...args);
}
fatal(msg: string, ...args: any[]): void;
fatal(obj: object, msg?: string, ...args: any[]): void;
fatal(...args: Parameters<LoggerFn>) {
this.call("fatal", ...args);
}
setContext(value: string) {
this.context = value;
}
private call(method: pino.Level, ...args: Parameters<LoggerFn>) {
const context = this.context;
if (context) {
const firstArg = args[0];
if (typeof firstArg === "object") {
args = [
Object.assign({ [this.contextName]: context }, firstArg),
...args.slice(1)
];
} else {
args = [{ [this.contextName]: context }, ...args];
}
}
(this.logger[method] as any)(...args);
}
private get logger() {
return getValue<pino.Logger>(LOGGER_KEY) || outOfContext;
}
}