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".
This commit is contained in:
Stefan Agner 2025-02-28 21:28:40 +01:00 committed by GitHub
parent 696dcf6149
commit 176e511180
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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: