Skip to content

Commit de963dc

Browse files
refactor(logger): replace abbreviations with words (#4853)
This refactors the internal parameters used in the default logger with the names that are documented by Pino: <https://github.com/pinojs/pino/blob/main/docs/api.md#trace>. This is useful because we document these parameters and abbreviations are jargon not always understood by everyone.
1 parent 2380307 commit de963dc

File tree

1 file changed

+100
-60
lines changed

1 file changed

+100
-60
lines changed

logger/index.ts

Lines changed: 100 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ function bigintReplacer(key: string, value: unknown) {
99
}
1010

1111
// TODO: Deduplicate this and sprintf implementation
12-
function tryStringify(o: unknown) {
12+
function tryStringify(value: unknown) {
1313
try {
14-
return JSON.stringify(o, bigintReplacer);
14+
return JSON.stringify(value, bigintReplacer);
1515
} catch {
1616
return "[Circular]";
1717
}
@@ -34,37 +34,45 @@ export interface LoggerOptions {
3434

3535
const PREFIX = "✦Aj";
3636

37-
function getMessage(obj: unknown, msg: unknown, args: unknown[]) {
38-
// The first argument was the message so juggle the args
39-
if (typeof obj === "string") {
40-
args = [msg, ...args];
41-
msg = obj;
37+
function getMessage(
38+
mergingObject: unknown,
39+
message: unknown,
40+
interpolationValues: unknown[],
41+
) {
42+
// The first argument was the message so juggle the arguments
43+
if (typeof mergingObject === "string") {
44+
interpolationValues = [message, ...interpolationValues];
45+
message = mergingObject;
4246
}
4347

44-
// Prefer a string message over `obj.msg`, as per Pino:
48+
// Prefer a string message over `mergingObject.msg`, as per Pino:
4549
// https://github.com/pinojs/pino/blob/8db130eba0439e61c802448d31eb1998cebfbc98/docs/api.md#message-string
46-
if (typeof msg === "string") {
47-
return format(msg, ...args);
50+
if (typeof message === "string") {
51+
return format(message, ...interpolationValues);
4852
}
4953

5054
if (
51-
typeof obj === "object" &&
52-
obj !== null &&
53-
"msg" in obj &&
54-
typeof obj.msg === "string"
55+
typeof mergingObject === "object" &&
56+
mergingObject !== null &&
57+
"msg" in mergingObject &&
58+
typeof mergingObject.msg === "string"
5559
) {
56-
return format(obj.msg, [msg, ...args]);
60+
return format(mergingObject.msg, [message, ...interpolationValues]);
5761
}
5862
}
5963

60-
function getOutput(obj: unknown, msg: unknown, args: unknown[]) {
61-
let output = getMessage(obj, msg, args);
64+
function getOutput(
65+
mergingObject: unknown,
66+
message: unknown,
67+
interpolationValues: unknown[],
68+
) {
69+
let output = getMessage(mergingObject, message, interpolationValues);
6270
if (typeof output !== "string") {
6371
return;
6472
}
6573

66-
if (typeof obj === "object" && obj !== null) {
67-
for (const [key, value] of Object.entries(obj)) {
74+
if (typeof mergingObject === "object" && mergingObject !== null) {
75+
for (const [key, value] of Object.entries(mergingObject)) {
6876
output += `\n ${key}: ${tryStringify(value)}`;
6977
}
7078
}
@@ -81,17 +89,17 @@ export class Logger {
8189
/**
8290
* Configuration.
8391
*
84-
* @param opts
92+
* @param options
8593
* Configuration.
8694
* @returns
8795
* Logger.
8896
*/
89-
constructor(opts: LoggerOptions) {
90-
if (typeof opts.level !== "string") {
97+
constructor(options: LoggerOptions) {
98+
if (typeof options.level !== "string") {
9199
throw new Error(`Invalid log level`);
92100
}
93101

94-
switch (opts.level) {
102+
switch (options.level) {
95103
case "debug":
96104
this.#logLevel = 0;
97105
break;
@@ -105,38 +113,46 @@ export class Logger {
105113
this.#logLevel = 3;
106114
break;
107115
default: {
108-
throw new Error(`Unknown log level: ${opts.level}`);
116+
throw new Error(`Unknown log level: ${options.level}`);
109117
}
110118
}
111119
}
112120

113121
/**
114122
* Debug.
115123
*
116-
* @param msg
124+
* @param message
117125
* Template.
118-
* @param args
126+
* @param interpolationValues
119127
* Parameters to interpolate.
120128
* @returns
121129
* Nothing.
122130
*/
123-
debug(msg: string, ...args: unknown[]): void;
131+
debug(message: string, ...interpolationValues: unknown[]): void;
124132
/**
125133
* Debug.
126134
*
127-
* @param obj
135+
* @param mergingObject
128136
* Merging object copied into the JSON log line.
129-
* @param msg
137+
* @param message
130138
* Template.
131-
* @param args
139+
* @param interpolationValues
132140
* Parameters to interpolate.
133141
* @returns
134142
* Nothing.
135143
*/
136-
debug(obj: Record<string, unknown>, msg?: string, ...args: unknown[]): void;
137-
debug(obj: unknown, msg?: unknown, ...args: unknown[]): void {
144+
debug(
145+
mergingObject: Record<string, unknown>,
146+
message?: string,
147+
...interpolationValues: unknown[]
148+
): void;
149+
debug(
150+
mergingObject: unknown,
151+
message?: unknown,
152+
...interpolationValues: unknown[]
153+
): void {
138154
if (this.#logLevel <= 0) {
139-
const output = getOutput(obj, msg, args);
155+
const output = getOutput(mergingObject, message, interpolationValues);
140156
if (typeof output !== "undefined") {
141157
console.debug(`${PREFIX} DEBUG ${output}`);
142158
}
@@ -146,30 +162,38 @@ export class Logger {
146162
/**
147163
* Info.
148164
*
149-
* @param msg
165+
* @param message
150166
* Template.
151-
* @param args
167+
* @param interpolationValues
152168
* Parameters to interpolate.
153169
* @returns
154170
* Nothing.
155171
*/
156-
info(msg: string, ...args: unknown[]): void;
172+
info(message: string, ...interpolationValues: unknown[]): void;
157173
/**
158174
* Info.
159175
*
160-
* @param obj
176+
* @param mergingObject
161177
* Merging object copied into the JSON log line.
162-
* @param msg
178+
* @param message
163179
* Template.
164-
* @param args
180+
* @param interpolationValues
165181
* Parameters to interpolate.
166182
* @returns
167183
* Nothing.
168184
*/
169-
info(obj: Record<string, unknown>, msg?: string, ...args: unknown[]): void;
170-
info(obj: unknown, msg?: unknown, ...args: unknown[]): void {
185+
info(
186+
mergingObject: Record<string, unknown>,
187+
message?: string,
188+
...interpolationValues: unknown[]
189+
): void;
190+
info(
191+
mergingObject: unknown,
192+
message?: unknown,
193+
...interpolationValues: unknown[]
194+
): void {
171195
if (this.#logLevel <= 1) {
172-
const output = getOutput(obj, msg, args);
196+
const output = getOutput(mergingObject, message, interpolationValues);
173197
if (typeof output !== "undefined") {
174198
console.info(`${PREFIX} INFO ${output}`);
175199
}
@@ -179,30 +203,38 @@ export class Logger {
179203
/**
180204
* Warn.
181205
*
182-
* @param msg
206+
* @param message
183207
* Template.
184-
* @param args
208+
* @param interpolationValues
185209
* Parameters to interpolate.
186210
* @returns
187211
* Nothing.
188212
*/
189-
warn(msg: string, ...args: unknown[]): void;
213+
warn(message: string, ...interpolationValues: unknown[]): void;
190214
/**
191215
* Warn.
192216
*
193-
* @param obj
217+
* @param mergingObject
194218
* Merging object copied into the JSON log line.
195-
* @param msg
219+
* @param message
196220
* Template.
197-
* @param args
221+
* @param interpolationValues
198222
* Parameters to interpolate.
199223
* @returns
200224
* Nothing.
201225
*/
202-
warn(obj: Record<string, unknown>, msg?: string, ...args: unknown[]): void;
203-
warn(obj: unknown, msg?: unknown, ...args: unknown[]): void {
226+
warn(
227+
mergingObject: Record<string, unknown>,
228+
message?: string,
229+
...interpolationValues: unknown[]
230+
): void;
231+
warn(
232+
mergingObject: unknown,
233+
message?: unknown,
234+
...interpolationValues: unknown[]
235+
): void {
204236
if (this.#logLevel <= 2) {
205-
const output = getOutput(obj, msg, args);
237+
const output = getOutput(mergingObject, message, interpolationValues);
206238
if (typeof output !== "undefined") {
207239
console.warn(`${PREFIX} WARN ${output}`);
208240
}
@@ -212,30 +244,38 @@ export class Logger {
212244
/**
213245
* Error.
214246
*
215-
* @param msg
247+
* @param message
216248
* Template.
217-
* @param args
249+
* @param interpolationValues
218250
* Parameters to interpolate.
219251
* @returns
220252
* Nothing.
221253
*/
222-
error(msg: string, ...args: unknown[]): void;
254+
error(message: string, ...interpolationValues: unknown[]): void;
223255
/**
224256
* Error.
225257
*
226-
* @param obj
258+
* @param mergingObject
227259
* Merging object copied into the JSON log line.
228-
* @param msg
260+
* @param message
229261
* Template.
230-
* @param args
262+
* @param interpolationValues
231263
* Parameters to interpolate.
232264
* @returns
233265
* Nothing.
234266
*/
235-
error(obj: Record<string, unknown>, msg?: string, ...args: unknown[]): void;
236-
error(obj: unknown, msg?: unknown, ...args: unknown[]): void {
267+
error(
268+
mergingObject: Record<string, unknown>,
269+
message?: string,
270+
...interpolationValues: unknown[]
271+
): void;
272+
error(
273+
mergingObject: unknown,
274+
message?: unknown,
275+
...interpolationValues: unknown[]
276+
): void {
237277
if (this.#logLevel <= 3) {
238-
const output = getOutput(obj, msg, args);
278+
const output = getOutput(mergingObject, message, interpolationValues);
239279
if (typeof output !== "undefined") {
240280
console.error(`${PREFIX} ERROR ${output}`);
241281
}

0 commit comments

Comments
 (0)