File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed
src/opentelemetry/sdk/_logs/_internal Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 1919import concurrent .futures
2020import json
2121import logging
22+ import sys
2223import threading
2324import traceback
2425import warnings
@@ -568,8 +569,12 @@ def _get_attributes(record: logging.LogRecord) -> _ExtendedAttributes:
568569 attributes [code_attributes .CODE_FUNCTION_NAME ] = record .funcName
569570 attributes [code_attributes .CODE_LINE_NUMBER ] = record .lineno
570571
572+ if isinstance (record .exc_info , str ):
573+ record .exc_info = sys .exc_info ()
574+
571575 if record .exc_info :
572576 exctype , value , tb = record .exc_info
577+
573578 if exctype is not None :
574579 attributes [exception_attributes .EXCEPTION_TYPE ] = (
575580 exctype .__name__
Original file line number Diff line number Diff line change 1414
1515import logging
1616import os
17+ import sys
1718import unittest
1819from unittest .mock import Mock , patch
1920
@@ -48,6 +49,20 @@ def test_handler_default_log_level(self):
4849 logger .warning ("Warning message" )
4950 self .assertEqual (processor .emit_count (), 1 )
5051
52+ def test_handler_error_exc_info (self ):
53+ processor , logger = set_up_test_logging (logging .NOTSET )
54+ exc_info_values = [
55+ # Don't know what caused it in my context, so I'm relying on mocks to replicate the behavior.
56+ # First the `record.exc_info` becomes a string somehow, then `sys.exc_info` brings the tuple.
57+ "Stringified exception" ,
58+ (None , None , None ),
59+ ]
60+
61+ with patch .object (sys , "exc_info" , side_effect = exc_info_values ):
62+ logger .exception ("Exception message" ) # Should not raise exception
63+
64+ assert processor .emit_count () == 1
65+
5166 def test_handler_custom_log_level (self ):
5267 processor , logger = set_up_test_logging (logging .ERROR )
5368
You can’t perform that action at this time.
0 commit comments