Skip to main content

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 unique X-Correlation-ID header per request (reads the incoming header if present, generates a UUID4 otherwise)
  • get_correlation_id() — context-var accessor usable anywhere in the request lifecycle
  • LoggingContextMiddleware — injects endpoint, method, status_code, and user_id into every log record for the duration of the request
  • set_log_context / get_log_context / clear_log_context — helpers for enriching per-request structured log fields
  • StructuredJSONFormatter — RFC 3339 UTC JSON output ready for Loki / Grafana Alloy
  • HumanReadableFormatter — 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_correlation-0.0.2.tar.gz (47.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_correlation-0.0.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

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

Hashes for fastapi_correlation-0.0.2.tar.gz
Algorithm Hash digest
SHA256 ed90e8c6a9e1e4b6c12cc403da3dac85ad3caa0fd569bba27ae92c4cf38a01f2
MD5 638c41f785baee4e18449fc559160912
BLAKE2b-256 acbc1e41c3ec434a6c46f2f3ea4739021c462589e06e9a7a1e9d9e7aa9cfb772

See more details on using hashes here.

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

Hashes for fastapi_correlation-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c0a069b0c1f6ffa7b51e55f730fc4d90321bd05068c3a7e30e0e9fb43eb2f77c
MD5 fbf19168876cf06d997c969798acda08
BLAKE2b-256 8ead568c3db7ba0fa1723291ef3f267a6e88f82dbf2765e3244322ac3d01ae87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page