Fix double rendering of exception tracebacks in stdlib integration#827
Open
M97Chahboun wants to merge 3 commits into
Open
Fix double rendering of exception tracebacks in stdlib integration#827M97Chahboun wants to merge 3 commits into
M97Chahboun wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes an issue where exception tracebacks are rendered twice when using
structlog's standard library integration withlogger.exception().Previously, if a processor (like
format_exc_infoor a custom renderer) formatted the exception and added it to theevent_dict, the underlying standard librarylogging.Logger.exception()would still receiveexc_info=Trueby default and render the traceback a second time.To resolve this, this PR introduces an in-band signaling mechanism. The
_proxy_to_loggermethod inBoundLoggernow checks for a_structlog_exception_already_renderedflag in the event dictionary (or within theextrakwargs ifrender_to_log_kwargsis used). If this flag is present andTrue, the wrapper suppresses the automatic exception info by explicitly passingexc_info=Falseto 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
mainbranch. (Note: Please ensure you create a new branch likefix/double-exception-renderingbefore submitting!)tests/test_exc_info_double_render.py)api.py. (N/A: This is an internal behavioral fix, no new public API is exposed.).rstand.mdfiles is written using semantic newlines.