|
1 | 1 | # fastapi-observer |
2 | 2 |
|
3 | | -FastAPI Observer provides structured logging and observability helpers for FastAPI applications. |
| 3 | +Structured logging and observability middleware for FastAPI applications. |
4 | 4 |
|
5 | 5 | [](https://github.com/MehrazRumman/fastapi-observer/actions/workflows/tests.yml) |
6 | 6 | [](https://github.com/MehrazRumman/fastapi-observer/actions/workflows/coverage-badge.yml) |
7 | 7 | [](https://pypi.org/project/fastapi-observer/) |
8 | 8 | [](#python-compatibility) |
9 | 9 |
|
10 | | -## Python compatibility |
| 10 | +**[Documentation](https://mehrazrumman.github.io/fastapi-observer/)** · [Changelog](CHANGELOG.md) · [Contributing](CONTRIBUTING.md) |
11 | 11 |
|
12 | | -- Supported Python versions: `3.10`, `3.11`, `3.12`, `3.13`, `3.14` |
13 | | -- Enforced in CI with tox matrix: `py310`, `py311`, `py312`, `py313`, `py314` |
| 12 | +--- |
14 | 13 |
|
15 | | -## Package versions |
| 14 | +## Features |
16 | 15 |
|
17 | | -### Runtime |
| 16 | +- **Zero-boilerplate middleware** — one line to log every request and response |
| 17 | +- **Structured events** — typed `LogEvent` objects with method, path, status code, latency, and more |
| 18 | +- **Pluggable storage** — in-memory, JSON file, JSON Lines, or SQLite backends |
| 19 | +- **Filter pipeline** — drop health-check noise, log only errors, or set a minimum latency threshold |
| 20 | +- **Built-in dashboard** — mount an event-inspector UI at any path in your app |
| 21 | +- **Multiple formatters** — plain-text or JSON output |
| 22 | +- **Python 3.10–3.14** with full CI coverage across all versions |
18 | 23 |
|
19 | | -- `pydantic>=2.0,<3.0` |
| 24 | +--- |
20 | 25 |
|
21 | | -### Testing |
| 26 | +## Install |
22 | 27 |
|
23 | | -- `pytest>=8.0` |
24 | | -- `pytest-cov>=5.0` |
| 28 | +```bash |
| 29 | +pip install fastapi-observer |
| 30 | +``` |
25 | 31 |
|
26 | | -### Documentation |
| 32 | +--- |
27 | 33 |
|
28 | | -- `mkdocs>=1.6` |
29 | | -- `mkdocs-material>=9.5` |
| 34 | +## Quick start |
30 | 35 |
|
31 | | -### Development |
| 36 | +### Basic logging |
32 | 37 |
|
33 | | -- `pre-commit>=3.7` |
34 | | -- `black>=24.10` |
| 38 | +```python |
| 39 | +from fastapi import FastAPI |
| 40 | +from fastapi_observer import ObserverConfig, ObserverMiddleware |
35 | 41 |
|
36 | | -## Install |
| 42 | +app = FastAPI() |
| 43 | +app.add_middleware(ObserverMiddleware, config=ObserverConfig()) |
37 | 44 |
|
38 | | -```bash |
39 | | -pip install fastapi-observer |
| 45 | +@app.get("/items") |
| 46 | +async def list_items(): |
| 47 | + return {"items": ["alpha", "beta", "gamma"]} |
40 | 48 | ``` |
41 | 49 |
|
42 | | -For development: |
| 50 | +Every request is now logged to the console with method, path, status code, and latency. |
43 | 51 |
|
44 | | -```bash |
45 | | -pip install -e ".[test,docs,dev]" |
46 | | -``` |
| 52 | +### Persist events and open the dashboard |
47 | 53 |
|
48 | | -## Tests and coverage |
| 54 | +```python |
| 55 | +from fastapi import FastAPI |
| 56 | +from fastapi_observer import ObserverConfig, ObserverMiddleware, build_dashboard_app |
| 57 | +from fastapi_observer.storage import InMemoryEventStore |
49 | 58 |
|
50 | | -Run tests: |
| 59 | +app = FastAPI() |
| 60 | +store = InMemoryEventStore() |
51 | 61 |
|
52 | | -```bash |
53 | | -pytest -q |
| 62 | +app.add_middleware(ObserverMiddleware, config=ObserverConfig(), storage=store) |
| 63 | +app.mount("/dashboard", build_dashboard_app(store, title="Observer Dashboard")) |
54 | 64 | ``` |
55 | 65 |
|
56 | | -Run tests with coverage: |
| 66 | +Open `http://localhost:8000/dashboard` to browse and inspect logged events. |
57 | 67 |
|
58 | | -```bash |
59 | | -pytest -q --cov=fastapi_observer --cov-report=term-missing --cov-report=xml |
| 68 | +### Filter out noise |
| 69 | + |
| 70 | +```python |
| 71 | +from fastapi_observer import ObserverConfig, ObserverMiddleware, only_errors, min_duration_ms |
| 72 | + |
| 73 | +app.add_middleware( |
| 74 | + ObserverMiddleware, |
| 75 | + config=ObserverConfig(handlers=["console"], log_format="json"), |
| 76 | + event_filters=[ |
| 77 | + only_errors, # log 4xx/5xx responses only |
| 78 | + min_duration_ms(50.0), # ignore fast responses |
| 79 | + ], |
| 80 | +) |
60 | 81 | ``` |
61 | 82 |
|
62 | | -Run full Python version matrix locally: |
| 83 | +See the [examples/](examples/) directory for more patterns including SQLite storage and custom filters. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## Storage backends |
| 88 | + |
| 89 | +| Backend | Class | Use case | |
| 90 | +|---|---|---| |
| 91 | +| In-memory | `InMemoryEventStore` | Development, testing | |
| 92 | +| JSON file | `JsonFileEventStore` | Simple persistence | |
| 93 | +| JSON Lines | `JsonLinesEventStore` | Append-only log files | |
| 94 | +| SQLite | `SQLiteEventStore` | Queryable local storage | |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +## Python compatibility |
| 99 | + |
| 100 | +- Supported versions: `3.10`, `3.11`, `3.12`, `3.13`, `3.14` |
| 101 | +- Enforced in CI with a tox matrix across all versions |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## Development |
| 106 | + |
| 107 | +Install all dependencies: |
63 | 108 |
|
64 | 109 | ```bash |
65 | | -tox |
| 110 | +pip install -e ".[test,docs,dev]" |
| 111 | +pre-commit install |
66 | 112 | ``` |
67 | 113 |
|
68 | | -## Docs |
69 | | - |
70 | | -Build docs: |
| 114 | +Run tests: |
71 | 115 |
|
72 | 116 | ```bash |
73 | | -mkdocs build --strict |
| 117 | +pytest -q |
| 118 | +pytest -q --cov=fastapi_observer --cov-report=term-missing # with coverage |
| 119 | +tox # full version matrix |
74 | 120 | ``` |
75 | 121 |
|
76 | | -Serve docs locally: |
| 122 | +Build and preview docs: |
77 | 123 |
|
78 | 124 | ```bash |
79 | | -mkdocs serve |
| 125 | +mkdocs serve # http://127.0.0.1:8000 |
| 126 | +mkdocs build --strict |
80 | 127 | ``` |
| 128 | + |
| 129 | +See [CONTRIBUTING.md](CONTRIBUTING.md) for the full contribution guide. |
0 commit comments