Skip to content

Commit 856d0c3

Browse files
committed
Add Posthog sessionId to browser logs when available
1 parent 13b59bb commit 856d0c3

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/lib/client/logging/client.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import {
55
enrichLoggerContextWithSentryTraceId,
66
} from '$lib/shared/logging/utils';
77
import type { LogLevelName } from './types';
8-
import { logLevelColors, logLevels, logTimestampColors } from './utils';
8+
import {
9+
enrichLoggerContextWithPosthogSessionId,
10+
logLevelColors,
11+
logLevels,
12+
logTimestampColors,
13+
} from './utils';
914

1015
export const logger = (function () {
1116
const createLogger = (methodName: LogLevelName) => {
@@ -18,7 +23,8 @@ export const logger = (function () {
1823
return;
1924
}
2025

21-
const contextClone = enrichLoggerContextWithSentryTraceId(context);
26+
let contextClone = enrichLoggerContextWithSentryTraceId(context);
27+
contextClone = enrichLoggerContextWithPosthogSessionId(contextClone);
2228

2329
console[methodName](
2430
...createLogPrefixStrings(methodName),

src/lib/client/logging/utils/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { posthog } from '$lib/client/posthog';
2+
import { getSessionId } from '$lib/client/posthog/utils';
3+
import type { LoggerContext } from '$lib/shared/logging/types';
14
import type { LogLevelName } from '../types';
25

36
export const logLevels = {
@@ -52,3 +55,21 @@ export const logTimestampColors = {
5255
} satisfies {
5356
[key in LogLevelName]: { color: string };
5457
};
58+
59+
export function enrichLoggerContextWithPosthogSessionId(
60+
context: LoggerContext,
61+
): LoggerContext {
62+
if (!posthog) {
63+
return { ...context };
64+
}
65+
66+
const sessionId = getSessionId();
67+
if (!sessionId) {
68+
return { ...context };
69+
}
70+
71+
return {
72+
...context,
73+
posthogSessionId: getSessionId(),
74+
};
75+
}

0 commit comments

Comments
 (0)