Skip to content

Commit eddca7e

Browse files
committed
Implement HTTP access logging
1 parent 96082ad commit eddca7e

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

src/hooks.server.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
checkMandatoryPrivateEnvVarsHandle,
3+
httpLogHandle,
34
maintenanceModeHandle,
45
} from '$lib/server/core/hooks';
56
import { addAuthDataToLocalHandle } from '$lib/server/lucia/hooks';
@@ -27,10 +28,14 @@ setupSentryClient({
2728
roarr.info('Starting the app server...');
2829

2930
export const handle = (async (input) => {
30-
const maintenanceModeHandles: Handle[] = [maintenanceModeHandle];
31+
const maintenanceModeHandles: Handle[] = [
32+
httpLogHandle,
33+
maintenanceModeHandle,
34+
];
3135
const nonMaintenanceModeHandles: Handle[] = [
3236
checkMandatoryPrivateEnvVarsHandle,
3337
addAuthDataToLocalHandle,
38+
httpLogHandle,
3439
];
3540

3641
if (sentry) {

src/lib/server/core/config/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,8 @@ export const config = {
6565
get isDebugContextShown(): boolean {
6666
return privateEnv.ROARR_SHOW_DEBUG_CONTEXT === 'true';
6767
},
68+
get isAccessLoggingEnabled(): boolean {
69+
return privateEnv.ROARR_ACCESS_LOG === 'true';
70+
},
6871
},
6972
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { Handle } from '@sveltejs/kit';
2+
import { roarr } from '$lib/server/roarr';
3+
import { config } from '$lib/server/core/config';
4+
5+
export const httpLogHandle = (async ({ event, resolve }) => {
6+
if (!config.roarr.isAccessLoggingEnabled) {
7+
return resolve(event);
8+
}
9+
10+
const requestTimestamp = Date.now();
11+
const response = await resolve(event);
12+
const responseTimeInMs = Date.now() - requestTimestamp;
13+
14+
const { method, url, headers: requestHeaders } = event.request;
15+
const { status, headers: responseHeaders } = response;
16+
17+
const contentLengthBytesString = responseHeaders.get('content-length');
18+
const contentLengthInBytes: number | null =
19+
Number(contentLengthBytesString) || 0;
20+
21+
roarr.info('Access log', {
22+
logType: 'http',
23+
request: {
24+
address: event.getClientAddress(),
25+
userId: event.locals.authUser?.userId ?? null,
26+
userAgent: requestHeaders.get('user-agent'),
27+
method,
28+
url,
29+
route: event.route.id,
30+
referrer: requestHeaders.get('referer') ?? requestHeaders.get('referrer'),
31+
},
32+
response: {
33+
status: status,
34+
contentLengthInBytes,
35+
responseTimeInMs,
36+
},
37+
});
38+
39+
return response;
40+
}) satisfies Handle;

src/lib/server/core/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './check-mandatory-private-env-vars.handle';
22
export * from './maintenance-mode.handle';
3+
export * from './http-log.handle';

src/lib/server/roarr/client.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const roarr = (function () {
2323
}
2424

2525
let contextClone = serializeErrorInContext(context);
26+
contextClone = enrichContextWithLogType(contextClone);
2627
contextClone = enrichContextWithSentryTraceId(contextClone);
2728

2829
Roarr[methodName](
@@ -118,6 +119,13 @@ function enrichContextWithDebugInfo(
118119
};
119120
}
120121

122+
function enrichContextWithLogType(context: LoggerContext): LoggerContext {
123+
return {
124+
logType: 'app',
125+
...context,
126+
};
127+
}
128+
121129
function getCallName(stackLevel: number = 3): string {
122130
const typeName = callsites()[stackLevel]?.getTypeName() ?? '';
123131
const functionName =

0 commit comments

Comments
 (0)