From 19e7d306966dbd3d954269b413fa2d5d1192a915 Mon Sep 17 00:00:00 2001 From: Berry den Hartog <38954346+berrydenhartog@users.noreply.github.com> Date: Wed, 15 May 2024 14:16:04 +0000 Subject: [PATCH 1/2] Fix duplicate logging --- .vscode/launch.json | 4 ++-- sonar-project.properties | 2 +- tad/core/log.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 8446e32f..00ed195b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,8 +6,8 @@ "type": "debugpy", "request": "launch", "module": "uvicorn", - "justMyCode": true, - "args": ["tad.main:app"], + "justMyCode": false, + "args": [ "--log-level", "warning" ,"tad.main:app"], "cwd": "${workspaceFolder}/", "env": { "PYTHONPATH": "${workspaceFolder}" diff --git a/sonar-project.properties b/sonar-project.properties index 1ee082ff..f13b4244 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -17,4 +17,4 @@ sonar.python.version=3.10,3.11,3.12 sonar.python.coverage.reportPaths=coverage.xml -sonar.coverage.exclusions=tad/migrations/ +sonar.coverage.exclusions=tad/migrations/* diff --git a/tad/core/log.py b/tad/core/log.py index b0a98a29..7b719963 100644 --- a/tad/core/log.py +++ b/tad/core/log.py @@ -10,7 +10,7 @@ LOGGING_CONFIG = { "version": 1, - "disable_existing_loggers": False, + "disable_existing_loggers": True, "formatters": { "generic": { "()": "logging.Formatter", @@ -30,7 +30,7 @@ }, }, "loggers": { - "tad": {"handlers": ["console", "file"], "level": "DEBUG", "propagate": True}, + "tad": {"handlers": ["console", "file"], "level": "DEBUG", "propagate": False}, }, } From 0cee967bbb130cac6fd9380b32b8526724b94078 Mon Sep 17 00:00:00 2001 From: Berry den Hartog <38954346+berrydenhartog@users.noreply.github.com> Date: Wed, 15 May 2024 14:40:45 +0000 Subject: [PATCH 2/2] Enable log propagation for testing --- tad/core/log.py | 2 +- tests/core/test_log.py | 45 ++++++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/tad/core/log.py b/tad/core/log.py index 7b719963..4850c419 100644 --- a/tad/core/log.py +++ b/tad/core/log.py @@ -10,7 +10,7 @@ LOGGING_CONFIG = { "version": 1, - "disable_existing_loggers": True, + "disable_existing_loggers": False, "formatters": { "generic": { "()": "logging.Formatter", diff --git a/tests/core/test_log.py b/tests/core/test_log.py index 9cebc91a..4aae5629 100644 --- a/tests/core/test_log.py +++ b/tests/core/test_log.py @@ -6,8 +6,10 @@ from tad.core.log import configure_logging -def test_module_logging_setup(caplog: pytest.LogCaptureFixture): - configure_logging() +def test_logging_tad_module(caplog: pytest.LogCaptureFixture): + config = {"loggers": {"tad": {"propagate": True}}} + + configure_logging(config=config) logger = logging.getLogger("tad") @@ -22,7 +24,7 @@ def test_module_logging_setup(caplog: pytest.LogCaptureFixture): assert caplog.records[0].message == message -def test_root_logging_setup(caplog: pytest.LogCaptureFixture): +def test_logging_root(caplog: pytest.LogCaptureFixture): configure_logging() logger = logging.getLogger("") @@ -38,8 +40,10 @@ def test_root_logging_setup(caplog: pytest.LogCaptureFixture): assert caplog.records[0].message == message -def test_module_main_logging_setup(caplog: pytest.LogCaptureFixture): - configure_logging() +def test_logging_submodule(caplog: pytest.LogCaptureFixture): + config = {"loggers": {"tad": {"propagate": True}}} + + configure_logging(config=config) logger = logging.getLogger("tad.main") @@ -54,35 +58,46 @@ def test_module_main_logging_setup(caplog: pytest.LogCaptureFixture): assert caplog.records[0].message == message -def test_module_main_logging_with_custom_logging_setup(caplog: pytest.LogCaptureFixture): - configure_logging(level="ERROR") +def test_logging_config(caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv( + "LOGGING_CONFIG", + '{"loggers": { "tad": { "propagate": "True" }},"formatters": { "generic": { "fmt": "{name}: {message}"}}}', + ) - logger = logging.getLogger("tad.main") + settings = Settings() # type: ignore - message = "This is a test log message" + configure_logging(config=settings.LOGGING_CONFIG) + + logger = logging.getLogger("tad") + + message = "This is a test log message with other formatting" logger.debug(message) logger.info(message) logger.warning(message) logger.error(message) logger.critical(message) - assert len(caplog.records) == 2 + assert len(caplog.records) == 4 + +def test_logging_loglevel(caplog: pytest.LogCaptureFixture): + config = {"loggers": {"tad": {"propagate": True}}} -def test_enviroment_setup(caplog: pytest.LogCaptureFixture): - os.environ["LOGGING_CONFIG"] = '{"formatters": { "generic": { "fmt": "{name}: {message}"}}}' + configure_logging(config=config) + + os.environ["LOGGING_LEVEL"] = "ERROR" settings = Settings() # type: ignore - configure_logging(config=settings.LOGGING_CONFIG) + configure_logging(config=config, level=settings.LOGGING_LEVEL) logger = logging.getLogger("tad.main") - message = "This is a test log message with other formatting" + message = "This is a test log message with different logging level" logger.debug(message) logger.info(message) logger.warning(message) logger.error(message) logger.critical(message) - assert len(caplog.records) == 4 + assert len(caplog.records) == 2