@@ -42,6 +42,26 @@ def test_logger_name(tmp_path):
42
42
assert logger_name in logging .root .manager .loggerDict
43
43
44
44
45
+ def test_logging_to_console (tmp_path , capsys ):
46
+ """
47
+ Check that logs are written to stdout when
48
+ `log_to_console` is `True`.
49
+ """
50
+ logger_name = "hello_world"
51
+
52
+ fancylog .start_logging (
53
+ tmp_path , fancylog , log_to_console = True , logger_name = logger_name
54
+ )
55
+
56
+ logger = logging .getLogger (logger_name )
57
+
58
+ logger .debug ("!!£%$" )
59
+
60
+ captured = capsys .readouterr ()
61
+
62
+ assert "!!£%$" in captured .out
63
+
64
+
45
65
def test_correct_handlers_are_set (tmp_path ):
46
66
"""
47
67
Test the handlers on the logger are as specified by the
@@ -51,7 +71,7 @@ def test_correct_handlers_are_set(tmp_path):
51
71
"""
52
72
logger_name = "hello_world"
53
73
54
- # Test no handlers are assigned when non requested
74
+ # Test no handlers are assigned when not requested
55
75
fancylog .start_logging (
56
76
tmp_path ,
57
77
fancylog ,
@@ -141,3 +161,37 @@ def test_handlers_are_refreshed(tmp_path):
141
161
)
142
162
143
163
assert logger .handlers == []
164
+
165
+
166
+ def test_named_logger_doesnt_propagate (tmp_path , capsys ):
167
+ """
168
+ By default, named loggers will propagate through
169
+ parent handlers. Root is always parent to named loggers.
170
+ This means that named logger can still print to console
171
+ through the root StreamHandler unless `propagate` is set
172
+ to `False`. Check here that propagate is set to False and
173
+ indeed named logger does not print to console.
174
+ """
175
+ logger_name = "hello_world"
176
+
177
+ fancylog .start_logging (
178
+ tmp_path , fancylog , logger_name = logger_name , log_to_console = False
179
+ )
180
+
181
+ logger = logging .getLogger (logger_name )
182
+
183
+ assert logger .propagate is False
184
+
185
+ logger .debug ("XN$£ not in stdout" )
186
+
187
+ logging .debug ("YYXX in stdout" )
188
+
189
+ logger .debug ("PQ&* not in stdout" )
190
+
191
+ captured = capsys .readouterr ()
192
+
193
+ assert "XN$£" not in captured .out , "logger initially writing to stdout"
194
+ assert "YYXX" in captured .out , "root is not writing to stdout"
195
+ assert (
196
+ "PQ&*" not in captured .out
197
+ ), "logger writing to stdout through root handler"
0 commit comments