Skip to content

Commit 17f8916

Browse files
fix(logging): Include langfuse logger in JSON logging when langfuse callback is used (BerriAI#19162)
When JSON_LOGS is enabled and langfuse is configured as a success/failure callback, the langfuse logger now receives the JSON formatter. This ensures langfuse SDK log messages (like 'Item exceeds size limit' warnings) are output as JSON with proper level information, instead of plain text that log aggregators may incorrectly classify as errors. Fixes issue where langfuse warnings appeared as errors in Datadog due to missing log level in unformatted output. Co-authored-by: openhands <openhands@all-hands.dev>
1 parent 2c75194 commit 17f8916

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

litellm/_logging.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,34 @@ def _suppress_loggers():
133133
]
134134

135135

136+
def _get_loggers_to_initialize():
137+
"""
138+
Get all loggers that should be initialized with the JSON handler.
139+
140+
Includes third-party integration loggers (like langfuse) if they are
141+
configured as callbacks.
142+
"""
143+
import litellm
144+
145+
loggers = list(ALL_LOGGERS)
146+
147+
# Add langfuse logger if langfuse is being used as a callback
148+
langfuse_callbacks = {"langfuse", "langfuse_otel"}
149+
all_callbacks = set(litellm.success_callback + litellm.failure_callback)
150+
if langfuse_callbacks & all_callbacks:
151+
loggers.append(logging.getLogger("langfuse"))
152+
153+
return loggers
154+
155+
136156
def _initialize_loggers_with_handler(handler: logging.Handler):
137157
"""
138158
Initialize all loggers with a handler
139159
140160
- Adds a handler to each logger
141161
- Prevents bubbling to parent/root (critical to prevent duplicate JSON logs)
142162
"""
143-
for lg in ALL_LOGGERS:
163+
for lg in _get_loggers_to_initialize():
144164
lg.handlers.clear() # remove any existing handlers
145165
lg.addHandler(handler) # add JSON formatter handler
146166
lg.propagate = False # prevent bubbling to parent/root

0 commit comments

Comments
 (0)