File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
packages/service-library/tests Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change 11# pylint:disable=redefined-outer-name
22
33import logging
4+ from collections .abc import Iterable
45from contextlib import suppress
56from pathlib import Path
67from typing import Any
@@ -244,14 +245,33 @@ def test_log_context(
244245 assert len (caplog .messages ) == 2
245246
246247
248+ @pytest .fixture
249+ def log_format_with_module_name () -> Iterable [None ]:
250+ for handler in logging .root .handlers :
251+ original_formatter = handler .formatter
252+ handler .setFormatter (
253+ logging .Formatter (
254+ "%(asctime)s %(levelname)s %(module)s:%(filename)s:%(lineno)d %(message)s" ,
255+ datefmt = "%Y-%m-%d %H:%M:%S" ,
256+ )
257+ )
258+
259+ yield
260+
261+ for handler in logging .root .handlers :
262+ handler .formatter = original_formatter
263+
264+
247265def test_log_context_caller_is_included_in_log (
248266 caplog : pytest .LogCaptureFixture ,
267+ log_format_with_module_name : None ,
249268):
250269 caplog .clear ()
251270
252271 with log_context (_logger , logging .ERROR , "a test message" ):
253272 ...
254273
274+ # Verify file name is in the log
255275 assert Path (__file__ ).name in caplog .text
256276
257277
You can’t perform that action at this time.
0 commit comments