77
88
99class InterceptHandler (logging .Handler ):
10- def emit (self , record ) :
10+ def emit (self , record : logging . LogRecord ) -> None :
1111 # Get corresponding Loguru level if it exists.
1212 try :
1313 level = logger .level (record .levelname ).name
@@ -16,7 +16,12 @@ def emit(self, record):
1616
1717 # Find caller from where originated the logged message.
1818 frame , depth = inspect .currentframe (), 0
19- while frame and (depth == 0 or frame .f_code .co_filename == logging .__file__ ):
19+ while frame :
20+ filename = frame .f_code .co_filename
21+ is_logging = filename == logging .__file__
22+ is_frozen = "importlib" in filename and "_bootstrap" in filename
23+ if depth > 0 and not (is_logging or is_frozen ):
24+ break
2025 frame = frame .f_back
2126 depth += 1
2227
@@ -31,7 +36,7 @@ def test_formatting(writer):
3136
3237 expected = (
3338 "tests.test_interception - test_interception.py - test_formatting - DEBUG - "
34- "10 - 39 - test_interception - This is the message\n "
39+ "10 - 44 - test_interception - This is the message\n "
3540 )
3641
3742 with make_logging_logger ("tests" , InterceptHandler ()) as logging_logger :
@@ -158,4 +163,4 @@ def test_using_logging_function(writer):
158163 logging .warning ("ABC" )
159164
160165 result = writer .read ()
161- assert result == "test_using_logging_function 158 test_interception test_interception.py ABC\n "
166+ assert result == "test_using_logging_function 163 test_interception test_interception.py ABC\n "
0 commit comments