|
16 | 16 | # under the License.
|
17 | 17 |
|
18 | 18 | import logging
|
| 19 | +import sys |
19 | 20 | import time
|
20 | 21 | from traceback import format_tb
|
21 | 22 | from ._meta import ECS_VERSION
|
@@ -112,6 +113,34 @@ def __init__(self, stack_trace_limit=None, exclude_fields=()):
|
112 | 113 | self._exclude_fields = frozenset(exclude_fields)
|
113 | 114 | self._stack_trace_limit = stack_trace_limit
|
114 | 115 |
|
| 116 | + def _record_error_type(self, record): |
| 117 | + # type: (logging.LogRecord) -> Optional[str] |
| 118 | + exc_info = record.exc_info |
| 119 | + if not exc_info: |
| 120 | + # exc_info is either an iterable or bool. If it doesn't |
| 121 | + # evaluate to True, then no error type is used. |
| 122 | + return None |
| 123 | + if isinstance(exc_info, bool): |
| 124 | + # if it is a bool, then look at sys.exc_info |
| 125 | + exc_info = sys.exc_info() |
| 126 | + if isinstance(exc_info, (list, tuple)) and exc_info[0] is not None: |
| 127 | + return exc_info[0].__name__ |
| 128 | + return None |
| 129 | + |
| 130 | + def _record_error_message(self, record): |
| 131 | + # type: (logging.LogRecord) -> Optional[str] |
| 132 | + exc_info = record.exc_info |
| 133 | + if not exc_info: |
| 134 | + # exc_info is either an iterable or bool. If it doesn't |
| 135 | + # evaluate to True, then no error message is used. |
| 136 | + return None |
| 137 | + if isinstance(exc_info, bool): |
| 138 | + # if it is a bool, then look at sys.exc_info |
| 139 | + exc_info = sys.exc_info() |
| 140 | + if isinstance(exc_info, (list, tuple)) and exc_info[1]: |
| 141 | + return str(exc_info[1]) |
| 142 | + return None |
| 143 | + |
115 | 144 | def format(self, record):
|
116 | 145 | # type: (logging.LogRecord) -> str
|
117 | 146 | result = self.format_to_ecs(record)
|
@@ -145,14 +174,8 @@ def format_to_ecs(self, record):
|
145 | 174 | "process.name": self._record_attribute("processName"),
|
146 | 175 | "process.thread.id": self._record_attribute("thread"),
|
147 | 176 | "process.thread.name": self._record_attribute("threadName"),
|
148 |
| - "error.type": lambda r: ( |
149 |
| - r.exc_info[0].__name__ |
150 |
| - if (r.exc_info is not None and r.exc_info[0] is not None) |
151 |
| - else None |
152 |
| - ), |
153 |
| - "error.message": lambda r: ( |
154 |
| - str(r.exc_info[1]) if r.exc_info and r.exc_info[1] else None |
155 |
| - ), |
| 177 | + "error.type": self._record_error_type, |
| 178 | + "error.message": self._record_error_message, |
156 | 179 | "error.stack_trace": self._record_error_stack_trace,
|
157 | 180 | } # type: Dict[str, Callable[[logging.LogRecord],Any]]
|
158 | 181 |
|
|
0 commit comments