|
1 | 1 | import logging |
2 | 2 | import numbers |
3 | | -import traceback |
4 | 3 | from types import TracebackType |
5 | 4 | from typing import Any, Dict, List, Optional, Tuple, Type, cast |
6 | 5 |
|
@@ -92,25 +91,6 @@ def format_value(cls, value) -> str: |
92 | 91 |
|
93 | 92 | return cls.format_string(str(value)) |
94 | 93 |
|
95 | | - @classmethod |
96 | | - def format_exc_info(cls, exc_info: ExcInfo) -> str: |
97 | | - """ |
98 | | - Format the provided exc_info into a logfmt formatted string. |
99 | | -
|
100 | | - This function should only be used to format exceptions which are |
101 | | - currently being handled. Not with those exceptions which are |
102 | | - manually passed into the logger. For example: |
103 | | -
|
104 | | - try: |
105 | | - raise Exception() |
106 | | - except Exception: |
107 | | - logging.exception() |
108 | | - """ |
109 | | - # Tracebacks have a single trailing newline that we don't need. |
110 | | - value = "".join(traceback.format_exception(*exc_info)).rstrip("\n") |
111 | | - |
112 | | - return cls.format_string(value) |
113 | | - |
114 | 94 | @classmethod |
115 | 95 | def format_params(cls, params: dict) -> str: |
116 | 96 | """ |
@@ -246,10 +226,13 @@ def format(self, record: logging.LogRecord) -> str: |
246 | 226 | if formatted_params: |
247 | 227 | tokens.append(formatted_params) |
248 | 228 |
|
249 | | - if record.exc_info: |
| 229 | + if record.exc_info and not record.exc_text: |
250 | 230 | # Cast exc_info to its not null variant to make mypy happy. |
251 | 231 | exc_info = cast(ExcInfo, record.exc_info) |
252 | | - tokens.append(f"exc_info={self.format_exc_info(exc_info)}") |
| 232 | + record.exc_text = self.formatException(exc_info) |
| 233 | + |
| 234 | + if record.exc_text: |
| 235 | + tokens.append(f"exc_info={self.format_string(record.exc_text)}") |
253 | 236 |
|
254 | 237 | if record.stack_info: |
255 | 238 | stack_info = self.formatStack(record.stack_info).rstrip("\n") |
|
0 commit comments