Skip to content

Conversation

@oyiz-michael
Copy link
Contributor

Issue number: closes #7690

Summary

This PR clarifies the behavior of append_context_keys() when keys overlap with existing keys, addressing user confusion reported in #7690. This is a documentation-only fix that explains the current implementation without changing functionality.

Changes

  • Updated docstrings in formatter.py and logger.py:

    • Added Warning section explaining that all keys added within the context are removed on exit
    • Clarified that overlapping keys lose their original values after context exit
    • Recommended using append_keys() for persistent keys
  • Enhanced documentation in docs/core/logger.md:

    • Added prominent "danger" callout box explaining key collision behavior
    • Included concrete code example showing what happens when keys overlap
    • Demonstrates the issue: persistent key order_id="ORD-123" is lost after using append_context_keys(order_id="ORD-CHILD")

User experience

Before:

  • Users expected Python context manager conventions (restore state on exit)
  • Documentation said "temporarily add logging keys" without clarifying removal behavior
  • When keys overlapped, original values were silently lost, breaking logging flows
  • No guidance on avoiding key name collisions

After:

  • Clear warnings in both code docstrings and user-facing documentation
  • Concrete example showing the key collision problem
  • Explicit recommendation: use append_keys() for persistent context, avoid overlapping key names with append_context_keys()
  • Users understand this is current expected behavior, not a bug

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

Fixes aws-powertools#7690

Clarifies that append_context_keys() removes all added keys on exit,
including keys that already existed with the same name. This addresses
user confusion about the 'temporarily' behavior when keys overlap.

Changes:
- Updated docstrings in formatter.py and logger.py with clear warning
- Added detailed documentation in logger.md with collision example
- Explains that original values are lost when keys overlap
- Recommends using append_keys() for persistent keys to avoid collisions

This is a documentation-only fix that clarifies current behavior without
changing functionality.
@oyiz-michael oyiz-michael requested a review from a team as a code owner December 20, 2025 01:26
@pull-request-size pull-request-size bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Dec 20, 2025
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation logger labels Dec 20, 2025
@sonarqubecloud
Copy link

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

Labels

documentation Improvements or additions to documentation logger size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: append_context_keys drops previously appended keys when temporary keys overlap

1 participant