diff options
author | Christian Fueller <christian.fueller@sap.com> | 2018-06-01 13:27:39 +0200 |
---|---|---|
committer | Ashley Camba <ashwoods@gmail.com> | 2018-07-24 14:24:16 +0200 |
commit | f6d79c3bcc25e804b6259fa9c4a6e030f9033bb2 (patch) | |
tree | 014aa2d30e9385b48ebf5e03a79912ccec7b50ce | |
parent | a698fc406d2e0f26616f0163b2b22e1a3a5b30f5 (diff) | |
download | raven-f6d79c3bcc25e804b6259fa9c4a6e030f9033bb2.tar.gz |
Allow dict-style logging in breadcrumbs processing
-rw-r--r-- | raven/breadcrumbs.py | 17 | ||||
-rw-r--r-- | tests/breadcrumbs/tests.py | 14 |
2 files changed, 28 insertions, 3 deletions
diff --git a/raven/breadcrumbs.py b/raven/breadcrumbs.py index 13987f1..fbf3026 100644 --- a/raven/breadcrumbs.py +++ b/raven/breadcrumbs.py @@ -1,5 +1,6 @@ from __future__ import absolute_import +import collections import os import logging @@ -132,13 +133,23 @@ def _record_log_breadcrumb(logger, level, msg, *args, **kwargs): def processor(data): formatted_msg = msg + format_args = args + + # Extract 'extra' key from kwargs and merge into data + extra = kwargs.pop('extra', {}) + data_value = kwargs + data_value.update(extra) + + if args and len(args) == 1 and isinstance(args[0], collections.Mapping) and args[0]: + format_args = args[0] + data_value.update(format_args) # If people log bad things, this can happen. Then just don't do # anything. try: formatted_msg = text_type(msg) - if args: - formatted_msg = msg % args + if format_args: + formatted_msg = msg % format_args except Exception: pass @@ -151,7 +162,7 @@ def _record_log_breadcrumb(logger, level, msg, *args, **kwargs): 'message': formatted_msg, 'category': logger.name, 'level': logging.getLevelName(level).lower(), - 'data': kwargs, + 'data': data_value, }) record(processor=processor) diff --git a/tests/breadcrumbs/tests.py b/tests/breadcrumbs/tests.py index da291e5..923f479 100644 --- a/tests/breadcrumbs/tests.py +++ b/tests/breadcrumbs/tests.py @@ -39,6 +39,20 @@ class BreadcrumbTestCase(TestCase): assert crumbs[0]['data'] == {'blah': 'baz'} assert crumbs[0]['message'] == 'This is a message with foo!' + def test_log_crumb_reporting_with_dict(self): + client = Client('http://foo:bar@example.com/0') + with client.context: + log = logging.getLogger('whatever.foo') + log.info('This is a message with %(foo)s!', {'foo': 'bar'}, + extra={'blah': 'baz'}) + crumbs = client.context.breadcrumbs.get_buffer() + + assert len(crumbs) == 1 + assert crumbs[0]['type'] == 'default' + assert crumbs[0]['category'] == 'whatever.foo' + assert crumbs[0]['data'] == {'foo': 'bar', 'blah': 'baz'} + assert crumbs[0]['message'] == 'This is a message with bar!' + def test_log_crumb_reporting_with_large_message(self): client = Client('http://foo:bar@example.com/0') with client.context: |