Skip to content

Commit a3d3b7f

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 82fd9a5 commit a3d3b7f

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

0 commit comments

Comments
 (0)