Skip to content

Commit

Permalink
Correctly handle (and test) request in Django logging handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Jan 25, 2012
1 parent e0cbdeb commit 5a18216
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.1.3

* Fix 'request' attribute on Django logging.

1.1.2

* Corrected logging behavior with extra data to match pre 1.x behavior.
Expand Down
7 changes: 5 additions & 2 deletions raven/contrib/django/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
from __future__ import absolute_import

import logging
from raven.handlers.logging import SentryHandler
from raven.handlers.logging import SentryHandler as BaseSentryHandler


class SentryHandler(BaseSentryHandler):
reserved = BaseSentryHandler.reserved + ['request']

class SentryHandler(SentryHandler):
def __init__(self):
logging.Handler.__init__(self)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

setup(
name='raven',
version='1.1.2',
version='1.1.3',
author='David Cramer',
author_email='[email protected]',
url='http://github.com/dcramer/raven',
Expand Down
35 changes: 35 additions & 0 deletions tests/contrib/django/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@

settings.SENTRY_CLIENT = 'tests.contrib.django.tests.TempStoreClient'


class MockClientHandler(TestClientHandler):
def __call__(self, environ, start_response=[]):
# this pretends doesnt require start_response
return super(MockClientHandler, self).__call__(environ)


class MockSentryMiddleware(Sentry):
def __call__(self, environ, start_response=[]):
# this pretends doesnt require start_response
return list(super(MockSentryMiddleware, self).__call__(environ, start_response))


class TempStoreClient(DjangoClient):
def __init__(self, *args, **kwargs):
self.events = []
Expand All @@ -43,6 +46,7 @@ def __init__(self, *args, **kwargs):
def send(self, **kwargs):
self.events.append(kwargs)


class Settings(object):
"""
Allows you to define settings that are required for this function to work.
Expand All @@ -69,6 +73,7 @@ def __exit__(self, exc_type, exc_value, traceback):
else:
setattr(settings, k, v)


class DjangoClientTest(TestCase):
## Fixture setup/teardown
urls = 'tests.contrib.django.urls'
Expand Down Expand Up @@ -365,6 +370,36 @@ def test_request_capture(self):
self.assertEquals(env['SERVER_PORT'], '80')


class DjangoLoggingTest(TestCase):
def setUp(self):
self.logger = logging.getLogger(__name__)
self.raven = get_client()

def test_request_kwarg(self):
handler = SentryHandler()

logger = self.logger
logger.handlers = []
logger.addHandler(handler)

logger.error('This is a test error', extra={
'request': WSGIRequest(environ={
'wsgi.input': StringIO(),
'REQUEST_METHOD': 'POST',
'SERVER_NAME': 'testserver',
'SERVER_PORT': '80',
'CONTENT_TYPE': 'application/octet-stream',
'ACCEPT': 'application/json',
})
})

self.assertEquals(len(self.raven.events), 1)
event = self.raven.events.pop(0)
self.assertTrue('sentry.interfaces.Http' in event)
http = event['sentry.interfaces.Http']
self.assertEquals(http['method'], 'POST')


class IsolatedCeleryClientTest(TestCase):
def setUp(self):
self.client = make_celery_client(TempStoreClient())
Expand Down

0 comments on commit 5a18216

Please sign in to comment.