Repository navigation
fix(logger): reject reserved LogRecord attributes in append_keys - #8832
HarshRajSinghania wants to merge 1 commit into
Conversation
Raise ValueError when attempting to append keys that collide with reserved LogRecord attributes, preventing silent logging failures. Fixes aws-powertools#8831
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
|
No acknowledgement section found. Please make sure you used the template to open a PR and didn't remove the acknowledgment section. Check the template at |
|
Manishram-ai
left a comment
There was a problem hiding this comment.
Thanks for picking this up. I ran the logger tests on this branch, and 16 tests in test_logger_utils.py fail with ValueError: Key 'name' is a reserved LogRecord attribute. copy_config_to_registered_loggers calls source_logger.append_keys(name="%(name)s"), so it raises as soon as it configures a logger. The issue also allows rejecting only values that can't be formatted, and checking the value type instead of the key name fixes this while still catching lineno=5 up front. With the suggestions below, tests/functional/logger and tests/unit/logger pass (206 tests), and ruff and mypy are clean.
| for key in additional_keys: | ||
| if key in RESERVED_LOG_ATTRS: | ||
| raise ValueError(f"Key '{key}' is a reserved LogRecord attribute and cannot be used.") |
There was a problem hiding this comment.
String values for these keys work today, because the formatter fills them in as % templates. So append_keys(filename="report.csv") prints fine on develop but raises with this check. The formatter only rejects non-string values, so this raises for the same values, just at call time. It also lets the append_keys(stack_info="%(stack_info)s") call in the new test from #8837 through.
| for key in additional_keys: | |
| if key in RESERVED_LOG_ATTRS: | |
| raise ValueError(f"Key '{key}' is a reserved LogRecord attribute and cannot be used.") | |
| for key, value in additional_keys.items(): | |
| if key in RESERVED_LOG_ATTRS and value and not isinstance(value, str): | |
| raise ValueError( | |
| f"Key '{key}' is a reserved LogRecord attribute and needs a str value, " | |
| f"instead got '{type(value).__name__}'.", | |
| ) |
| for key in additional_keys: | ||
| if key in RESERVED_LOG_ATTRS: | ||
| raise ValueError(f"Key '{key}' is a reserved LogRecord attribute and cannot be used.") |
There was a problem hiding this comment.
Same change for the thread-safe version.
| for key in additional_keys: | |
| if key in RESERVED_LOG_ATTRS: | |
| raise ValueError(f"Key '{key}' is a reserved LogRecord attribute and cannot be used.") | |
| for key, value in additional_keys.items(): | |
| if key in RESERVED_LOG_ATTRS and value and not isinstance(value, str): | |
| raise ValueError( | |
| f"Key '{key}' is a reserved LogRecord attribute and needs a str value, " | |
| f"instead got '{type(value).__name__}'.", | |
| ) |
| # WHEN attempting to append a reserved LogRecord attribute in thread-safe method | ||
| # THEN it should raise ValueError | ||
| with pytest.raises(ValueError, match="Key 'levelname' is a reserved LogRecord attribute"): | ||
| logger.thread_safe_append_keys(levelname="INFO") |
There was a problem hiding this comment.
With the type check, levelname="INFO" is a valid template and doesn't raise, so this test needs a non-string value.
| logger.thread_safe_append_keys(levelname="INFO") | |
| logger.thread_safe_append_keys(levelname=20) |



Summary
Reject reserved LogRecord attributes when calling
append_keys()andthread_safe_append_keys()by raising a clearValueErrorat call time.Motivation
Fixes #8831. Currently, appending a reserved key like
linenois accepted but causes subsequent log formatting to fail silently (swallowed by the stdlib as a logging error), with no indication of the root cause.Implementation
RESERVED_LOG_ATTRSfrom the formatter.append_keysandthread_safe_append_keyson theLoggerclass to check for colliding keys and raiseValueErrorwith the key name.Testing
pytest tests/functional/logger/required_dependencies/test_logger.py::test_append_keys_rejects_reserved_log_attrs tests/functional/logger/required_dependencies/test_logger.py::test_thread_safe_append_keys_rejects_reserved_log_attrs -v→ 2 passed.pytest tests/functional/logger/required_dependencies/test_logger.py -q→ 91 passed (including the 2 new ones), 2 pre-existing warnings.AI-assisted work; all tests were executed and results observed as reported above.
Fixes #8831