@@ -8,6 +8,9 @@ import { createGzip } from 'node:zlib';
88import serverHandler from './dist/server/server.js' ;
99import { extractUserInfoFromRequest , logRequestInit , logResponse } from './request-logging.js' ;
1010
11+ // Verify logging functions are loaded
12+ console . log ( '✅ Request logging module loaded' ) ;
13+
1114const __dirname = fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
1215const PORT = process . env . PORT || 3030 ;
1316const HOST = process . env . HOST || '0.0.0.0' ;
@@ -74,9 +77,36 @@ const server = createServer(async (req, res) => {
7477 const pathname = url . pathname ;
7578 const method = req . method ;
7679
80+ // Debug: Log that request handler is being called
81+ console . log ( `[DEBUG] Request received: ${ method } ${ pathname } [${ requestId } ]` ) ;
82+
7783 // Extract user ID and org ID and log request initialization
78- const { userId, orgId } = await extractUserInfoFromRequest ( req ) ;
79- logRequestInit ( method , pathname , requestId , userId , orgId ) ;
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+ }
80110
81111 // Set request timeout
82112 req . setTimeout ( REQUEST_TIMEOUT , ( ) => {
@@ -108,8 +138,12 @@ const server = createServer(async (req, res) => {
108138 } ) ;
109139 res . end ( content ) ;
110140 // Log response for static files
111- const latency = Date . now ( ) - requestStart ;
112- logResponse ( method , pathname , requestId , latency , 200 ) ;
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+ }
113147 return ;
114148 } catch ( err ) {
115149 // File not found, fall through to SSR handler
@@ -234,8 +268,12 @@ const server = createServer(async (req, res) => {
234268 }
235269
236270 // Log response after sending
237- const latency = Date . now ( ) - requestStart ;
238- logResponse ( method , pathname , requestId , latency , res . statusCode ) ;
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+ }
239277 } catch ( error ) {
240278 console . error ( `Server error [${ requestId } ]:` , error ) ;
241279 if ( ! res . headersSent ) {
@@ -244,8 +282,12 @@ const server = createServer(async (req, res) => {
244282 res . end ( 'Internal Server Error' ) ;
245283 }
246284 // Log error response
247- const latency = Date . now ( ) - requestStart ;
248- logResponse ( method , pathname , requestId , latency , res . statusCode || 500 ) ;
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+ }
249291 }
250292} ) ;
251293
0 commit comments