Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Logbook and logging are now functioning with tests

  • Loading branch information...
commit 5afc6abc30ed648a2211eaa870d95cc77b24a40f 1 parent d6abddc
@dcramer authored gandalfar committed
View
6 sentry/contrib/logbook/__init__.py
@@ -10,11 +10,9 @@
from sentry import capture
-class SentryLogbookHandler(logbook.Handler):
+class SentryHandler(logbook.Handler):
def emit(self, record):
-
- # TODO: level should be a string
- tags = (('level', record.level), ('logger', record.channel))
+ tags = (('level', logbook.get_level_name(record.level).lower()), ('logger', record.channel))
if record.exc_info:
return capture('Exception', exc_info=record.exc_info, tags=tags)
View
6 sentry/contrib/logging/__init__.py
@@ -14,4 +14,8 @@
class SentryHandler(logging.Handler):
def emit(self, record):
- return capture('Message', message=record.msg, params=record.args)
+ tags = (('level', record.levelname.lower()), ('logger', record.name))
+
+ if record.exc_info:
+ return capture('Exception', exc_info=record.exc_info, tags=tags)
+ return capture('Message', message=record.msg, tags=tags)
View
1  setup.py
@@ -21,6 +21,7 @@
'unittest2',
'Django>=1.2,<1.4',
'django-celery',
+ 'logbook',
]
setup(
View
27 tests/test_contrib/test_logbook.py
@@ -0,0 +1,27 @@
+from .. import BaseTest
+
+from logbook import Logger
+from sentry.contrib.logbook import SentryHandler
+from sentry.models import Event
+
+class LogbookTest(BaseTest):
+ def test_simple(self):
+ logger = Logger('sentry.tests.test_contrib.test_logbook')
+ handler = SentryHandler('INFO')
+ with handler.applicationbound():
+ logger.warn('foo')
+
+ event = Event.objects.all()[0]
+
+ self.assertEquals(event.type, 'sentry.events.Message')
+ self.assertEquals(event.time_spent, 0)
+ self.assertTrue('sentry.interfaces.Message' in event.data)
+ event_data = event.data['sentry.interfaces.Message']
+ self.assertTrue('message' in event_data)
+ self.assertEquals(event_data['message'], 'foo')
+ self.assertTrue('params' in event_data)
+ self.assertEquals(event_data['params'], [])
+
+ tags = dict(event.tags)
+ self.assertTrue('level' in tags)
+ self.assertEquals(tags['level'], 'warning')
View
31 tests/test_contrib/test_logging.py
@@ -0,0 +1,31 @@
+from .. import BaseTest
+
+import logging
+
+from sentry.contrib.logging import SentryHandler
+from sentry.models import Event
+
+class LoggingTest(BaseTest):
+ def test_simple(self):
+ handler = SentryHandler()
+
+ logger = logging.getLogger('sentry.tests.test_contrib.test_logging')
+ logger.setLevel(logging.INFO)
+ logger.addHandler(handler)
+
+ logger.info('foo')
+
+ event = Event.objects.all()[0]
+
+ self.assertEquals(event.type, 'sentry.events.Message')
+ self.assertEquals(event.time_spent, 0)
+ self.assertTrue('sentry.interfaces.Message' in event.data)
+ event_data = event.data['sentry.interfaces.Message']
+ self.assertTrue('message' in event_data)
+ self.assertEquals(event_data['message'], 'foo')
+ self.assertTrue('params' in event_data)
+ self.assertEquals(event_data['params'], [])
+
+ tags = dict(event.tags)
+ self.assertTrue('level' in tags)
+ self.assertEquals(tags['level'], 'info')
Please sign in to comment.
Something went wrong with that request. Please try again.