Skip to content

Commit 88b2d3a

Browse files
mitsuhikountitaker
authored andcommitted
ref: Alternative setup for debug logging (#55)
* ref: Alternative setup for debug logging * fix: remove unused import
1 parent 270bc75 commit 88b2d3a

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

sentry_sdk/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
from .api import * # noqa
22
from .api import __all__ # noqa
3+
4+
# Initialize the debug support after everything is loaded
5+
from .debug import init_debug_support
6+
7+
init_debug_support()
8+
del init_debug_support

sentry_sdk/debug.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
import logging
3+
4+
from .hub import Hub
5+
from .utils import logger
6+
7+
8+
class _HubBasedClientFilter(logging.Filter):
9+
def filter(self, record):
10+
hub = Hub.current
11+
if hub is not None and hub.client is not None:
12+
return hub.client.options["debug"]
13+
return False
14+
15+
16+
def init_debug_support():
17+
if logger.handlers:
18+
return
19+
_handler = logging.StreamHandler(sys.stderr)
20+
_handler.setFormatter(logging.Formatter(" [sentry] %(levelname)s: %(message)s"))
21+
logger.addHandler(_handler)
22+
logger.setLevel(logging.DEBUG)
23+
logger.addFilter(_HubBasedClientFilter())

sentry_sdk/hub.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def _internal_exceptions():
1616
try:
1717
yield
1818
except Exception:
19-
Hub.current.capture_internal_exception(sys.exc_info())
19+
hub = Hub.current
20+
if hub:
21+
hub.capture_internal_exception(sys.exc_info())
2022

2123

2224
def _get_client_options():
@@ -159,9 +161,7 @@ def capture_exception(self, error=None):
159161
def capture_internal_exception(self, exc_info):
160162
"""Capture an exception that is likely caused by a bug in the SDK
161163
itself."""
162-
client = self.client
163-
if client is not None and client.options["debug"]:
164-
logger.debug("Internal error in sentry_sdk", exc_info=exc_info)
164+
logger.debug("Internal error in sentry_sdk", exc_info=exc_info)
165165

166166
def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
167167
"""Adds a breadcrumb."""

sentry_sdk/utils.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
epoch = datetime(1970, 1, 1)
1313

1414

15+
# The logger is created here but initializde in the debug support module
16+
logger = logging.getLogger("sentry_sdk.errors")
17+
18+
1519
def to_timestamp(value):
1620
return (value - epoch).total_seconds()
1721

@@ -523,14 +527,6 @@ def strip_string(value, assume_length=None, max_length=512):
523527
return value[:max_length]
524528

525529

526-
logger = logging.getLogger("sentry_sdk.errors")
527-
if not logger.handlers:
528-
_handler = logging.StreamHandler(sys.stderr)
529-
_handler.setFormatter(logging.Formatter(" [sentry] %(levelname)s: %(message)s"))
530-
logger.addHandler(_handler)
531-
logger.setLevel(logging.DEBUG)
532-
533-
534530
try:
535531
from contextvars import ContextVar
536532
except ImportError:

0 commit comments

Comments
 (0)