mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
fix: cache log level index at module init in loggingMiddleware
This commit is contained in:
@@ -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()}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user