From 05e45ae89770ccde7f872a96838e99227fa4891a Mon Sep 17 00:00:00 2001 From: tyuXX Date: Sun, 27 Sep 2026 16:20:34 +0300 Subject: [PATCH] Add better logging? --- src/index.ts | 27 ++++++++++++++++++++++++++- src/routes.ts | 28 ++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index aec4241..a827adb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,13 @@ app.use(express.json()); // Request logging middleware app.use((req, res, next) => { console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`); + console.log('[REQUEST] Headers:', { + 'content-type': req.headers['content-type'], + 'authorization': req.headers.authorization ? 'Bearer ***' : 'missing', + 'user-agent': req.headers['user-agent'], + 'origin': req.headers['origin'], + 'referer': req.headers['referer'] + }); next(); }); @@ -22,9 +29,27 @@ app.use('/v1', authenticateApiKey); // Routes app.use('/', routes); +// Catch-all route for debugging (must be after routes) +app.use('*', (req, res) => { + console.log('[DEBUG] Unmatched route:', { + method: req.method, + path: req.path, + url: req.url, + headers: req.headers + }); + res.status(404).json({ + error: { + message: 'Route not found', + type: 'invalid_request_error', + code: 'route_not_found', + path: req.path + } + }); +}); + // Error handling middleware app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => { - console.error('Unhandled error:', err); + console.error('[ERROR] Unhandled error:', err); res.status(500).json({ error: { message: 'Internal server error', diff --git a/src/routes.ts b/src/routes.ts index 2a992bb..0de4d80 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -57,7 +57,17 @@ router.post('/v1/chat/completions', async (req: Request, res: Response) => { try { const body: OpenAIChatRequest = req.body; - // Log the request + // Log the request with full details for debugging + console.log('[DEBUG] Incoming request:', { + requestId, + headers: { + 'content-type': req.headers['content-type'], + 'authorization': req.headers.authorization ? 'Bearer ***' : 'missing', + 'user-agent': req.headers['user-agent'] + }, + body: body + }); + requestLogger.logRequest(requestId, { path: req.path, method: req.method, @@ -103,6 +113,13 @@ router.post('/v1/chat/completions', async (req: Request, res: Response) => { const openaiResponse = convertToOpenAIResponse({ response }, requestedModel, requestId); // Log the response + console.log('[DEBUG] Successful response:', { + requestId, + statusCode: 200, + model: requestedModel, + contentLength: openaiResponse.choices[0]?.message?.content?.length || 0 + }); + requestLogger.logResponse(requestId, { statusCode: 200, response: openaiResponse @@ -111,7 +128,14 @@ router.post('/v1/chat/completions', async (req: Request, res: Response) => { res.json(openaiResponse); } catch (error: any) { - console.error('Error in chat completion:', error); + console.error('[ERROR] Error in chat completion:', { + requestId, + error: error.message, + stack: error.stack, + name: error.name, + status: error.status, + code: error.code + }); // Log the error requestLogger.logError(requestId, {