Skip to content

Commit 1fc7471

Browse files
committed
docs: clarify append_context_keys behavior with overlapping keys
Fixes #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.
1 parent 1041188 commit 1fc7471

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

aws_lambda_powertools/logging/formatter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ def append_context_keys(self, **additional_keys: Any) -> Generator[None, None, N
281281
**additional_keys: Any
282282
Key-value pairs to include in the log context during the lifespan of the context manager.
283283
284+
Warning
285+
-------
286+
All keys added within this context are removed when exiting, even if they existed before.
287+
If a key with the same name already exists, the original value will be lost after the context exits.
288+
To persist keys across multiple log messages, use `append_keys()` instead.
289+
284290
Example
285291
--------
286292
logger = Logger(service="example_service")

aws_lambda_powertools/logging/logger.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,12 @@ def append_context_keys(self, **additional_keys: Any) -> Generator[None, None, N
820820
**additional_keys: Any
821821
Key-value pairs to include in the log context during the lifespan of the context manager.
822822
823+
Warning
824+
-------
825+
All keys added within this context are removed when exiting, even if they existed before.
826+
If a key with the same name already exists, the original value will be lost after the context exits.
827+
To persist keys across multiple log messages, use `append_keys()` instead.
828+
823829
Example
824830
--------
825831
**Logging with contextual keys**

docs/core/logger.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ You can append your own keys to your existing Logger via `append_keys(**addition
195195

196196
The append_context_keys method allows temporary modification of a Logger instance's context without creating a new logger. It's useful for adding context keys to specific workflows while maintaining the logger's overall state and simplicity.
197197

198+
???+ danger "Important: Keys are removed on context exit, even if they existed before"
199+
All keys added within the context are removed when exiting, **including keys that already existed with the same name**.
200+
201+
If you need to temporarily override a key's value while preserving the original, use `append_keys()` for persistent keys and avoid key name collisions with `append_context_keys()`.
202+
203+
**Example of key collision:**
204+
```python
205+
logger.append_keys(order_id="ORD-123") # Persistent key
206+
logger.info("Order received") # Has order_id="ORD-123"
207+
208+
with logger.append_context_keys(order_id="ORD-CHILD"): # Overwrites
209+
logger.info("Processing") # Has order_id="ORD-CHILD"
210+
211+
logger.info("Order completed") # order_id key is now MISSING!
212+
```
213+
198214
=== "append_context_keys.py"
199215

200216
```python hl_lines="7 8"

0 commit comments

Comments
 (0)