fix: cache log level index at module init in loggingMiddleware

This commit is contained in:
Charles GTE
2026-05-15 21:56:37 +02:00
parent b5350b5fe3
commit ecd09aedb9
+4 -6
View File
@@ -3,11 +3,9 @@ import { NextRequest, NextResponse } from 'next/server';
const LOG_LEVELS = ["debug", "info", "warn", "error"] as const; const LOG_LEVELS = ["debug", "info", "warn", "error"] as const;
type LogLevel = typeof LOG_LEVELS[number]; type LogLevel = typeof LOG_LEVELS[number];
function currentLevelIndex(): number { const raw = (process.env.LOG_LEVEL ?? "info") as LogLevel;
const raw = (process.env.LOG_LEVEL ?? "info") as LogLevel; const _idx = LOG_LEVELS.indexOf(raw);
const idx = LOG_LEVELS.indexOf(raw); const LEVEL_INDEX = _idx === -1 ? 1 : _idx;
return idx === -1 ? 1 : idx;
}
const STATUS_PATH_RE = /\/api\/agent\/[^/]+\/status\/?$/; const STATUS_PATH_RE = /\/api\/agent\/[^/]+\/status\/?$/;
@@ -16,7 +14,7 @@ export function loggingMiddleware(request: NextRequest) {
const isStatusPing = STATUS_PATH_RE.test(request.nextUrl.pathname); const isStatusPing = STATUS_PATH_RE.test(request.nextUrl.pathname);
// Status pings are debug-level; all other API calls are info-level. // Status pings are debug-level; all other API calls are info-level.
const requiredIndex = isStatusPing ? 0 : 1; const requiredIndex = isStatusPing ? 0 : 1;
if (currentLevelIndex() <= requiredIndex) { if (LEVEL_INDEX <= requiredIndex) {
console.log(`[API] Received ${request.method} request : ${request.url} at ${new Date()}`); console.log(`[API] Received ${request.method} request : ${request.url} at ${new Date()}`);
} }
} }