summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Stasiak <jakub@stasiak.at>2018-03-28 13:47:46 +0200
committerAshley Camba <ashwoods@gmail.com>2018-03-29 21:44:45 +0200
commit9223d40d0c8da7fbbe60eb2bf58fea3992bee910 (patch)
tree8a8b526f13ec77863397534f264138bb2d6c64fd
parente056c9dd6852d77b49ad1041f63605eb2523c286 (diff)
downloadraven-9223d40d0c8da7fbbe60eb2bf58fea3992bee910.tar.gz
logging: Stop mutating record.data
Without this patch raven's logging handler can't be used with connexion, because connexion does the following[1]: logger.debug('Getting data and status code', extra={ 'data': response, 'data_type': type(response), 'url': flask.request.url }) When response was a dictionary raven would modify it, thus causing unexpected and unwanted side effects. [1] https://github.com/zalando/connexion/blob/9f20c5ffb70ccde05193077e677da2a09af362c9/connexion/apis/flask_api.py#L110
-rw-r--r--raven/handlers/logging.py3
-rw-r--r--tests/handlers/logging/tests.py8
2 files changed, 11 insertions, 0 deletions
diff --git a/raven/handlers/logging.py b/raven/handlers/logging.py
index eb5b7e3..49a17ce 100644
--- a/raven/handlers/logging.py
+++ b/raven/handlers/logging.py
@@ -40,6 +40,9 @@ def extract_extra(record, reserved=RESERVED, contextual=CONTEXTUAL):
extra = {'data': extra}
else:
extra = {}
+ else:
+ # record.data may be something we don't want to mutate to not cause unexpected side effects
+ extra = dict(extra)
for k, v in iteritems(vars(record)):
if k in reserved:
diff --git a/tests/handlers/logging/tests.py b/tests/handlers/logging/tests.py
index 5b6dc5b..7f24f78 100644
--- a/tests/handlers/logging/tests.py
+++ b/tests/handlers/logging/tests.py
@@ -119,6 +119,14 @@ class LoggingIntegrationTest(TestCase):
expected = "u'http://example.com'"
self.assertEqual(event['extra']['url'], expected)
+ def test_extra_data_dict_is_not_mutated(self):
+ # The code used to modify the dictionary included in extra arguments under the 'data' key.
+ # This is unexpected behavior, let's make sure it doesn't happen anymore.
+ data = {'data_key': 'data_value'}
+ record = self.make_record('irrelevant', extra={'data': data})
+ self.handler.emit(record)
+ self.assertEqual(data, {'data_key': 'data_value'})
+
def test_logger_exc_info(self):
try:
raise ValueError('This is a test ValueError')