From a6743f8219e2717dfc893d239e173488a4ac4ffc Mon Sep 17 00:00:00 2001 From: kk Date: Sun, 9 Oct 2022 00:10:35 +0800 Subject: [PATCH] Set `propagate` to `False` on "uvicorn" logger (#1288) --- tests/conftest.py | 11 +++++++++++ uvicorn/config.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index f5313cea33..64e9721070 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,6 +15,17 @@ from uvicorn.config import LOGGING_CONFIG +# Note: We explicitly turn the propagate on just for tests, because pytest +# caplog not able to capture no-propagate loggers. +# +# And the caplog_for_logger helper also not work on test config cases, because +# when create Config object, Config.configure_logging will remove caplog.handler. +# +# The simple solution is set propagate=True before execute tests. +# +# See also: https://github.com/pytest-dev/pytest/issues/3697 +LOGGING_CONFIG["loggers"]["uvicorn"]["propagate"] = True + @pytest.fixture def tls_certificate_authority() -> trustme.CA: diff --git a/uvicorn/config.py b/uvicorn/config.py index ea7888aee0..589d72d375 100644 --- a/uvicorn/config.py +++ b/uvicorn/config.py @@ -118,7 +118,7 @@ }, }, "loggers": { - "uvicorn": {"handlers": ["default"], "level": "INFO"}, + "uvicorn": {"handlers": ["default"], "level": "INFO", "propagate": False}, "uvicorn.error": {"level": "INFO"}, "uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False}, },