Fix: prevent PositionalArgumentsFormatter from crashing on TypeError (#258) - #832
Open
newchannelid432-code wants to merge 3 commits into
Open
Conversation
When PositionalArgumentsFormatter encounters a mismatch between the
event string and the positional args (e.g. 'Info message' with args=('x',)),
it raises an uncaught TypeError, crashing the application.
This behavior differs from the Python stdlib logging module, which
gracefully catches formatting errors to prevent logging from taking
down the application.
This patch wraps the formatting operation in a try/except block
catching TypeError and ValueError. If formatting fails, the event
string is left unformatted and the application continues running.
Added a regression test to ensure this behavior is maintained.
Closes hynek#258
for more information, see https://pre-commit.ci
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.
Fixes #258.
Problem
When using
structlogwithPositionalArgumentsFormatter, passing a positional argument that does not match a formatting placeholder in the event string (e.g.,log.warning('Info message', 'x')) raises an uncaughtTypeError: not all arguments converted during string formatting. This crashes the entire application.Root Cause
In
src/structlog/stdlib.py, the formatter directly executesevent_dict["event"] %= argswithout catching formatting exceptions. This differs from the Python standard libraryloggingmodule, which gracefully catchesTypeErrorandValueErrorduring formatting to ensure logging never crashes the host application.Solution
This PR wraps the
%=formatting operation in atry...except (TypeError, ValueError)block. If formatting fails due to a user error (mismatched placeholders), the exception is caught and the event string is simply left unformatted. This mimics the fail-safe behavior of stdlib logging and ensures that bad log statements do not result in fatal runtime errors.A regression test has been added to
tests/test_stdlib.pyto verify that the processor gracefully handles mismatched placeholders without raising an exception.