Skip to content

Commit 82fd9a5

Browse files
committed
PB-931 Fix the exc_text field handling in JSON formatter
Adding the exc_text field to the record before formatting a message. This allows for formatting the message through the django logging facility Removing the addition of exc_text to the message directly. Instead adding it to the record, so that it can be formatted through the django logging facility
1 parent 460eccf commit 82fd9a5

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

logging_utilities/formatters/json_formatter.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,18 +343,16 @@ def format(self, record):
343343
if self.add_always_extra:
344344
extra = self._get_extra_attrs(record)
345345

346-
message = self.formatMessage(record)
347-
348-
if self.add_always_extra:
349-
self._add_extra_to_message(extra, message)
350-
351346
if record.exc_info:
352347
# Cache the traceback text to avoid converting it multiple times
353348
# (it's constant anyway)
354349
if not record.exc_text:
355350
record.exc_text = self.formatException(record.exc_info)
356-
if record.exc_text:
357-
message['exc_text'] = record.exc_text
351+
352+
message = self.formatMessage(record)
353+
354+
if self.add_always_extra:
355+
self._add_extra_to_message(extra, message)
358356

359357
if record.stack_info:
360358
message['stack_info'] = self.formatStack(record.stack_info)

tests/test_json_formatter.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
dictionary = OrderedDict
1919

2020

21+
# pylint: disable=too-many-public-methods
2122
class BasicJsonFormatterTest(unittest.TestCase):
2223
maxDiff = None
2324

@@ -787,3 +788,41 @@ def test_missing_attribute(self):
787788
("message", "Composed message without extra"),
788789
])
789790
)
791+
792+
def test_exc_info_without_configuration(self):
793+
"""
794+
Test that the exc_text doesn't appear if it's not configured
795+
in the logger, even though the logging call records it
796+
"""
797+
with self.assertLogs('test_formatter', level=logging.DEBUG) as ctx:
798+
logger = logging.getLogger('test_formatter')
799+
self._configure_logger(
800+
logger,
801+
fmt=dictionary([
802+
('level', 'levelname'),
803+
('request', ['request.path', 'request.method']),
804+
('message', 'message'),
805+
]),
806+
remove_empty=True,
807+
ignore_missing=True
808+
)
809+
810+
try:
811+
raise Exception("Error occurred")
812+
except Exception as err: # pylint: disable=broad-except
813+
logger.critical(
814+
str(err),
815+
exc_info=sys.exc_info(),
816+
extra={'request': {
817+
'path': '/my/path', 'method': 'GET', 'scheme': 'https'
818+
}}
819+
)
820+
821+
self.assertDictEqual(
822+
json.loads(ctx.output[0], object_pairs_hook=dictionary),
823+
dictionary([
824+
("level", "CRITICAL"),
825+
("request", ["/my/path", "GET"]),
826+
("message", "Error occurred"), # no exc_text because it wasn't configured
827+
])
828+
)

0 commit comments

Comments
 (0)