Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(logging): Include langfuse logger in JSON logging when langfuse callback is used (#19162)

Graham Neubig17f8916ce3d5
diff --git a/litellm/_logging.py b/litellm/_logging.py
index 73902d2fc5a..b3156b15ba7 100644
--- a/litellm/_logging.py
+++ b/litellm/_logging.py
@@ -133,6 +133,26 @@ ALL_LOGGERS = [
]
+def _get_loggers_to_initialize():
+ """
+ Get all loggers that should be initialized with the JSON handler.
+
+ Includes third-party integration loggers (like langfuse) if they are
+ configured as callbacks.
+ """
+ import litellm
+
+ loggers = list(ALL_LOGGERS)
+
+ # Add langfuse logger if langfuse is being used as a callback
+ langfuse_callbacks = {"langfuse", "langfuse_otel"}
+ all_callbacks = set(litellm.success_callback + litellm.failure_callback)
+ if langfuse_callbacks & all_callbacks:
+ loggers.append(logging.getLogger("langfuse"))
+
+ return loggers
+
+
def _initialize_loggers_with_handler(handler: logging.Handler):
"""
Initialize all loggers with a handler
@@ -140,7 +160,7 @@ def _initialize_loggers_with_handler(handler: logging.Handler):
- Adds a handler to each logger
- Prevents bubbling to parent/root (critical to prevent duplicate JSON logs)
"""
- for lg in ALL_LOGGERS:
+ for lg in _get_loggers_to_initialize():
lg.handlers.clear() # remove any existing handlers
lg.addHandler(handler) # add JSON formatter handler
lg.propagate = False # prevent bubbling to parent/root