Skip to content

Commit 412ef97

Browse files
committed
🔊 improve logging utils
1 parent 0a55250 commit 412ef97

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/models/controller.model.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class Controller {
6464
throw new ServerError(501, "Fetching contacts is not implemented");
6565
}
6666

67-
infoLogger(providerConfig, `Fetching contacts…`);
67+
infoLogger(`Fetching contacts…`, providerConfig);
6868

6969
const fetchedContacts: Contact[] = await this.adapter.getContacts(
7070
providerConfig
@@ -89,7 +89,7 @@ export class Controller {
8989

9090
const raceResult = await Promise.race([fetcherPromise, timeoutPromise]);
9191
if (raceResult === "TIMEOUT") {
92-
infoLogger(providerConfig, `Fetching too slow, returning empty array.`);
92+
infoLogger(`Fetching too slow, returning empty array.`, providerConfig);
9393
}
9494

9595
const responseContacts: Contact[] = Array.isArray(raceResult)
@@ -98,7 +98,7 @@ export class Controller {
9898

9999
const contactsCount = responseContacts.length;
100100

101-
infoLogger(providerConfig, `Found ${contactsCount} cached contacts.`);
101+
infoLogger(`Found ${contactsCount} cached contacts.`, providerConfig);
102102

103103
if (
104104
!Array.isArray(raceResult) &&
@@ -116,8 +116,8 @@ export class Controller {
116116
res.status(200).send(responseContacts);
117117
} catch (error) {
118118
errorLogger(
119-
providerConfig,
120119
"Could not get contacts:",
120+
providerConfig,
121121
error || "Unknown"
122122
);
123123
next(error);
@@ -469,14 +469,14 @@ export class Controller {
469469

470470
if (shouldSkipCallEvent(req.body as CallEvent)) {
471471
infoLogger(
472-
providerConfig,
473-
`Skipping call event for call id ${req.body.id}`
472+
`Skipping call event for call id ${req.body.id}`,
473+
providerConfig
474474
);
475475
res.status(200).send("Skipping call event");
476476
return;
477477
}
478478

479-
infoLogger(providerConfig, `Handling call event`);
479+
infoLogger(`Handling call event`, providerConfig);
480480

481481
const integrationCallEventRef = await this.adapter.handleCallEvent(
482482
providerConfig,
@@ -486,8 +486,8 @@ export class Controller {
486486
res.status(200).send(integrationCallEventRef);
487487
} catch (error) {
488488
errorLogger(
489-
providerConfig,
490489
"Could not handle call event:",
490+
providerConfig,
491491
error || "Unknown"
492492
);
493493
next(error);

src/util/logger.util.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,49 @@ import { anonymizeKey } from "./anonymize-key";
22
import { Config } from "../models";
33

44
export const infoLogger = (
5-
{ apiKey }: Config,
65
message: string,
6+
config?: Config,
77
...args: unknown[]
88
): void => {
99
// eslint-disable-next-line no-console
10-
console.log(
11-
`${anonymizeKey(apiKey)}: ${message}`,
12-
args && args.length ? args : ""
13-
);
10+
if (config) {
11+
console.log(
12+
`${`[${anonymizeKey(config.apiKey)}]`}: ${message}`,
13+
args && args.length ? args : ""
14+
);
15+
} else {
16+
console.log(message, args && args.length ? args : "");
17+
}
1418
};
1519

1620
export const errorLogger = (
17-
config: Config | undefined,
1821
message: string,
22+
config?: Config,
1923
...args: unknown[]
2024
): void => {
2125
// eslint-disable-next-line no-console
22-
console.error(
23-
`${config?.apiKey ? `[${anonymizeKey(config.apiKey)}]` : ""}: ${message}`,
24-
args && args.length ? args : ""
25-
);
26+
if (config?.apiKey) {
27+
console.error(
28+
`[${anonymizeKey(config.apiKey)}] ${message}`,
29+
args && args.length ? args : ""
30+
);
31+
} else {
32+
console.error(message, args && args.length ? args : "");
33+
}
2634
};
2735

2836
export const warnLogger = (
29-
config: Config | undefined,
3037
message: string,
38+
config?: Config,
3139
...args: unknown[]
3240
): void => {
3341
// eslint-disable-next-line no-console
34-
console.warn(
35-
`${config?.apiKey ? `[${anonymizeKey(config.apiKey)}]` : ""}: ${message}`,
36-
args && args.length ? args : ""
37-
);
42+
if (config?.apiKey) {
43+
console.warn(
44+
`[${anonymizeKey(config.apiKey)}] ${message}`,
45+
args && args.length ? args : ""
46+
);
47+
} else {
48+
console.warn(message, args && args.length ? args : "");
49+
}
3850
};

0 commit comments

Comments
 (0)