Add backend log management

This commit is contained in:
inman
2026-06-03 11:49:45 +08:00
parent 13ddc66cfe
commit fb0229ba06
14 changed files with 804 additions and 2 deletions

40
app/api/logs/route.ts Normal file
View File

@@ -0,0 +1,40 @@
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";
}