feat: add detailed logging to all API routes and global access log middleware
Each API module now logs: - evaluations: trigger (scenario path, task_id), status polls, list - runs: list (count), detail (run_id, metrics, sample counts) - scenarios: list (total, valid, error counts) - pipeline: submit (docs_path, models, max_docs), status polls, list - llm_profiles: CRUD ops (name, model, id), probe/test (model, ok, latency), apply (patched fields) - score: already had per-request logging Global middleware (webapp.access logger): - Every API request: METHOD path -> status (latency_ms) at INFO - Static file requests demoted to DEBUG to reduce noise Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from webapp.models import (
|
||||
@@ -13,19 +15,23 @@ from webapp.services import scenario_scanner
|
||||
from webapp.services.task_manager import task_manager
|
||||
|
||||
router = APIRouter(prefix="/api/evaluations", tags=["evaluations"])
|
||||
logger = logging.getLogger("webapp.api.evaluations")
|
||||
|
||||
|
||||
@router.post("", response_model=TriggerEvaluationResponse)
|
||||
def trigger_evaluation(request: TriggerEvaluationRequest) -> TriggerEvaluationResponse:
|
||||
"""Validate the scenario path and queue a background evaluation task."""
|
||||
logger.info("[trigger] scenario=%s", request.scenario_path)
|
||||
resolved = scenario_scanner.resolve_scenario_path(request.scenario_path)
|
||||
if resolved is None:
|
||||
logger.warning("[trigger] invalid scenario path: %s", request.scenario_path)
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"无效或不允许的场景路径: {request.scenario_path}",
|
||||
)
|
||||
|
||||
task_id = task_manager.submit(request.scenario_path)
|
||||
logger.info("[trigger] queued task_id=%s scenario=%s", task_id, request.scenario_path)
|
||||
return TriggerEvaluationResponse(task_id=task_id)
|
||||
|
||||
|
||||
@@ -34,11 +40,15 @@ def get_task_status(task_id: str) -> TaskStatus:
|
||||
"""Return the current status and logs for one evaluation task."""
|
||||
status = task_manager.get(task_id)
|
||||
if status is None:
|
||||
logger.warning("[task_status] not found task_id=%s", task_id)
|
||||
raise HTTPException(status_code=404, detail=f"未找到任务: {task_id}")
|
||||
logger.debug("[task_status] task_id=%s status=%s", task_id, status.status)
|
||||
return status
|
||||
|
||||
|
||||
@router.get("", response_model=dict)
|
||||
def list_tasks() -> dict[str, list]:
|
||||
"""Return all known evaluation tasks for this server session."""
|
||||
return {"tasks": [task.model_dump() for task in task_manager.list_tasks()]}
|
||||
tasks = task_manager.list_tasks()
|
||||
logger.info("[list_tasks] count=%d", len(tasks))
|
||||
return {"tasks": [task.model_dump() for task in tasks]}
|
||||
|
||||
Reference in New Issue
Block a user