Permalink
Browse files

Logbook and logging are now functioning with tests

  • Loading branch information...
1 parent ae318de commit efcefb53305d3758ce46e11531274320acb53437 @dcramer committed Jun 25, 2011
@@ -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)
@@ -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
@@ -18,6 +18,7 @@
'unittest2',
'Django>=1.2,<1.4',
'django-celery',
+ 'logbook',
]
setup(
@@ -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')
@@ -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')

0 comments on commit efcefb5

Please sign in to comment.