hynek / structlog

Simple, powerful, and fast logging for Python.
https://www.structlog.org/
Other
3.6k stars 225 forks source link

ExceptionDictTransformer issues AttributeError when `exc_info = (None, None, None)` #656

Closed yaelmi3 closed 3 weeks ago

yaelmi3 commented 2 months ago

We have been using structlog for a while, along with the ExceptionDictTransformer processor, which integrates wonderfully with DataDog. Recently, we encountered a minor but frustrating issue in our production environment. This issue arose from logging exceptions without the exc_info parameter, for example.

The issue occurs because ExceptionDictTransformer calls extract before exc_info verification opposed to _format_exception which returns MISSING

full trace:

--- Logging error ---
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/logging/__init__.py", line 1110, in emit
    msg = self.format(record)
          ^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/logging/__init__.py", line 953, in format
    return fmt.format(record)
           ^^^^^^^^^^^^^^^^^^
  File "/Users/kernel-panic/cronenberg/venv/lib/python3.11/site-packages/structlog/stdlib.py", line 1059, in format
    ed = proc(logger, meth_name, ed)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kernel-panic/cronenberg/venv/lib/python3.11/site-packages/dd_json_logger/custom_processors.py", line 85, in format_exception_as_json
    return ExceptionRenderer(ExceptionDictTransformer(show_locals=False))(logger, name, event_dict)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kernel-panic/cronenberg/venv/lib/python3.11/site-packages/structlog/processors.py", line 412, in __call__
    event_dict["exception"] = self.format_exception(
                              ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kernel-panic/cronenberg/venv/lib/python3.11/site-packages/structlog/tracebacks.py", line 262, in __call__
    trace = extract(
            ^^^^^^^^
  File "/Users/kernel-panic/cronenberg/venv/lib/python3.11/site-packages/structlog/tracebacks.py", line 156, in extract
    exc_type=safe_str(exc_type.__name__),
                      ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '__name__'