Correlation ID middleware and structured logging for FastAPI — zero project-specific dependencies
Project description
fastapi-correlation
📢 Hobby Project Notice: This is a research and learning project exploring FastAPI middleware and structured logging best practices. Feel free to use it as a reference, report issues, or suggest improvements! Contributions and feedback are always welcome.
Correlation ID middleware and structured logging for FastAPI — zero project-specific dependencies (only Starlette).
Features
CorrelationIDMiddleware— injects a uniqueX-Correlation-IDheader per request (reads the incoming header if present, generates a UUID4 otherwise)get_correlation_id()— context-var accessor usable anywhere in the request lifecycleLoggingContextMiddleware— injectsendpoint,method,status_code, anduser_idinto every log record for the duration of the requestset_log_context/get_log_context/clear_log_context— helpers for enriching per-request structured log fieldsStructuredJSONFormatter— RFC 3339 UTC JSON output ready for Loki / Grafana AlloyHumanReadableFormatter— compact, coloured output for local development
Installation
pip install fastapi-correlation
Quick start
from fastapi import FastAPI
from fastapi_correlation import (
CorrelationIDMiddleware,
LoggingContextMiddleware,
StructuredJSONFormatter,
set_log_context,
get_correlation_id,
)
import logging, sys
# Wire up structured JSON logging
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(StructuredJSONFormatter())
logging.getLogger().addHandler(handler)
app = FastAPI()
app.add_middleware(LoggingContextMiddleware)
app.add_middleware(CorrelationIDMiddleware)
@app.get("/ping")
async def ping():
set_log_context(custom_field="hello")
return {"correlation_id": get_correlation_id()}
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_correlation-0.0.2.tar.gz.
File metadata
- Download URL: fastapi_correlation-0.0.2.tar.gz
- Upload date:
- Size: 47.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"openSUSE Tumbleweed","version":"20260223","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed90e8c6a9e1e4b6c12cc403da3dac85ad3caa0fd569bba27ae92c4cf38a01f2
|
|
| MD5 |
638c41f785baee4e18449fc559160912
|
|
| BLAKE2b-256 |
acbc1e41c3ec434a6c46f2f3ea4739021c462589e06e9a7a1e9d9e7aa9cfb772
|
File details
Details for the file fastapi_correlation-0.0.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_correlation-0.0.2-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"openSUSE Tumbleweed","version":"20260223","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0a069b0c1f6ffa7b51e55f730fc4d90321bd05068c3a7e30e0e9fb43eb2f77c
|
|
| MD5 |
fbf19168876cf06d997c969798acda08
|
|
| BLAKE2b-256 |
8ead568c3db7ba0fa1723291ef3f267a6e88f82dbf2765e3244322ac3d01ae87
|