@@ -842,30 +842,61 @@ def test_initialize_components_kwargs(
842
842
843
843
def test_basicConfig_works_with_otel_handler (self ):
844
844
with ClearLoggingHandlers ():
845
- # Initialize auto-instrumentation with logging enabled
846
845
_init_logging (
847
846
{"otlp" : DummyOTLPLogExporter },
848
847
Resource .create ({}),
849
848
setup_logging_handler = True ,
850
849
)
851
850
852
- # Call basicConfig - this should work despite OTel handler being present
851
+ # Call the patched basicConfig
853
852
logging .basicConfig (level = logging .INFO )
854
853
855
- # Verify a StreamHandler was added
856
854
root_logger = logging .getLogger ()
857
855
stream_handlers = [
858
856
h
859
857
for h in root_logger .handlers
860
858
if isinstance (h , logging .StreamHandler )
861
- and not isinstance (h , LoggingHandler )
862
859
]
863
860
self .assertEqual (
864
861
len (stream_handlers ),
865
862
1 ,
866
863
"basicConfig should add a StreamHandler even when OTel handler exists" ,
867
864
)
868
865
866
+ def test_basicConfig_preserves_otel_handler (self ):
867
+ with ClearLoggingHandlers ():
868
+ _init_logging (
869
+ {"otlp" : DummyOTLPLogExporter },
870
+ Resource .create ({}),
871
+ setup_logging_handler = True ,
872
+ )
873
+
874
+ root_logger = logging .getLogger ()
875
+ self .assertEqual (
876
+ len (root_logger .handlers ),
877
+ 1 ,
878
+ "Should be exactly one OpenTelemetry LoggingHandler" ,
879
+ )
880
+ handler = root_logger .handlers [0 ]
881
+ self .assertIsInstance (handler , LoggingHandler )
882
+
883
+ logging .basicConfig ()
884
+
885
+ # Assert that we have a new handler
886
+ self .assertGreater (len (root_logger .handlers ), 1 )
887
+
888
+ # And that our logging handler is still there
889
+ logging_handlers = [
890
+ h
891
+ for h in root_logger .handlers
892
+ if isinstance (h , LoggingHandler )
893
+ ]
894
+ self .assertEqual (
895
+ len (logging_handlers ),
896
+ 1 ,
897
+ "Should still have exactly one OpenTelemetry LoggingHandler" ,
898
+ )
899
+
869
900
870
901
class TestMetricsInit (TestCase ):
871
902
def setUp (self ):
0 commit comments