Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sentry_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions sentry_sdk/debug.py
Original file line number Diff line number Diff line change
@@ -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())
8 changes: 4 additions & 4 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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."""
Expand Down
12 changes: 4 additions & 8 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand Down