Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 40ccb83

Browse files
committedJan 6, 2025
Override formatException and formatStack
1 parent 571c88e commit 40ccb83

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed
 

‎src/pythonjsonlogger/core.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,11 @@ def format(self, record: logging.LogRecord) -> str:
253253
if not message_dict.get("exc_info") and record.exc_text:
254254
message_dict["exc_info"] = record.exc_text
255255

256-
if self.exc_info_as_array and message_dict.get("exc_info"):
257-
message_dict["exc_info"] = message_dict["exc_info"].splitlines()
258-
259256
# Display formatted record of stack frames
260257
# default format is a string returned from :func:`traceback.print_stack`
261258
if record.stack_info and not message_dict.get("stack_info"):
262259
message_dict["stack_info"] = self.formatStack(record.stack_info)
263260

264-
if self.stack_info_as_array and message_dict.get("stack_info"):
265-
message_dict["stack_info"] = message_dict["stack_info"].splitlines()
266-
267261
log_record: LogRecord = {}
268262
self.add_fields(log_record, record, message_dict)
269263
log_record = self.process_log_record(log_record)
@@ -380,3 +374,23 @@ def process_log_record(self, log_record: LogRecord) -> LogRecord:
380374
log_record: incoming data
381375
"""
382376
return log_record
377+
378+
def formatException(self, ei) -> Union[str, list[str]]:
379+
"""Format and return the specified exception information.
380+
381+
If exc_info_as_array is set to True, This method returns an array of strings.
382+
"""
383+
exception_info_str = super().formatException(ei)
384+
return exception_info_str.splitlines() if self.exc_info_as_array else exception_info_str
385+
386+
def formatStack(self, stack_info) -> Union[str, list[str]]:
387+
"""Format and return the specified stack information.
388+
389+
If stack_info_as_array is set to True, This method returns an array of strings.
390+
"""
391+
stack_info_str = super().formatStack(stack_info)
392+
return (
393+
stack_info_str.splitlines()
394+
if self.stack_info_as_array
395+
else stack_info_str.formatStack(stack_info)
396+
)

0 commit comments

Comments
 (0)
Please sign in to comment.