@@ -6,6 +6,10 @@ import { fileURLToPath } from 'node:url';
66import { Readable } from 'node:stream' ;
77import { createGzip } from 'node:zlib' ;
88import serverHandler from './dist/server/server.js' ;
9+ import { extractUserInfoFromRequest , logRequestInit , logResponse } from './request-logging.js' ;
10+
11+ // Verify logging functions are loaded
12+ console . log ( '✅ Request logging module loaded' ) ;
913
1014const __dirname = fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
1115const PORT = process . env . PORT || 3030 ;
@@ -68,6 +72,42 @@ const server = createServer(async (req, res) => {
6872 const requestId = req . headers [ 'x-request-id' ] || `ssr-${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 10 ) } ` ;
6973 const requestStart = Date . now ( ) ;
7074
75+ // Parse URL early for logging
76+ const url = new URL ( req . url , `http://${ req . headers . host } ` ) ;
77+ const pathname = url . pathname ;
78+ const method = req . method ;
79+
80+ // Debug: Log that request handler is being called
81+ console . log ( `[DEBUG] Request received: ${ method } ${ pathname } [${ requestId } ]` ) ;
82+
83+ // Extract user ID and org ID and log request initialization
84+ // Always log, even if extraction fails
85+ let userId = 'anonymous' ;
86+ let orgId = 'anonymous' ;
87+ try {
88+ const userInfo = await extractUserInfoFromRequest ( req ) ;
89+ userId = userInfo . userId ;
90+ orgId = userInfo . orgId ;
91+ } catch ( error ) {
92+ console . error ( `User info extraction error [${ requestId } ]:` , error ) ;
93+ }
94+
95+ // Always log request initialization
96+ try {
97+ logRequestInit ( method , pathname , requestId , userId , orgId ) ;
98+ } catch ( error ) {
99+ console . error ( `Request logging error [${ requestId } ]:` , error ) ;
100+ // Fallback to direct console.log if logging function fails
101+ console . log ( JSON . stringify ( {
102+ event : 'request_initialized' ,
103+ method,
104+ path : pathname ,
105+ requestId,
106+ userId,
107+ orgId,
108+ } ) ) ;
109+ }
110+
71111 // Set request timeout
72112 req . setTimeout ( REQUEST_TIMEOUT , ( ) => {
73113 console . error ( `⏱️ Request timeout (${ REQUEST_TIMEOUT } ms): ${ req . method } ${ req . url } [${ requestId } ]` ) ;
@@ -78,8 +118,6 @@ const server = createServer(async (req, res) => {
78118 } ) ;
79119
80120 try {
81- const url = new URL ( req . url , `http://${ req . headers . host } ` ) ;
82- const pathname = url . pathname ;
83121
84122 // Try to serve static files from dist/client first
85123 // Serve: /assets/*, *.js, *.css, *.json, images, fonts, favicons
@@ -99,6 +137,13 @@ const server = createServer(async (req, res) => {
99137 'Cache-Control' : 'public, max-age=31536000, immutable' ,
100138 } ) ;
101139 res . end ( content ) ;
140+ // Log response for static files
141+ try {
142+ const latency = Date . now ( ) - requestStart ;
143+ logResponse ( method , pathname , requestId , latency , 200 ) ;
144+ } catch ( err ) {
145+ console . error ( `Response logging error [${ requestId } ]:` , err ) ;
146+ }
102147 return ;
103148 } catch ( err ) {
104149 // File not found, fall through to SSR handler
@@ -132,9 +177,9 @@ const server = createServer(async (req, res) => {
132177
133178 // Log slow SSR requests
134179 if ( ssrTime > 2000 ) {
135- console . log ( `🔥 VERY SLOW SSR: ${ req . method } ${ pathname } took ${ ssrTime } ms [${ requestId } ]` ) ;
180+ console . debug ( `🔥 VERY SLOW SSR: ${ req . method } ${ pathname } took ${ ssrTime } ms [${ requestId } ]` ) ;
136181 } else if ( ssrTime > 1000 ) {
137- console . log ( `⚠️ SLOW SSR: ${ req . method } ${ pathname } took ${ ssrTime } ms [${ requestId } ]` ) ;
182+ console . debug ( `⚠️ SLOW SSR: ${ req . method } ${ pathname } took ${ ssrTime } ms [${ requestId } ]` ) ;
138183 }
139184
140185 // Convert Web Standard Response to Node.js response
@@ -221,13 +266,28 @@ const server = createServer(async (req, res) => {
221266 } else {
222267 res . end ( ) ;
223268 }
269+
270+ // Log response after sending
271+ try {
272+ const latency = Date . now ( ) - requestStart ;
273+ logResponse ( method , pathname , requestId , latency , res . statusCode ) ;
274+ } catch ( err ) {
275+ console . error ( `Response logging error [${ requestId } ]:` , err ) ;
276+ }
224277 } catch ( error ) {
225278 console . error ( `Server error [${ requestId } ]:` , error ) ;
226279 if ( ! res . headersSent ) {
227280 res . statusCode = 500 ;
228281 res . setHeader ( 'Content-Type' , 'text/plain' ) ;
229282 res . end ( 'Internal Server Error' ) ;
230283 }
284+ // Log error response
285+ try {
286+ const latency = Date . now ( ) - requestStart ;
287+ logResponse ( method , pathname , requestId , latency , res . statusCode || 500 ) ;
288+ } catch ( err ) {
289+ console . error ( `Error response logging error [${ requestId } ]:` , err ) ;
290+ }
231291 }
232292} ) ;
233293
0 commit comments