From 176e5111805b574ae34e042067645216eb3955a7 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Fri, 28 Feb 2025 21:28:40 +0100 Subject: [PATCH] Capture warnings and report to sentry (#5697) By default, warnings are simply printed to stderr. This makes them easy to miss in the log. Capture warnings and user Python logger to log them with warning level. Also, if the message is an instance of Exception (which it typically is), report the warning to Sentry. This is e.g. useful for asyncio RuntimeWarning warnings "coroutine was never awaited". --- supervisor/bootstrap.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/supervisor/bootstrap.py b/supervisor/bootstrap.py index b5b0fd230..64ded55ce 100644 --- a/supervisor/bootstrap.py +++ b/supervisor/bootstrap.py @@ -4,8 +4,10 @@ import logging import os import signal +import warnings from colorlog import ColoredFormatter +from sentry_sdk import capture_exception from .addons.manager import AddonManager from .api import RestAPI @@ -222,6 +224,13 @@ def initialize_system(coresys: CoreSys) -> None: config.path_addon_configs.mkdir() +def warning_handler(message, category, filename, lineno, file=None, line=None): + """Warning handler which logs warnings using the logging module.""" + _LOGGER.warning("%s:%s: %s: %s", filename, lineno, category.__name__, message) + if isinstance(message, Exception): + capture_exception(message) + + def initialize_logging() -> None: """Initialize the logging.""" logging.basicConfig(level=logging.INFO) @@ -248,6 +257,7 @@ def initialize_logging() -> None: }, ) ) + warnings.showwarning = warning_handler def check_environment() -> None: