diff --git a/sentry_sdk/__init__.py b/sentry_sdk/__init__.py index 9dbe596722..baccfa7e43 100644 --- a/sentry_sdk/__init__.py +++ b/sentry_sdk/__init__.py @@ -1,2 +1,8 @@ from .api import * # noqa from .api import __all__ # noqa + +# Initialize the debug support after everything is loaded +from .debug import init_debug_support + +init_debug_support() +del init_debug_support diff --git a/sentry_sdk/debug.py b/sentry_sdk/debug.py new file mode 100644 index 0000000000..617fa9fc0d --- /dev/null +++ b/sentry_sdk/debug.py @@ -0,0 +1,23 @@ +import sys +import logging + +from .hub import Hub +from .utils import logger + + +class _HubBasedClientFilter(logging.Filter): + def filter(self, record): + hub = Hub.current + if hub is not None and hub.client is not None: + return hub.client.options["debug"] + return False + + +def init_debug_support(): + if logger.handlers: + return + _handler = logging.StreamHandler(sys.stderr) + _handler.setFormatter(logging.Formatter(" [sentry] %(levelname)s: %(message)s")) + logger.addHandler(_handler) + logger.setLevel(logging.DEBUG) + logger.addFilter(_HubBasedClientFilter()) diff --git a/sentry_sdk/hub.py b/sentry_sdk/hub.py index 2287ad3507..6b4b13f0c2 100644 --- a/sentry_sdk/hub.py +++ b/sentry_sdk/hub.py @@ -16,7 +16,9 @@ def _internal_exceptions(): try: yield except Exception: - Hub.current.capture_internal_exception(sys.exc_info()) + hub = Hub.current + if hub: + hub.capture_internal_exception(sys.exc_info()) def _get_client_options(): @@ -159,9 +161,7 @@ def capture_exception(self, error=None): def capture_internal_exception(self, exc_info): """Capture an exception that is likely caused by a bug in the SDK itself.""" - client = self.client - if client is not None and client.options["debug"]: - logger.debug("Internal error in sentry_sdk", exc_info=exc_info) + logger.debug("Internal error in sentry_sdk", exc_info=exc_info) def add_breadcrumb(self, crumb=None, hint=None, **kwargs): """Adds a breadcrumb.""" diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index fb5e66bc1d..89de455fbc 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -12,6 +12,10 @@ epoch = datetime(1970, 1, 1) +# The logger is created here but initializde in the debug support module +logger = logging.getLogger("sentry_sdk.errors") + + def to_timestamp(value): return (value - epoch).total_seconds() @@ -523,14 +527,6 @@ def strip_string(value, assume_length=None, max_length=512): return value[:max_length] -logger = logging.getLogger("sentry_sdk.errors") -if not logger.handlers: - _handler = logging.StreamHandler(sys.stderr) - _handler.setFormatter(logging.Formatter(" [sentry] %(levelname)s: %(message)s")) - logger.addHandler(_handler) - logger.setLevel(logging.DEBUG) - - try: from contextvars import ContextVar except ImportError: