diff --git a/tests/processors/test_processors.py b/tests/processors/test_processors.py index e43485e..99516b4 100644 --- a/tests/processors/test_processors.py +++ b/tests/processors/test_processors.py @@ -36,6 +36,7 @@ from structlog.processors import ( format_exc_info, ) from structlog.stdlib import ProcessorFormatter +from structlog.testing import CapturingLoggerFactory from structlog.typing import EventDict, ExcInfo from ..additional_frame import additional_frame @@ -378,6 +379,7 @@ class TestCallsiteParameterAdder: async def test_async(self, wrapper_class, method_name) -> None: """ Callsite information for async invocations are correct. + Thread information is now correctly captured before async bridge. """ string_io = StringIO() @@ -395,17 +397,56 @@ class TestCallsiteParameterAdder: logger = structlog.stdlib.get_logger() + # Capture thread info before async call + expected_thread = threading.get_ident() + expected_thread_name = threading.current_thread().name + callsite_params = self.get_callsite_parameters() await getattr(logger, method_name)("baz") logger_params = json.loads(string_io.getvalue()) - # These are different when running under async + assert expected_thread == logger_params["thread"] + assert expected_thread_name == logger_params["thread_name"] + + # Remove thread info from comparison for remaining params for key in ["thread", "thread_name"]: callsite_params.pop(key) logger_params.pop(key) assert {"event": "baz", **callsite_params} == logger_params + @pytest.mark.asyncio + async def test_async_native_logger(self) -> None: + """ + Callsite thread information for native async invocations is correct. + """ + cf = CapturingLoggerFactory() + structlog.configure( + processors=[ + CallsiteParameterAdder( + parameters=[ + CallsiteParameter.THREAD, + CallsiteParameter.THREAD_NAME, + ] + ), + ], + logger_factory=cf, + wrapper_class=structlog._native.BoundLoggerFilteringAtInfo, + cache_logger_on_first_use=True, + ) + + logger = structlog.get_logger() + + expected_thread = threading.get_ident() + expected_thread_name = threading.current_thread().name + + await logger.ainfo("test native async") + + captured = cf.logger.calls[0].kwargs + + assert expected_thread == captured["thread"] + assert expected_thread_name == captured["thread_name"] + def test_additional_ignores(self, monkeypatch: pytest.MonkeyPatch) -> None: """ Stack frames from modules with names that start with values in