Skip to content

Commit

Permalink
PB-931 Fix stack_info formatting in JSON formatter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
schtibe committed Jan 16, 2025
1 parent d7e03bf commit f4584be
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
6 changes: 3 additions & 3 deletions logging_utilities/formatters/json_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,14 @@ def format(self, record):
if not record.exc_text:
record.exc_text = self.formatException(record.exc_info)

if record.stack_info:
record.stack_info = self.formatStack(record.stack_info)

message = self.formatMessage(record)

if self.add_always_extra:
self._add_extra_to_message(extra, message)

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

# When adding all extras, to avoid crash when a log message adds an extra with a non
# serializable object, we add a default serializer.
default = self.kwargs.pop('default', None)
Expand Down
7 changes: 2 additions & 5 deletions tests/test_json_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,8 @@ def test_stack_info(self):
logger = logging.getLogger('test_formatter')
self._configure_logger(
logger,
fmt=dictionary([
('levelname', 'levelname'),
('name', 'name'),
('message', 'message'),
])
fmt=dictionary([('levelname', 'levelname'), ('name', 'name'),
('message', 'message'), ('stack_info', 'stack_info')])
)
logger.info('Message with stack info', stack_info=True)
message = json.loads(ctx.output[0], object_pairs_hook=dictionary)
Expand Down

0 comments on commit f4584be

Please sign in to comment.