From cb22154b368e776067654eda5b7919044e9a791b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 18 May 2023 21:41:13 +0300 Subject: [PATCH] Capture warnings.warn and friends messages in logs (#85875) The default behavior of these warnings is to go to stderr, which in some setups goes easily unnoticed. For example in Docker based ones, they end up only in the container logs, and not e.g. in the HA log. Capture these to make them available in logs where other such messages are, and to make them subject to filtering as usual. https://docs.python.org/3/library/logging.html#logging.captureWarnings --- homeassistant/bootstrap.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 2077274be55..46bd4b5d881 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -380,6 +380,11 @@ def async_enable_logging( # formatting. If the above succeeds, this will result in a no-op. logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.INFO) + # Capture warnings.warn(...) and friends messages in logs. + # The standard destination for them is stderr, which may end up unnoticed. + # This way they're where other messages are, and can be filtered as usual. + logging.captureWarnings(True) + # Suppress overly verbose logs from libraries that aren't helpful logging.getLogger("requests").setLevel(logging.WARNING) logging.getLogger("urllib3").setLevel(logging.WARNING)