Structured logging and observability middleware for FastAPI applications.
Documentation · Changelog · Contributing
- Zero-boilerplate middleware — one line to log every request and response
- Structured events — typed
LogEventobjects with method, path, status code, latency, and more - Pluggable storage — in-memory, JSON file, JSON Lines, or SQLite backends
- Filter pipeline — drop health-check noise, log only errors, or set a minimum latency threshold
- Built-in dashboard — mount an event-inspector UI at any path in your app
- Multiple formatters — plain-text or JSON output
- Python 3.10–3.14 with full CI coverage across all versions
pip install fastapi-inspectorfrom fastapi import FastAPI
from fastapi_inspector import ObserverConfig, ObserverMiddleware
app = FastAPI()
app.add_middleware(ObserverMiddleware, config=ObserverConfig())
@app.get("/items")
async def list_items():
return {"items": ["alpha", "beta", "gamma"]}Every request is now logged to the console with method, path, status code, and latency.
from fastapi import FastAPI
from fastapi_inspector import ObserverConfig, ObserverMiddleware, build_dashboard_app
from fastapi_inspector.storage import InMemoryEventStore
app = FastAPI()
store = InMemoryEventStore()
app.add_middleware(ObserverMiddleware, config=ObserverConfig(), storage=store)
app.mount("/dashboard", build_dashboard_app(store, title="Observer Dashboard"))Open http://localhost:8000/dashboard to browse and inspect logged events.
from fastapi_inspector import ObserverConfig, ObserverMiddleware, only_errors, min_duration_ms
app.add_middleware(
ObserverMiddleware,
config=ObserverConfig(handlers=["console"], log_format="json"),
event_filters=[
only_errors, # log 4xx/5xx responses only
min_duration_ms(50.0), # ignore fast responses
],
)See the examples/ directory for more patterns including SQLite storage and custom filters.
| Backend | Class | Use case |
|---|---|---|
| In-memory | InMemoryEventStore |
Development, testing |
| JSON file | JsonFileEventStore |
Simple persistence |
| JSON Lines | JsonLinesEventStore |
Append-only log files |
| SQLite | SQLiteEventStore |
Queryable local storage |
- Supported versions:
3.10,3.11,3.12,3.13,3.14 - Enforced in CI with a tox matrix across all versions
Install all dependencies:
pip install -e ".[test,docs,dev]"
pre-commit installRun tests:
pytest -q
pytest -q --cov=fastapi_inspector --cov-report=term-missing # with coverage
tox # full version matrixBuild and preview docs:
mkdocs serve # http://127.0.0.1:8000
mkdocs build --strictSee CONTRIBUTING.md for the full contribution guide.