Add better logging?
This commit is contained in:
parent
dfff9d0c52
commit
05e45ae897
2 changed files with 52 additions and 3 deletions
27
src/index.ts
27
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',
|
||||
|
|
|
|||
|
|
@ -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, {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue