Skip to content

Commit 7a17a1b

Browse files
committed
🔊 Move api key to json payload in log entries
1 parent 559da48 commit 7a17a1b

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

‎src/util/logger.util.ts‎

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,15 @@ import { playtypusUserIdStorage } from '../middlewares';
66
function addMessageToTraceSpan(
77
method: 'log' | 'error' | 'warn',
88
message: string,
9-
args: unknown[] = [],
9+
args?: unknown[],
1010
) {
1111
const span = trace.getSpan(context.active());
12-
if (!span) {
13-
return;
12+
if (span) {
13+
span.addEvent(method, {
14+
message,
15+
args: args ? args.map((arg) => JSON.stringify(arg)).join(',') : '',
16+
});
1417
}
15-
16-
const data = args;
17-
const userId = playtypusUserIdStorage.getStore();
18-
19-
if (userId) {
20-
data.push({ platypusUserId: userId });
21-
}
22-
23-
span.addEvent(method, {
24-
message,
25-
args:
26-
data?.length !== 0
27-
? data.map((arg) => JSON.stringify(arg)).join(',')
28-
: '',
29-
});
3018
}
3119

3220
/**
@@ -89,14 +77,7 @@ const logger = (
8977
apiKey: string | undefined,
9078
...args: unknown[]
9179
): void => {
92-
// eslint-disable-next-line no-console
93-
const anonymizedApiKey = apiKey ? anonymizeKey(apiKey) : undefined;
94-
95-
const formatedMessage = constructLogMessage(
96-
anonymizedApiKey ? `[${anonymizedApiKey}]` : undefined,
97-
`[${source}]`,
98-
message,
99-
);
80+
const formatedMessage = constructLogMessage(`[${source}]`, message);
10081

10182
if (process.env.NODE_ENV === 'development') {
10283
logFn(formatedMessage, ...args);
@@ -110,6 +91,12 @@ const logger = (
11091
data.platypusUserId = userId;
11192
}
11293

94+
const anonymizedApiKey = apiKey ? anonymizeKey(apiKey) : undefined;
95+
96+
if (anonymizedApiKey) {
97+
data.apiKey = anonymizedApiKey;
98+
}
99+
113100
logFn(
114101
JSON.stringify({
115102
message: formatedMessage,

0 commit comments

Comments
 (0)