File tree Expand file tree Collapse file tree 5 files changed +58
-1
lines changed Expand file tree Collapse file tree 5 files changed +58
-1
lines changed Original file line number Diff line number Diff line change 11import {
22 checkMandatoryPrivateEnvVarsHandle ,
3+ httpLogHandle ,
34 maintenanceModeHandle ,
45} from '$lib/server/core/hooks' ;
56import { addAuthDataToLocalHandle } from '$lib/server/lucia/hooks' ;
@@ -27,10 +28,14 @@ setupSentryClient({
2728roarr . info ( 'Starting the app server...' ) ;
2829
2930export 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 ) {
Original file line number Diff line number Diff 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} ;
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 11export * from './check-mandatory-private-env-vars.handle' ;
22export * from './maintenance-mode.handle' ;
3+ export * from './http-log.handle' ;
Original file line number Diff line number Diff 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+
121129function getCallName ( stackLevel : number = 3 ) : string {
122130 const typeName = callsites ( ) [ stackLevel ] ?. getTypeName ( ) ?? '' ;
123131 const functionName =
You can’t perform that action at this time.
0 commit comments