9
9
10
10
logger = logging .getLogger (__name__ )
11
11
12
+ _NOT_FORMATTED_ASSERTION_MESSAGE = "This should not have been formatted as a string."
13
+
12
14
13
15
def test_log_kwargs () -> None :
14
16
"""Test that objects included via keyword args are formatted."""
@@ -36,12 +38,11 @@ def test_lambda() -> None:
36
38
37
39
def test_lazy_object () -> None :
38
40
"""Test that formatting of objects are done lazily and not when the logging level is not appropriate."""
39
- assertion_message = "This should not have been formatted as a string."
40
41
41
42
class _NotFormattedObject :
42
43
@override
43
44
def __repr__ (self ) -> str :
44
- raise AssertionError (assertion_message )
45
+ raise AssertionError (_NOT_FORMATTED_ASSERTION_MESSAGE )
45
46
46
47
# Logging level is INFO, so DEBUG messages shouldn't be logged / formatted.
47
48
recorded_logger : logging .Logger
@@ -51,14 +52,35 @@ def __repr__(self) -> str:
51
52
52
53
53
54
def test_lazy_lambda () -> None :
54
- """Test that a lambda input is not evaluated when the logging level is not appropriate."""
55
- assertion_message = "This should not have been formatted as a string."
55
+ """Test that a lambda message is not evaluated when the logging level is not appropriate."""
56
56
57
57
def _should_not_be_called () -> str :
58
- raise AssertionError (assertion_message )
58
+ raise AssertionError (_NOT_FORMATTED_ASSERTION_MESSAGE )
59
59
60
60
# Logging level is INFO, so DEBUG messages shouldn't be logged / formatted.
61
61
recorded_logger : logging .Logger
62
62
handler : RecordingLogHandler
63
63
with recorded_logging_context (logger , logging .INFO ) as (recorded_logger , handler ):
64
64
recorded_logger .debug (LazyFormat (lambda : f"{ _should_not_be_called ()} " ))
65
+
66
+
67
+ def test_lambda_argument () -> None :
68
+ """Tests that a callable that's supplied as an argument value is evaluated."""
69
+ recorded_logger : logging .Logger
70
+ handler : RecordingLogHandler
71
+ with recorded_logging_context (logger , logging .INFO ) as (recorded_logger , handler ):
72
+ recorded_logger .info (LazyFormat ("Example message" , arg_0 = lambda : 1 ))
73
+ assert handler .get_last_message () == "Example message (arg_0=1)"
74
+
75
+
76
+ def test_lazy_lambda_argument () -> None :
77
+ """Test that a lambda input is not evaluated when the logging level is not appropriate."""
78
+
79
+ def _should_not_be_called () -> str :
80
+ raise AssertionError (_NOT_FORMATTED_ASSERTION_MESSAGE )
81
+
82
+ # Logging level is INFO, so DEBUG messages shouldn't be logged / formatted.
83
+ recorded_logger : logging .Logger
84
+ handler : RecordingLogHandler
85
+ with recorded_logging_context (logger , logging .INFO ) as (recorded_logger , handler ):
86
+ recorded_logger .debug (LazyFormat ("Example message" , arg_0 = lambda : f"{ _should_not_be_called ()} " ))
0 commit comments