Skip to content

Commit 42aa95b

Browse files
committed
PB-931 Fix stack_info formatting in JSON formatter
Add the stack info to the record instead of the message directly in JSON formatter. This way it can be configured through the Django logging facility
1 parent 1d07ea7 commit 42aa95b

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

logging_utilities/formatters/json_formatter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,14 @@ def format(self, record):
349349
if not record.exc_text:
350350
record.exc_text = self.formatException(record.exc_info)
351351

352+
if record.stack_info:
353+
record.stack_info = self.formatStack(record.stack_info)
354+
352355
message = self.formatMessage(record)
353356

354357
if self.add_always_extra:
355358
self._add_extra_to_message(extra, message)
356359

357-
if record.stack_info:
358-
message['stack_info'] = self.formatStack(record.stack_info)
359-
360360
# When adding all extras, to avoid crash when a log message adds an extra with a non
361361
# serializable object, we add a default serializer.
362362
default = self.kwargs.pop('default', None)

tests/test_json_formatter.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,8 @@ def test_stack_info(self):
378378
logger = logging.getLogger('test_formatter')
379379
self._configure_logger(
380380
logger,
381-
fmt=dictionary([
382-
('levelname', 'levelname'),
383-
('name', 'name'),
384-
('message', 'message'),
385-
])
381+
fmt=dictionary([('levelname', 'levelname'), ('name', 'name'),
382+
('message', 'message'), ('stack_info', 'stack_info')])
386383
)
387384
logger.info('Message with stack info', stack_info=True)
388385
message = json.loads(ctx.output[0], object_pairs_hook=dictionary)
@@ -825,3 +822,41 @@ def test_exc_info_without_configuration(self):
825822
("message", "Error occurred"), # no exc_text because it wasn't configured
826823
])
827824
)
825+
826+
def test_stack_info_without_configuration(self):
827+
"""
828+
Test that the stack_info doesn't appear in the record when it's
829+
not configured, even though the log call records it
830+
"""
831+
with self.assertLogs('test_formatter', level=logging.DEBUG) as ctx:
832+
logger = logging.getLogger('test_formatter')
833+
self._configure_logger(
834+
logger,
835+
fmt=dictionary([
836+
('level', 'levelname'),
837+
('request', ['request.path', 'request.method']),
838+
('message', 'message'),
839+
]),
840+
remove_empty=True,
841+
ignore_missing=True
842+
)
843+
844+
logger.info(
845+
"Stack isn't wished for here",
846+
exc_info=sys.exc_info(),
847+
extra={'request': {
848+
'path': '/my/path', 'method': 'GET', 'scheme': 'https'
849+
}},
850+
# record the stack_info
851+
stack_info=True
852+
)
853+
854+
self.assertDictEqual(
855+
json.loads(ctx.output[0], object_pairs_hook=dictionary),
856+
dictionary([
857+
("level", "INFO"),
858+
("request", ["/my/path", "GET"]),
859+
("message",
860+
"Stack isn't wished for here"), # no stack_info because it wasn't configured
861+
])
862+
)

0 commit comments

Comments
 (0)