From a8607785dc17ec3932210e19cb459d5877cb880b Mon Sep 17 00:00:00 2001 From: Joe Ziminski <55797454+JoeZiminski@users.noreply.github.com> Date: Thu, 2 May 2024 11:33:03 +0100 Subject: [PATCH] Don't propagate named loggers. (#32) * Don't propagate named loggers and add tests. * Tidy up test docstring. * Fix working of test name. --- fancylog/fancylog.py | 1 + tests/tests/test_general.py | 56 ++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/fancylog/fancylog.py b/fancylog/fancylog.py index 2ef48e1..156089a 100644 --- a/fancylog/fancylog.py +++ b/fancylog/fancylog.py @@ -244,6 +244,7 @@ def initialise_logger( if logger_name: logger = logging.getLogger(logger_name) logger.handlers = [] + logger.propagate = False else: logger = logging.getLogger() diff --git a/tests/tests/test_general.py b/tests/tests/test_general.py index 3f8b623..fe78753 100644 --- a/tests/tests/test_general.py +++ b/tests/tests/test_general.py @@ -42,6 +42,26 @@ def test_logger_name(tmp_path): assert logger_name in logging.root.manager.loggerDict +def test_logging_to_console(tmp_path, capsys): + """ + Check that logs are written to stdout when + `log_to_console` is `True`. + """ + logger_name = "hello_world" + + fancylog.start_logging( + tmp_path, fancylog, log_to_console=True, logger_name=logger_name + ) + + logger = logging.getLogger(logger_name) + + logger.debug("!!£%$") + + captured = capsys.readouterr() + + assert "!!£%$" in captured.out + + def test_correct_handlers_are_set(tmp_path): """ Test the handlers on the logger are as specified by the @@ -51,7 +71,7 @@ def test_correct_handlers_are_set(tmp_path): """ logger_name = "hello_world" - # Test no handlers are assigned when non requested + # Test no handlers are assigned when not requested fancylog.start_logging( tmp_path, fancylog, @@ -141,3 +161,37 @@ def test_handlers_are_refreshed(tmp_path): ) assert logger.handlers == [] + + +def test_named_logger_doesnt_propagate(tmp_path, capsys): + """ + By default, named loggers will propagate through + parent handlers. Root is always parent to named loggers. + This means that named logger can still print to console + through the root StreamHandler unless `propagate` is set + to `False`. Check here that propagate is set to False and + indeed named logger does not print to console. + """ + logger_name = "hello_world" + + fancylog.start_logging( + tmp_path, fancylog, logger_name=logger_name, log_to_console=False + ) + + logger = logging.getLogger(logger_name) + + assert logger.propagate is False + + logger.debug("XN$£ not in stdout") + + logging.debug("YYXX in stdout") + + logger.debug("PQ&* not in stdout") + + captured = capsys.readouterr() + + assert "XN$£" not in captured.out, "logger initially writing to stdout" + assert "YYXX" in captured.out, "root is not writing to stdout" + assert ( + "PQ&*" not in captured.out + ), "logger writing to stdout through root handler"