Skip to content

Commit 4dd2327

Browse files
authored
feat: refactored debug logging (#59)
1 parent ee637fb commit 4dd2327

File tree

19 files changed

+105
-86
lines changed

19 files changed

+105
-86
lines changed

sentry_sdk/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
except Exception as e:
2323
sentry_sdk.capture_exception(e)
2424
"""
25-
from .api import * # noqa
26-
from .api import __all__ # noqa
25+
from sentry_sdk.api import * # noqa
26+
from sentry_sdk.api import __all__ # noqa
2727

2828
# modules we consider public
2929
__all__.append("integrations")
3030

3131
# Initialize the debug support after everything is loaded
32-
from .debug import init_debug_support
32+
from sentry_sdk.debug import init_debug_support
3333

3434
init_debug_support()
3535
del init_debug_support

sentry_sdk/api.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import inspect
22
from contextlib import contextmanager
33

4-
from .hub import Hub
5-
from .scope import Scope
6-
from .utils import EventHint
7-
from .transport import Transport, HttpTransport
8-
from .client import Client, get_options
9-
from .integrations import setup_integrations
4+
from sentry_sdk.hub import Hub
5+
from sentry_sdk.scope import Scope
6+
from sentry_sdk.utils import EventHint
7+
from sentry_sdk.transport import Transport, HttpTransport
8+
from sentry_sdk.client import Client, get_options
9+
from sentry_sdk.integrations import setup_integrations
1010

1111

1212
__all__ = ["Hub", "Scope", "Client", "EventHint", "Transport", "HttpTransport"]

sentry_sdk/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
import random
44
from datetime import datetime
55

6-
from ._compat import string_types
7-
from .utils import (
6+
from sentry_sdk._compat import string_types
7+
from sentry_sdk.utils import (
88
strip_event,
99
flatten_metadata,
1010
convert_types,
1111
handle_in_app,
1212
get_type_name,
1313
logger,
1414
)
15-
from .transport import make_transport
16-
from .consts import DEFAULT_OPTIONS, SDK_INFO
15+
from sentry_sdk.transport import make_transport
16+
from sentry_sdk.consts import DEFAULT_OPTIONS, SDK_INFO
1717

1818

1919
def get_options(*args, **kwargs):

sentry_sdk/debug.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import sys
22
import logging
33

4-
from .hub import Hub
5-
from .utils import logger
4+
from sentry_sdk import utils
5+
from sentry_sdk.hub import Hub
6+
from sentry_sdk.utils import logger
67

78

89
class _HubBasedClientFilter(logging.Filter):
@@ -14,10 +15,21 @@ def filter(self, record):
1415

1516

1617
def init_debug_support():
17-
if logger.handlers:
18-
return
18+
if not logger.handlers:
19+
configure_logger()
20+
configure_debug_hub()
21+
22+
23+
def configure_logger():
1924
_handler = logging.StreamHandler(sys.stderr)
2025
_handler.setFormatter(logging.Formatter(" [sentry] %(levelname)s: %(message)s"))
2126
logger.addHandler(_handler)
2227
logger.setLevel(logging.DEBUG)
2328
logger.addFilter(_HubBasedClientFilter())
29+
30+
31+
def configure_debug_hub():
32+
def _get_debug_hub():
33+
return Hub.current
34+
35+
utils._get_debug_hub = _get_debug_hub

sentry_sdk/hub.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,19 @@
33
from datetime import datetime
44
from contextlib import contextmanager
55

6-
from ._compat import with_metaclass
7-
from .scope import Scope
8-
from .utils import exc_info_from_error, event_from_exception, logger, ContextVar
6+
from sentry_sdk._compat import with_metaclass
7+
from sentry_sdk.scope import Scope
8+
from sentry_sdk.utils import (
9+
exc_info_from_error,
10+
event_from_exception,
11+
logger,
12+
ContextVar,
13+
)
914

1015

1116
_local = ContextVar("sentry_current_hub")
1217

1318

14-
@contextmanager
15-
def _internal_exceptions():
16-
try:
17-
yield
18-
except Exception:
19-
hub = Hub.current
20-
if hub:
21-
hub._capture_internal_exception(sys.exc_info())
22-
23-
2419
def _get_client_options():
2520
hub = Hub.current
2621
if hub and hub.client:

sentry_sdk/integrations/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""This package"""
22
from threading import Lock
33

4-
from ..utils import logger
5-
from ..consts import INTEGRATIONS as _installed_integrations
4+
from sentry_sdk.utils import logger
5+
from sentry_sdk.consts import INTEGRATIONS as _installed_integrations
66

77

88
_installer_lock = Lock()
@@ -18,11 +18,11 @@ def get_default_integrations():
1818
- `DedupeIntegration`
1919
- `AtexitIntegration`
2020
"""
21-
from .logging import LoggingIntegration
22-
from .stdlib import StdlibIntegration
23-
from .excepthook import ExcepthookIntegration
24-
from .dedupe import DedupeIntegration
25-
from .atexit import AtexitIntegration
21+
from sentry_sdk.integrations.logging import LoggingIntegration
22+
from sentry_sdk.integrations.stdlib import StdlibIntegration
23+
from sentry_sdk.integrations.excepthook import ExcepthookIntegration
24+
from sentry_sdk.integrations.dedupe import DedupeIntegration
25+
from sentry_sdk.integrations.atexit import AtexitIntegration
2626

2727
yield LoggingIntegration()
2828
yield StdlibIntegration()

sentry_sdk/integrations/_wsgi.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
import sys
33

44
from sentry_sdk import capture_exception
5-
from sentry_sdk.hub import (
6-
Hub,
7-
_internal_exceptions,
8-
_should_send_default_pii,
9-
_get_client_options,
10-
)
11-
from sentry_sdk.utils import AnnotatedValue
5+
from sentry_sdk.hub import Hub, _should_send_default_pii, _get_client_options
6+
from sentry_sdk.utils import AnnotatedValue, capture_internal_exceptions
127
from sentry_sdk._compat import reraise, implements_iterator
138

149

@@ -153,7 +148,7 @@ def get_client_ip(environ):
153148
def run_wsgi_app(app, environ, start_response):
154149
hub = Hub.current
155150
hub.push_scope()
156-
with _internal_exceptions():
151+
with capture_internal_exceptions():
157152
with hub.configure_scope() as scope:
158153
scope.add_event_processor(_make_wsgi_event_processor(environ))
159154

@@ -208,7 +203,7 @@ def close(self):
208203

209204
def _make_wsgi_event_processor(environ):
210205
def event_processor(event):
211-
with _internal_exceptions():
206+
with capture_internal_exceptions():
212207
# if the code below fails halfway through we at least have some data
213208
request_info = event.setdefault("request", {})
214209

sentry_sdk/integrations/atexit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from sentry_sdk.hub import Hub
88
from sentry_sdk.utils import logger
9-
from . import Integration
9+
from sentry_sdk.integrations import Integration
1010

1111

1212
def default_shutdown_callback(pending, timeout):

sentry_sdk/integrations/celery.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
from celery.exceptions import SoftTimeLimitExceeded
55

66
from sentry_sdk import Hub
7-
from sentry_sdk.hub import _internal_exceptions
8-
9-
from . import Integration
7+
from sentry_sdk.utils import capture_internal_exceptions
8+
from sentry_sdk.integrations import Integration
109

1110

1211
class CeleryIntegration(Integration):
@@ -39,7 +38,7 @@ def _process_failure_signal(self, sender, task_id, einfo, **kw):
3938
hub.capture_exception(einfo.exc_info)
4039

4140
def _handle_task_prerun(self, sender, task, **kw):
42-
with _internal_exceptions():
41+
with capture_internal_exceptions():
4342
hub = Hub.current
4443
hub.push_scope()
4544
with hub.configure_scope() as scope:

sentry_sdk/integrations/dedupe.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from sentry_sdk.api import configure_scope
22
from sentry_sdk.utils import ContextVar
3-
4-
from . import Integration
3+
from sentry_sdk.integrations import Integration
54

65

76
_last_seen = ContextVar("last-seen")

0 commit comments

Comments
 (0)