mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-20 07:36:29 +00:00
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:
parent
696dcf6149
commit
176e511180
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user