Skip to content

Loguru Coexistence Guide

This guide shows how to keep existing loguru call sites while routing logs through fastapi-observer for: - request/trace correlation - redaction/sanitization - bounded queue handling - sink circuit-breaker protection

Install

pip install "fastapi-observer[loguru]"

Use one logging pipeline:

loguru calls -> bridge -> stdlib root logger -> fastapi-observer queue/sinks

This avoids duplicated sink config and keeps security policy enforcement in one place.

Minimal Setup

from fastapi import FastAPI
from loguru import logger as loguru_logger

from fastapiobserver import (
    ObservabilitySettings,
    install_loguru_bridge,
    install_observability,
)

app = FastAPI()

settings = ObservabilitySettings(
    app_name="orders-api",
    service="orders",
    environment="production",
)
install_observability(app, settings)

# Forward all Loguru records into fastapi-observer's stdlib pipeline.
bridge_id = install_loguru_bridge()

loguru_logger.bind(event={"component": "checkout"}).info("payment.started")

Structured Context

loguru bound fields are forwarded as stdlib extra fields.
If you bind an event dict, it is preserved by StructuredJsonFormatter.

loguru_logger.bind(
    request_id="req-123",
    event={"order_id": "ord-42", "phase": "validate"},
).info("order.validation")

Removing the Bridge

from fastapiobserver.loguru import remove_loguru_bridge

remove_loguru_bridge(bridge_id)

Loop-Safety Rule

Do not install both directions at the same time: - Loguru -> stdlib bridge (this guide) - stdlib -> Loguru intercept

Running both in one process can create recursive log loops.