RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Local AI APIs and Integration
  6. /Ch. 10
Local AI APIs and Integration

10. Request Logging

Chapter 10 of 18 · 15 min
KEY INSIGHT

Request logging transforms debugging from guesswork into scienceΓÇöevery HTTP method, path, status code, and timing metric tells a story about system behavior. Production APIs without logging are black boxes. When a client reports unexpected behavior, engineers without logs spend hours reproducing issues. Logs make debugging a deterministic process: examine the request sequence, identify anomalies, and trace problems to root causes. Structured logging in FastAPI requires the `logging` module configured with JSON output for machine parsing. A logger captures request metadata, response status codes, and timing information at minimum. Adding request body sampling (with size limits) enables deeper forensic analysis when failures occur. The logging middleware pattern wraps each request, capturing start time before the handler executes and computing elapsed time after completion. Context variables propagate request identifiers through nested function calls, ensuring logs can be correlated across distributed components. ```python import logging import time import uuid from contextvars import ContextVar from starlette.middleware.base import BaseHTTPMiddleware from starlette.requests import Request request_id_var: ContextVar[str] = ContextVar("request_id") logger = logging.getLogger("api.access") class AccessLoggingMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): request_id = request.headers.get("X-Request-ID", str(uuid.uuid4())) request_id_var.set(request_id) start_time = time.perf_counter() logger.info( "request_start", extra={ "request_id": request_id, "method": request.method, "path": request.url.path, "client": request.client.host if request.client else None, } ) response = await call_next(request) elapsed = (time.perf_counter() - start_time) * 1000 logger.info( "request_complete", extra={ "request_id": request_id, "status_code": response.status_code, "duration_ms": round(elapsed, 2), } ) response.headers["X-Request-ID"] = request_id return response ``` Failure modes include log explosion from verbose output, storage costs for retention policies, and sensitive data exposure in plaintext logs. Address these by establishing log levels (DEBUG for development, INFO for production), implementing PII filtering, and rotating logs with compression. Rotate logs daily or when exceeding size thresholds. Ship logs to a centralized system for aggregation and search. Retention policies balance investigative value against storage costsΓÇötypically 30 days for hot storage, 12 months for cold archive.

EXERCISE

Implement the access logging middleware in a FastAPI application. Configure log rotation using logging.handlers.RotatingFileHandler. Add filtering to redact email addresses from logged paths before storage.

← Chapter 9
Model Routing
Chapter 11 →
Error Responses