From 4efada7db0eaffe18cf5bd576c53ccc7c82f05a2 Mon Sep 17 00:00:00 2001 From: uvjustin <46082645+uvjustin@users.noreply.github.com> Date: Tue, 22 Feb 2022 00:58:15 +0800 Subject: [PATCH] Allow stream log level to change at runtime (#66153) --- homeassistant/components/stream/__init__.py | 4 +- homeassistant/components/stream/manifest.json | 3 +- tests/components/stream/test_init.py | 46 +++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 tests/components/stream/test_init.py diff --git a/homeassistant/components/stream/__init__.py b/homeassistant/components/stream/__init__.py index 66929fff79c..e22e06df7e2 100644 --- a/homeassistant/components/stream/__init__.py +++ b/homeassistant/components/stream/__init__.py @@ -120,10 +120,8 @@ CONFIG_SCHEMA = vol.Schema( def filter_libav_logging() -> None: """Filter libav logging to only log when the stream logger is at DEBUG.""" - stream_debug_enabled = logging.getLogger(__name__).isEnabledFor(logging.DEBUG) - def libav_filter(record: logging.LogRecord) -> bool: - return stream_debug_enabled + return logging.getLogger(__name__).isEnabledFor(logging.DEBUG) for logging_namespace in ( "libav.mp4", diff --git a/homeassistant/components/stream/manifest.json b/homeassistant/components/stream/manifest.json index 5f6dd4e61aa..1fe64defe36 100644 --- a/homeassistant/components/stream/manifest.json +++ b/homeassistant/components/stream/manifest.json @@ -6,6 +6,5 @@ "dependencies": ["http"], "codeowners": ["@hunterjm", "@uvjustin", "@allenporter"], "quality_scale": "internal", - "iot_class": "local_push", - "loggers": ["av"] + "iot_class": "local_push" } diff --git a/tests/components/stream/test_init.py b/tests/components/stream/test_init.py new file mode 100644 index 00000000000..92b2848caef --- /dev/null +++ b/tests/components/stream/test_init.py @@ -0,0 +1,46 @@ +"""Test stream init.""" + +import logging + +import av + +from homeassistant.components.stream import __name__ as stream_name +from homeassistant.setup import async_setup_component + + +async def test_log_levels(hass, caplog): + """Test that the worker logs the url without username and password.""" + + logging.getLogger(stream_name).setLevel(logging.INFO) + + await async_setup_component(hass, "stream", {"stream": {}}) + + # These namespaces should only pass log messages when the stream logger + # is at logging.DEBUG or below + namespaces_to_toggle = ( + "mp4", + "h264", + "hevc", + "rtsp", + "tcp", + "tls", + "mpegts", + "NULL", + ) + + # Since logging is at INFO, these should not pass + for namespace in namespaces_to_toggle: + av.logging.log(av.logging.ERROR, namespace, "SHOULD NOT PASS") + + logging.getLogger(stream_name).setLevel(logging.DEBUG) + + # Since logging is now at DEBUG, these should now pass + for namespace in namespaces_to_toggle: + av.logging.log(av.logging.ERROR, namespace, "SHOULD PASS") + + # Even though logging is at DEBUG, these should not pass + av.logging.log(av.logging.WARNING, "mp4", "SHOULD NOT PASS") + av.logging.log(av.logging.WARNING, "swscaler", "SHOULD NOT PASS") + + assert "SHOULD PASS" in caplog.text + assert "SHOULD NOT PASS" not in caplog.text