Skip to content
This repository was archived by the owner on Dec 21, 2024. It is now read-only.

Commit c15337a

Browse files
committed
Consistent control of caller stack frames output
This commit aims to achieve consistent stack frames control by `stack_info` flag in logging calls the same as that in python logging module. Currently, both a field named `stack_info` for JsonFormatter and `stack_info=True` for logging function are needed for stack frames to be properly displayed. If `stack_info` field name is not passed, no output is given irrespective of the `stack_info` flag in logging calls. If `stack_info=False` in logging calls and `stack_info` is also passed as one field for JsonFormatter, the output is `{"stack_info": null}`. Above behavior is not very consistent and requires two controls to output stack frames. This commit makes 'stack_info' flag in logging calls as single point control of stack frames output and there is no need to pass `stack_info` field into JsonFormatter.
1 parent 12ab1c3 commit c15337a

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/pythonjsonlogger/jsonlogger.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def format_datetime_obj(self, obj):
7777
class JsonFormatter(logging.Formatter):
7878
"""
7979
A custom formatter to format logging records as json strings.
80-
extra values will be formatted as str() if nor supported by
80+
Extra values will be formatted as str() if not supported by
8181
json default encoder
8282
"""
8383

@@ -193,6 +193,10 @@ def format(self, record):
193193
message_dict['exc_info'] = self.formatException(record.exc_info)
194194
if not message_dict.get('exc_info') and record.exc_text:
195195
message_dict['exc_info'] = record.exc_text
196+
# Display formatted record of stack frames
197+
# default format is a string returned from :func:`traceback.print_stack`
198+
if record.stack_info and not message_dict.get('stack_info'):
199+
message_dict['stack_info'] = self.formatStack(record.stack_info)
196200

197201
try:
198202
log_record = OrderedDict()

0 commit comments

Comments
 (0)