Skip to main content

Async-safe structured JSON logging for FastAPI on SAP BTP

Reason this release was yanked:

Incorrect documentation

Project description

Here is a comprehensive, professional README.md for your package. I have designed it to be "documentation-ready" for PyPI and GitHub.

It clearly explains The Problem (Context Leakage), The Solution, and provides copy-paste examples for both standard usage and the background task wrapper.


SAP FastAPI Logger

Async-safe, structured JSON logging for FastAPI applications running on SAP BTP (Business Technology Platform).

This library solves the critical "Context Leakage" issue common when using standard logging libraries with ASGI frameworks like FastAPI. It ensures that Correlation IDs are correctly propagated across asynchronous requests and background tasks, making logs traceable in SAP Cloud Logging (Kibana).


🚨 The Problem

Standard Python logging libraries (including sap-cf-logging) rely on threading.local to store request context.

  • In Flask (WSGI): One request = One thread. This works fine.
  • In FastAPI (ASGI): One thread handles multiple concurrent requests via an event loop.

The Consequence: Using standard logging in FastAPI causes Context Leakage.

  1. Logs from User A appear with User B's Correlation ID.
  2. Correlation IDs are lost ("null") when await is called.
  3. Background tasks lose context entirely.

✅ The Solution

sap-fastapi-logger replaces thread-local storage with Python's native contextvars. This attaches context to the logical task rather than the thread, ensuring 100% async safety.

Features

  • 🚀 Zero Context Leakage: Uses contextvars for safe async context propagation.
  • 📊 SAP BTP Compatible: Outputs strict JSON format required by SAP Cloud Logging / Kibana (msg, written_at, correlation_id, level).
  • 🔗 Auto-Correlation: Automatically extracts X-CorrelationID from headers or generates a UUID.
  • Background Task Support: Includes a drop-in replacement for FastAPI's BackgroundTasks that preserves context.

📦 Installation

pip install sap-fastapi-logger

🚀 Quick Start

1. Basic Setup (in server.py or main.py)

You only need to do two things: initialize the logger and add the middleware.

import logging
from fastapi import FastAPI
from sap_fastapi_logger import setup_logging, CorrelationMiddleware

# 1. Initialize the JSON Logger (Run this before creating the app)
setup_logging()

app = FastAPI()

# 2. Add the Middleware to handle Correlation IDs
app.add_middleware(CorrelationMiddleware)

@app.get("/")
async def root():
    # Usage: Just use the standard python logger!
    logging.info("Processing request...") 
    return {"message": "Hello World"}

Output in Console (and Kibana):

{"level": "INFO", "msg": "Processing request...", "logger": "root", "correlation_id": "a1b2-c3d4-e5f6...", "written_at": "2025-01-01 12:00:00,000"}

⚡ Handling Background Tasks

Standard BackgroundTasks in FastAPI will lose the correlation ID because the middleware finishes and resets the context before the background task runs.

Use ContextAwareBackgroundTasks provided by this library. It automatically captures the ID from the request and injects it into the background thread.

Example

from fastapi import FastAPI
import logging
# Import the custom class
from sap_fastapi_logger import ContextAwareBackgroundTasks 

app = FastAPI()

def long_running_process(contract_id: str):
    # This log will HAVE the correct correlation ID
    logging.info(f"Processing contract {contract_id} in background...")

@app.post("/extract")
def extract_data(
    # Type hint using the custom class
    background_tasks: ContextAwareBackgroundTasks 
):
    logging.info("Request received. Starting background task.")
    
    # Use exactly like standard FastAPI BackgroundTasks
    background_tasks.add_task(long_running_process, contract_id="999")
    
    return {"message": "Processing started"}

🛠 Advanced Usage

Accessing the Correlation ID Manually

If you need to get the current Correlation ID string (e.g., to pass to a database query or an external API header), you can access the context variable directly.

from sap_fastapi_logger import correlation_id_ctx

@app.get("/debug")
def debug():
    current_id = correlation_id_ctx.get()
    return {"my_id": current_id}

How it works (Architecture)

  1. Middleware: Intercepts request Generates/Extracts ID Sets ContextVar.
  2. Formatter: logging calls Formatter Formatter reads ContextVar Injects into JSON.
  3. Background Tasks: ContextAwareBackgroundTasks snapshots ContextVar during request Re-hydrates it inside the worker thread.

📄 License

This project is licensed under the MIT License.

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

sap_fastapi_logger-0.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

sap_fastapi_logger-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file sap_fastapi_logger-0.1.0.tar.gz.

File metadata

  • Download URL: sap_fastapi_logger-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for sap_fastapi_logger-0.1.0.tar.gz
Algorithm Hash digest
SHA256 785e472526855ea79217b7a622f799e3508e6c103ae729758be4051340b0782b
MD5 3e1d64a8be3ea35e978f4fece3993ca1
BLAKE2b-256 1beab4a965078d584e030c2fd533273e0b073ebe155b507fe3f2712715f7ce76

See more details on using hashes here.

File details

Details for the file sap_fastapi_logger-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sap_fastapi_logger-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c8de9567d65b7a948448aa87869d0bd656289d478f7cf50d4609a9238ca3f8c
MD5 e89aca7e3b12f964dffe63998753be5b
BLAKE2b-256 7c29bd538f00c389bbcc64985a3100028f8a452594e65ca1fa8152ab4837f6ee

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