Skip to content

Fix double rendering of exception tracebacks in stdlib integration#827

Open
M97Chahboun wants to merge 3 commits into
hynek:mainfrom
M97Chahboun:main
Open

Fix double rendering of exception tracebacks in stdlib integration#827
M97Chahboun wants to merge 3 commits into
hynek:mainfrom
M97Chahboun:main

Conversation

@M97Chahboun

Copy link
Copy Markdown

This PR fixes an issue where exception tracebacks are rendered twice when using structlog's standard library integration with logger.exception().

Previously, if a processor (like format_exc_info or a custom renderer) formatted the exception and added it to the event_dict, the underlying standard library logging.Logger.exception() would still receive exc_info=True by default and render the traceback a second time.

To resolve this, this PR introduces an in-band signaling mechanism. The _proxy_to_logger method in BoundLogger now checks for a _structlog_exception_already_rendered flag in the event dictionary (or within the extra kwargs if render_to_log_kwargs is used). If this flag is present and True, the wrapper suppresses the automatic exception info by explicitly passing exc_info=False to the underlying logger.

This approach perfectly aligns with the discussion in #590, allowing processors to take full control of exception rendering without breaking backward compatibility for standard use cases.

Pull Request Checklist

  • I acknowledge this project's AI policy.
  • This pull request is not from my main branch. (Note: Please ensure you create a new branch like fix/double-exception-rendering before submitting!)
  • There's tests for all new and changed code. (Added tests/test_exc_info_double_render.py)
  • New APIs are added to our typing tests in api.py. (N/A: This is an internal behavioral fix, no new public API is exposed.)
  • Updated documentation for changed code. (Added a note in the stdlib documentation regarding this behavior.)
  • Documentation in .rst and .md files is written using semantic newlines.
  • Changes are documented in the changelog. (See snippet below)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant