Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit f6d79c3

Browse files
rakai93ashwoods
authored andcommitted
Allow dict-style logging in breadcrumbs processing
1 parent a698fc4 commit f6d79c3

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

raven/breadcrumbs.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import
22

3+
import collections
34
import os
45
import logging
56

@@ -132,13 +133,23 @@ def _record_log_breadcrumb(logger, level, msg, *args, **kwargs):
132133

133134
def processor(data):
134135
formatted_msg = msg
136+
format_args = args
137+
138+
# Extract 'extra' key from kwargs and merge into data
139+
extra = kwargs.pop('extra', {})
140+
data_value = kwargs
141+
data_value.update(extra)
142+
143+
if args and len(args) == 1 and isinstance(args[0], collections.Mapping) and args[0]:
144+
format_args = args[0]
145+
data_value.update(format_args)
135146

136147
# If people log bad things, this can happen. Then just don't do
137148
# anything.
138149
try:
139150
formatted_msg = text_type(msg)
140-
if args:
141-
formatted_msg = msg % args
151+
if format_args:
152+
formatted_msg = msg % format_args
142153
except Exception:
143154
pass
144155

@@ -151,7 +162,7 @@ def processor(data):
151162
'message': formatted_msg,
152163
'category': logger.name,
153164
'level': logging.getLevelName(level).lower(),
154-
'data': kwargs,
165+
'data': data_value,
155166
})
156167
record(processor=processor)
157168

tests/breadcrumbs/tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ def test_log_crumb_reporting(self):
3939
assert crumbs[0]['data'] == {'blah': 'baz'}
4040
assert crumbs[0]['message'] == 'This is a message with foo!'
4141

42+
def test_log_crumb_reporting_with_dict(self):
43+
client = Client('http://foo:[email protected]/0')
44+
with client.context:
45+
log = logging.getLogger('whatever.foo')
46+
log.info('This is a message with %(foo)s!', {'foo': 'bar'},
47+
extra={'blah': 'baz'})
48+
crumbs = client.context.breadcrumbs.get_buffer()
49+
50+
assert len(crumbs) == 1
51+
assert crumbs[0]['type'] == 'default'
52+
assert crumbs[0]['category'] == 'whatever.foo'
53+
assert crumbs[0]['data'] == {'foo': 'bar', 'blah': 'baz'}
54+
assert crumbs[0]['message'] == 'This is a message with bar!'
55+
4256
def test_log_crumb_reporting_with_large_message(self):
4357
client = Client('http://foo:[email protected]/0')
4458
with client.context:

0 commit comments

Comments
 (0)