@@ -262,30 +262,18 @@ def configure_third_party_logging() -> None:
262262def setup_global_json_logging () -> None :
263263 """
264264 Set up global logging configuration to ensure all new loggers use JSON format.
265- This is a one-time setup that affects all future logger creation.
265+ This patches logging.Logger.addHandler so that any handler added by any library
266+ automatically gets the ThirdPartyJSONFormatter applied.
266267 """
267- # Store original getLogger function
268- original_getLogger = logging .getLogger
268+ original_add_handler = logging .Logger .addHandler
269269
270- def patched_getLogger (name : str | None = None ) -> logging .Logger :
271- """Patched getLogger that automatically applies JSON formatting to new loggers."""
272- logger = original_getLogger (name = name )
270+ def patched_add_handler (self : logging .Logger , hdlr : logging .Handler ) -> None :
271+ """Patched addHandler that automatically applies JSON formatting."""
272+ if not isinstance (hdlr .formatter , (ThirdPartyJSONFormatter , JSONOnlyFormatter )):
273+ hdlr .setFormatter (fmt = ThirdPartyJSONFormatter ())
274+ original_add_handler (self , hdlr ) # noqa: FCN001
273275
274- # Skip if this is our structlog wrapper or if already configured
275- if (name and ("opendatahub_logger" in name or "structlog" in name )) or hasattr (logger , "_json_configured" ):
276- return logger
277-
278- # Apply JSON formatter to any handlers this logger might have
279- for handler in logger .handlers :
280- if isinstance (handler .formatter , (logging .Formatter , type (None ))):
281- handler .setFormatter (fmt = ThirdPartyJSONFormatter ())
282-
283- # Mark as configured to avoid repeated processing
284- logger ._json_configured = True # type: ignore[attr-defined]
285- return logger
286-
287- # Patch the logging.getLogger function
288- logging .getLogger = patched_getLogger
276+ logging .Logger .addHandler = patched_add_handler # type: ignore[method-assign]
289277
290278
291279# Global flag to ensure we only set up global logging once
0 commit comments