Skip to content

Commit

Permalink
Capture warnings and report to sentry (#5697)
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
agners authored Feb 28, 2025
1 parent 696dcf6 commit 176e511
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions supervisor/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -248,6 +257,7 @@ def initialize_logging() -> None:
},
)
)
warnings.showwarning = warning_handler


def check_environment() -> None:
Expand Down

0 comments on commit 176e511

Please sign in to comment.