import { jsonError, jsonOk } from "@/lib/server/api"; import { appLogFilePath, appLogMaxBytes, clearAppLogs, listAppLogs, type AppLogLevel } from "@/lib/server/log-manager"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; export async function GET(request: Request) { try { const url = new URL(request.url); const level = parseLevel(url.searchParams.get("level")); const q = url.searchParams.get("q") || undefined; const limit = Number(url.searchParams.get("limit") || 100); const entries = await listAppLogs({ level, q, limit: Number.isFinite(limit) ? limit : 100 }); return jsonOk({ entries, logPath: appLogFilePath(), maxBytes: appLogMaxBytes() }); } catch (error) { return jsonError(error, 500, { request, source: "api.logs" }); } } export async function DELETE(request: Request) { try { await clearAppLogs(); return jsonOk({ ok: true }); } catch (error) { return jsonError(error, 500, { request, source: "api.logs" }); } } function parseLevel(value: string | null): AppLogLevel | "all" { if (value === "info" || value === "warning" || value === "error") return value; return "all"; }