Add logging system: app.log + matching.log with admin viewer
- Backend: RotatingFileHandler for app.log (all) and matching.log (matcher/matching services)
- New GET/DELETE /api/logs/{app|matching} endpoints (admin-only)
- matcher.py: improved cover-download logging (URL, bytes, HTTP errors, missing cover URL)
- Frontend: Logs tab in Admin panel with log switcher, line count selector, color-coded ERROR/WARNING lines, clear button
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
57
backend/app/routers/logs.py
Normal file
57
backend/app/routers/logs.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import os
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from ..dependencies import require_admin
|
||||
from ..models.user import User
|
||||
from ..config import get_settings
|
||||
|
||||
router = APIRouter(prefix="/api/logs", tags=["logs"])
|
||||
|
||||
_LOG_FILES = {
|
||||
"app": "app.log",
|
||||
"matching": "matching.log",
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{log_name}")
|
||||
async def get_log(
|
||||
log_name: str,
|
||||
lines: int = Query(300, ge=10, le=5000),
|
||||
_admin: User = Depends(require_admin),
|
||||
):
|
||||
if log_name not in _LOG_FILES:
|
||||
raise HTTPException(status_code=404, detail="Unbekannte Log-Datei")
|
||||
|
||||
settings = get_settings()
|
||||
path = os.path.join(settings.log_dir, _LOG_FILES[log_name])
|
||||
|
||||
if not os.path.exists(path):
|
||||
return {"lines": [], "total": 0, "showing": 0, "exists": False, "path": path}
|
||||
|
||||
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
||||
all_lines = f.readlines()
|
||||
|
||||
last = all_lines[-lines:]
|
||||
return {
|
||||
"lines": [l.rstrip("\n") for l in last],
|
||||
"total": len(all_lines),
|
||||
"showing": len(last),
|
||||
"exists": True,
|
||||
"path": path,
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/{log_name}")
|
||||
async def clear_log(
|
||||
log_name: str,
|
||||
_admin: User = Depends(require_admin),
|
||||
):
|
||||
if log_name not in _LOG_FILES:
|
||||
raise HTTPException(status_code=404, detail="Unbekannte Log-Datei")
|
||||
|
||||
settings = get_settings()
|
||||
path = os.path.join(settings.log_dir, _LOG_FILES[log_name])
|
||||
|
||||
if os.path.exists(path):
|
||||
open(path, "w").close()
|
||||
|
||||
return {"success": True}
|
||||
Reference in New Issue
Block a user