Skip to content

fix(logger): reject reserved LogRecord attributes in append_keys - #8832

Open
HarshRajSinghania wants to merge 1 commit into
aws-powertools:developfrom
HarshRajSinghania:fix/reject-reserved-log-attrs
Open

HarshRajSinghania wants to merge 1 commit into
aws-powertools:developfrom
HarshRajSinghania:fix/reject-reserved-log-attrs

Conversation

@HarshRajSinghania

Copy link
Copy Markdown

Summary

Reject reserved LogRecord attributes when calling append_keys() and thread_safe_append_keys() by raising a clear ValueError at call time.

Motivation

Fixes #8831. Currently, appending a reserved key like lineno is 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

  • Imported RESERVED_LOG_ATTRS from the formatter.
  • Added validation in both append_keys and thread_safe_append_keys on the Logger class to check for colliding keys and raise ValueError with the key name.
  • Added functional tests verifying the exception and that subsequent logging continues to work.

Testing

  • Cloned the fork and installed in editable mode.
  • Ran the new tests: 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.
  • Ran the full test file: 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

Raise ValueError when attempting to append keys that collide with reserved LogRecord attributes, preventing silent logging failures.

Fixes aws-powertools#8831
@HarshRajSinghania
HarshRajSinghania requested a review from a team as a code owner November 15, 2026 15:37
@boring-cyborg

boring-cyborg Bot commented Nov 15, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@powertools-for-aws-oss-automation

Copy link
Copy Markdown

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 .github/PULL_REQUEST_TEMPLATE.md#acknowledgment

@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 15, 2026
@sonarqubecloud

Copy link
Copy Markdown

@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 15, 2026

@Manishram-ai Manishram-ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +812 to +814
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.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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__}'.",
)

Comment on lines +863 to +865
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.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same change for the thread-safe version.

Suggested change
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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the type check, levelname="INFO" is a valid template and doesn't raise, so this test needs a non-string value.

Suggested change
logger.thread_safe_append_keys(levelname="INFO")
logger.thread_safe_append_keys(levelname=20)

This branch has not been deployed

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

Labels

logger size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: appending a reserved attribute with a non-string value breaks every later log line

3 participants