Skip to content

Commit 717c95c

Browse files
BokaTanokevinkoessl
andcommitted
✨ add platypus user ID to logs and spans
Co-Authored-By: Kevin Kössl <[email protected]>
1 parent d277fa9 commit 717c95c

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

src/util/logger.util.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@ 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-
span.addEvent(method, {
14-
message,
15-
args: args ? args.map((arg) => JSON.stringify(arg)).join(',') : '',
16-
});
12+
if (!span) {
13+
return;
1714
}
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+
});
1830
}
1931

2032
/**
@@ -30,9 +42,7 @@ export const infoLogger = (
3042
apiKey?: string,
3143
...args: unknown[]
3244
): void => {
33-
const userId = playtypusUserIdStorage.getStore();
34-
35-
addMessageToTraceSpan('log', message, [...args, userId]);
45+
addMessageToTraceSpan('log', message, [...args]);
3646

3747
logger(console.info, source, message, apiKey, ...args);
3848
};
@@ -50,9 +60,7 @@ export const errorLogger = (
5060
apiKey?: string,
5161
...args: unknown[]
5262
): void => {
53-
const userId = playtypusUserIdStorage.getStore();
54-
55-
addMessageToTraceSpan('error', message, [...args, userId]);
63+
addMessageToTraceSpan('error', message, [...args]);
5664

5765
logger(console.error, source, message, apiKey, ...args);
5866
};
@@ -70,13 +78,10 @@ export const warnLogger = (
7078
apiKey?: string,
7179
...args: unknown[]
7280
): void => {
73-
const userId = playtypusUserIdStorage.getStore();
74-
75-
addMessageToTraceSpan('warn', message, [...args, userId]);
81+
addMessageToTraceSpan('warn', message, [...args]);
7682

7783
logger(console.warn, source, message, apiKey, ...args);
7884
};
79-
8085
const logger = (
8186
logFn: (message?: any, ...optionalParams: any[]) => void,
8287
source: string,
@@ -86,33 +91,39 @@ const logger = (
8691
): void => {
8792
// eslint-disable-next-line no-console
8893
const anonymizedApiKey = apiKey ? anonymizeKey(apiKey) : undefined;
89-
const userId = playtypusUserIdStorage.getStore();
9094

9195
const formatedMessage = constructLogMessage(
9296
anonymizedApiKey ? `[${anonymizedApiKey}]` : undefined,
93-
userId ? `[${userId}]` : undefined,
9497
`[${source}]`,
9598
message,
9699
);
97100

98-
if (process.env.NODE_ENV == 'development') {
101+
if (process.env.NODE_ENV === 'development') {
99102
logFn(formatedMessage, ...args);
100-
} else {
101-
logFn(
102-
JSON.stringify({
103-
message: formatedMessage,
104-
data: { ...args, integrationUserId: userId },
105-
}),
106-
);
103+
return;
104+
}
105+
106+
const data: Record<string, unknown> = { ...args };
107+
const userId = playtypusUserIdStorage.getStore();
108+
109+
if (userId) {
110+
data.platypusUserId = userId;
107111
}
112+
113+
logFn(
114+
JSON.stringify({
115+
message: formatedMessage,
116+
data,
117+
}),
118+
);
108119
};
109120

110121
const constructLogMessage = (...args: unknown[]): string =>
111122
`${args
112123
.flat()
113-
.filter((item) => item != undefined)
124+
.filter((item) => item !== undefined)
114125
.map((item: unknown) => {
115-
if (Array.isArray(item) && item.length == 0) return;
126+
if (Array.isArray(item) && item.length === 0) return;
116127
return typeof item !== 'string' ? JSON.stringify(item) : item;
117128
})
118129
.join(' ')}`;

0 commit comments

Comments
 (0)