File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed
src/opentelemetry/sdk/_configuration Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -253,6 +253,37 @@ def _init_logging(
253253 set_event_logger_provider (event_logger_provider )
254254
255255 if setup_logging_handler :
256+ # noinspection PyPep8Naming
257+ original_basicConfig = logging .basicConfig
258+
259+ # noinspection PyPep8Naming
260+ def patched_basicConfig (* args , ** kwargs ):
261+ # Get the root logger
262+ root = logging .getLogger ()
263+
264+ # Check if the only handler is our OTel handler
265+ has_only_otel = len (root .handlers ) == 1 and isinstance (
266+ root .handlers [0 ], LoggingHandler
267+ )
268+
269+ if has_only_otel :
270+ # Temporarily remove OTel handler
271+ otel_handler = root .handlers [0 ]
272+ root .handlers = []
273+
274+ # Call original basicConfig
275+ original_basicConfig (* args , ** kwargs )
276+
277+ # Add OTel handler back
278+ root .addHandler (otel_handler )
279+ else :
280+ # Normal behavior
281+ original_basicConfig (* args , ** kwargs )
282+
283+ # Apply the monkey patch
284+ logging .basicConfig = patched_basicConfig
285+
286+ # Add OTel handler
256287 handler = LoggingHandler (
257288 level = logging .NOTSET , logger_provider = provider
258289 )
Original file line number Diff line number Diff line change @@ -842,6 +842,32 @@ def test_initialize_components_kwargs(
842842 True ,
843843 )
844844
845+ def test_basicConfig_works_with_otel_handler (self ):
846+ with ClearLoggingHandlers ():
847+ # Initialize auto-instrumentation with logging enabled
848+ _init_logging (
849+ {"otlp" : DummyOTLPLogExporter },
850+ Resource .create ({}),
851+ setup_logging_handler = True ,
852+ )
853+
854+ # Call basicConfig - this should work despite OTel handler being present
855+ logging .basicConfig (level = logging .INFO )
856+
857+ # Verify a StreamHandler was added
858+ root_logger = logging .getLogger ()
859+ stream_handlers = [
860+ h
861+ for h in root_logger .handlers
862+ if isinstance (h , logging .StreamHandler )
863+ and not isinstance (h , LoggingHandler )
864+ ]
865+ self .assertEqual (
866+ len (stream_handlers ),
867+ 1 ,
868+ "basicConfig should add a StreamHandler even when OTel handler exists" ,
869+ )
870+
845871
846872class TestMetricsInit (TestCase ):
847873 def setUp (self ):
You can’t perform that action at this time.
0 commit comments