Skip to content

Commit 178550b

Browse files
Rémy HUBSCHERunderyx
Rémy HUBSCHER
authored andcommitted
Add option to patch stdlib logging to send events
Closes #19 Closes #20
1 parent 205fcc4 commit 178550b

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 0.6.0 (2019-07-11)
4+
5+
- Add option to patch stdlib logging to send events (thanks, @Natim!)
6+
37
## 0.5.0 (2019-01-10)
48

59
- Use new-style, aiohttp 2.3+ middleware definition,

README.rst

+20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ With this, you can specify environment details, filter out specific exceptions,
4343
),
4444
)
4545
46+
If you are using the standard library's ``logging`` module,
47+
we have a convenient parameter to patch it for you,
48+
to have logger calls send events to Sentry automatically:
49+
50+
.. warning::
51+
This modifies your logging configuration globally
52+
when you instantiate the middleware.
53+
Even if you don't end up using the middleware instance for a request,
54+
all your logs will be sent to Sentry.
55+
56+
.. code-block:: python
57+
58+
import logging
59+
from aiohttp import web
60+
from aiohttp_sentry import SentryMiddleware
61+
62+
app = web.Application(
63+
middlewares=[SentryMiddleware(patch_logging=True, sentry_log_level=logging.WARNING)],
64+
)
65+
4666
.. _aiohttp: http://aiohttp.readthedocs.io/en/stable/
4767
.. _Sentry: http://sentry.io/
4868

aiohttp_sentry/__init__.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
import asyncio
2+
import logging
13
import sys
24

3-
import asyncio
45
from aiohttp import web
6+
57
import raven
68
import raven_aiohttp
9+
from raven.conf import setup_logging
10+
from raven.handlers.logging import SentryHandler
711

812

913
@web.middleware
1014
class SentryMiddleware:
11-
def __init__(self, sentry_kwargs=None, *, install_excepthook=True, loop=None):
15+
def __init__(
16+
self,
17+
sentry_kwargs=None,
18+
*,
19+
install_excepthook=True,
20+
loop=None,
21+
patch_logging=False,
22+
sentry_log_level=logging.ERROR
23+
):
1224
if sentry_kwargs is None:
1325
sentry_kwargs = {}
1426

@@ -21,6 +33,12 @@ def __init__(self, sentry_kwargs=None, *, install_excepthook=True, loop=None):
2133
}
2234
self.client = raven.Client(**sentry_kwargs)
2335

36+
# Setup Sentry logger - https://docs.sentry.io/clients/python/integrations/#logging
37+
if patch_logging:
38+
handler = SentryHandler(client=self.client)
39+
handler.setLevel(sentry_log_level)
40+
setup_logging(handler)
41+
2442
if install_excepthook:
2543
self.update_excepthook(loop)
2644

@@ -49,7 +67,7 @@ def aiohttp_transport_excepthook(*exc_info):
4967
event_loop.run_until_complete(transport.close())
5068
# the idea of using the original excepthook
5169
# was taken from raven-python repository:
52-
# https://github.com/getsentry/raven-python/blob/f6d79c3bcc25e804b6259fa9c4a6e030f9033bb2/raven/base.py#L280
70+
# https://github.com/getsentry/raven-python/blob/f6d79c3b/raven/base.py#L280
5371
original_excepthook(*exc_info)
5472

5573
sys.excepthook = aiohttp_transport_excepthook

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name="aiohttp-sentry",
15-
version="0.5.0",
15+
version="0.6.0",
1616
url="https://github.com/underyx/aiohttp-sentry",
1717
author="Bence Nagy",
1818
author_email="[email protected]",

0 commit comments

Comments
 (0)