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
|
// Request logging middleware
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
|
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();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -22,9 +29,27 @@ app.use('/v1', authenticateApiKey);
|
||||||
// Routes
|
// Routes
|
||||||
app.use('/', 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
|
// Error handling middleware
|
||||||
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
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({
|
res.status(500).json({
|
||||||
error: {
|
error: {
|
||||||
message: 'Internal server error',
|
message: 'Internal server error',
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,17 @@ router.post('/v1/chat/completions', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const body: OpenAIChatRequest = req.body;
|
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, {
|
requestLogger.logRequest(requestId, {
|
||||||
path: req.path,
|
path: req.path,
|
||||||
method: req.method,
|
method: req.method,
|
||||||
|
|
@ -103,6 +113,13 @@ router.post('/v1/chat/completions', async (req: Request, res: Response) => {
|
||||||
const openaiResponse = convertToOpenAIResponse({ response }, requestedModel, requestId);
|
const openaiResponse = convertToOpenAIResponse({ response }, requestedModel, requestId);
|
||||||
|
|
||||||
// Log the response
|
// Log the response
|
||||||
|
console.log('[DEBUG] Successful response:', {
|
||||||
|
requestId,
|
||||||
|
statusCode: 200,
|
||||||
|
model: requestedModel,
|
||||||
|
contentLength: openaiResponse.choices[0]?.message?.content?.length || 0
|
||||||
|
});
|
||||||
|
|
||||||
requestLogger.logResponse(requestId, {
|
requestLogger.logResponse(requestId, {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
response: openaiResponse
|
response: openaiResponse
|
||||||
|
|
@ -111,7 +128,14 @@ router.post('/v1/chat/completions', async (req: Request, res: Response) => {
|
||||||
res.json(openaiResponse);
|
res.json(openaiResponse);
|
||||||
|
|
||||||
} catch (error: any) {
|
} 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
|
// Log the error
|
||||||
requestLogger.logError(requestId, {
|
requestLogger.logError(requestId, {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue