Skip to content

Commit 0615ff9

Browse files
committed
feat: magic logging config
1 parent c7e2c7f commit 0615ff9

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

sentry_sdk/integrations/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ def _celery_integration(*a, **kw):
4343
from .celery import install
4444

4545
return install(*a, **kw)
46+
47+
48+
@register_integration('logging')
49+
def _logging_integration(*a, **kw):
50+
from .logging import install
51+
52+
return install(*a, **kw)

sentry_sdk/integrations/logging.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,37 @@
33

44
import sys
55
import logging
6+
from threading import Lock
67

78
from sentry_sdk import get_current_hub, capture_event, add_breadcrumb
89
from sentry_sdk.utils import to_string, Event, skip_internal_frames
910

1011

12+
_installer_lock = Lock()
13+
_installed = False
14+
_master_handler = None
15+
16+
def install(client):
17+
global _installed
18+
global _master_handler
19+
with _installer_lock:
20+
if _installed:
21+
return
22+
23+
_master_handler = SentryHandler()
24+
_master_handler.setLevel(logging.INFO) # TODO: make configurable
25+
26+
old_callhandlers = logging.Logger.callHandlers
27+
28+
def sentry_patched_callhandlers(self, record):
29+
_master_handler.handle(record)
30+
return old_callhandlers(self, record)
31+
32+
logging.Logger.callHandlers = sentry_patched_callhandlers
33+
34+
_installed = True
35+
36+
1137
class SentryHandler(logging.Handler, object):
1238
def emit(self, record):
1339
try:

tests/integrations/flask/test_flask.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from sentry_sdk.integrations.logging import SentryHandler
1515
import sentry_sdk.integrations.flask as flask_sentry
1616

17-
get_current_hub().bind_client(Client(integrations=["flask"]))
17+
get_current_hub().bind_client(Client(integrations=["flask", "logging"]))
1818
login_manager = LoginManager()
1919

2020

@@ -23,11 +23,6 @@ def app():
2323
app = Flask(__name__)
2424
app.config["TESTING"] = True
2525
app.secret_key = "haha"
26-
app.logger.setLevel(logging.DEBUG)
27-
handler = SentryHandler()
28-
handler.setLevel(logging.DEBUG)
29-
app.logger.handlers = []
30-
app.logger.addHandler(handler)
3126

3227
login_manager.init_app(app)
3328

@@ -273,6 +268,8 @@ def index():
273268

274269

275270
def test_logging(capture_events, app):
271+
# ensure that Flask's logger magic doesn't break ours
272+
276273
@app.route("/")
277274
def index():
278275
app.logger.error("hi")
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
import pytest
12
import logging
23

34
import sentry_sdk
4-
from sentry_sdk.integrations.logging import SentryHandler
55

6-
logger = logging.getLogger(__name__)
6+
other_logger = logging.getLogger("testfoo")
7+
other_logger.setLevel(logging.DEBUG)
78

8-
logger.handlers = [SentryHandler()]
9-
logger.setLevel(logging.DEBUG)
9+
sentry_sdk.get_current_hub().bind_client(sentry_sdk.Client(integrations=['logging']))
1010

11-
sentry_sdk.get_current_hub().bind_client(sentry_sdk.Client())
11+
logger = logging.getLogger(__name__)
12+
logger.setLevel(logging.DEBUG)
1213

1314

14-
def test_logging(capture_events):
15+
@pytest.mark.parametrize("logger", [logger, other_logger])
16+
def test_logging(capture_events, logger):
1517
logger.info("bread")
1618
logger.critical("LOL")
1719
event, = capture_events
1820
assert event["level"] == "fatal"
1921
assert not event["logentry"]["params"]
2022
assert event["logentry"]["message"] == "LOL"
21-
assert event["breadcrumbs"][0]["message"] == "bread"
23+
assert any(crumb["message"] == "bread" for crumb in event['breadcrumbs'])

0 commit comments

Comments
 (0)