mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-04 04:27:47 +01:00
Create lib/logger.ts — lightweight wrapper that outputs plain text in dev and structured JSON in production. Replace 117 console.log/error/warn calls across 31 files (27 API routes + 4 lib files) with logger. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
85 lines
2.0 KiB
TypeScript
85 lines
2.0 KiB
TypeScript
import type { NextRequest } from 'next/server';
|
|
import { logger } from '../logger';
|
|
|
|
interface RequestMetrics {
|
|
path: string;
|
|
method: string;
|
|
startTime: number;
|
|
duration?: number;
|
|
status?: number;
|
|
}
|
|
|
|
const metrics: RequestMetrics[] = [];
|
|
|
|
export async function monitorRequest<T extends Request | NextRequest>(
|
|
req: T,
|
|
handler: (req: T) => Promise<Response>,
|
|
) {
|
|
const startTime = performance.now();
|
|
const getPath = (req: Request | NextRequest): string => {
|
|
if ('nextUrl' in req) {
|
|
return req.nextUrl.pathname;
|
|
}
|
|
try {
|
|
return new URL(req.url).pathname;
|
|
} catch {
|
|
// If URL parsing fails, try to extract path from url string
|
|
const urlPath = req.url.split('?')[0];
|
|
return urlPath.startsWith('http') ? new URL(urlPath).pathname : urlPath;
|
|
}
|
|
};
|
|
|
|
const path = getPath(req);
|
|
const method = req.method;
|
|
|
|
try {
|
|
const response =
|
|
process.env.NODE_ENV === 'test'
|
|
? await handler(req)
|
|
: await Promise.race([
|
|
handler(req),
|
|
new Promise<never>((_, reject) =>
|
|
setTimeout(() => reject(new Error('Request timeout')), 30000),
|
|
),
|
|
]);
|
|
|
|
const duration = performance.now() - startTime;
|
|
|
|
// Store metrics
|
|
metrics.push({
|
|
path,
|
|
method,
|
|
startTime,
|
|
duration,
|
|
status: response.status,
|
|
});
|
|
|
|
// Log performance metrics
|
|
logger.log(
|
|
`API ${method} ${path} completed in ${duration.toFixed(2)}ms with status ${response.status}`,
|
|
);
|
|
|
|
return response;
|
|
} catch (error) {
|
|
const duration = performance.now() - startTime;
|
|
|
|
// Store error metrics
|
|
metrics.push({
|
|
path,
|
|
method,
|
|
startTime,
|
|
duration,
|
|
status: 500,
|
|
});
|
|
|
|
logger.error(`API ${method} ${path} failed after ${duration.toFixed(2)}ms:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Utility to get recent metrics
|
|
export function getRecentMetrics(minutes: number = 5): RequestMetrics[] {
|
|
const cutoff = Date.now() - minutes * 60 * 1000;
|
|
return metrics.filter((m) => m.startTime > cutoff);
|
|
}
|