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
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Thanks fir the PR @oyiz-michael!

APPROVED!

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 5, 2026

@codecov
Copy link

codecov bot commented Jan 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.54%. Comparing base (ef3da99) to head (692022d).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #7846   +/-   ##
========================================
  Coverage    96.54%   96.54%           
========================================
  Files          275      275           
  Lines        13194    13194           
  Branches       999      999           
========================================
  Hits         12738    12738           
  Misses         353      353           
  Partials       103      103           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena leandrodamascena merged commit eceb809 into aws-powertools:develop Jan 5, 2026
15 checks passed
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

2 participants