Skip to content

Commit a860778

Browse files
authored
Don't propagate named loggers. (#32)
* Don't propagate named loggers and add tests. * Tidy up test docstring. * Fix working of test name.
1 parent be62d33 commit a860778

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

fancylog/fancylog.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def initialise_logger(
244244
if logger_name:
245245
logger = logging.getLogger(logger_name)
246246
logger.handlers = []
247+
logger.propagate = False
247248
else:
248249
logger = logging.getLogger()
249250

tests/tests/test_general.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ def test_logger_name(tmp_path):
4242
assert logger_name in logging.root.manager.loggerDict
4343

4444

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+
4565
def test_correct_handlers_are_set(tmp_path):
4666
"""
4767
Test the handlers on the logger are as specified by the
@@ -51,7 +71,7 @@ def test_correct_handlers_are_set(tmp_path):
5171
"""
5272
logger_name = "hello_world"
5373

54-
# Test no handlers are assigned when non requested
74+
# Test no handlers are assigned when not requested
5575
fancylog.start_logging(
5676
tmp_path,
5777
fancylog,
@@ -141,3 +161,37 @@ def test_handlers_are_refreshed(tmp_path):
141161
)
142162

143163
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

Comments
 (0)