-
Notifications
You must be signed in to change notification settings - Fork 461
Description
Expected Behaviour
After exiting append_context_keys, all keys that existed before the context (and their values) should be restored.
Current Behaviour
Logger.append_context_keys states to “temporarily add logging keys”, but when a temporary key shares the same name as an existing key, the original key/value is permanently removed after the context exits. This breaks typical request-level logging flows where immutable context (e.g., order_id, user_id) is set once and child functions use the context manager to override/add keys briefly. After the first overlap, all later log lines lose the original key.
Code snippet
from aws_lambda_powertools import Logger
logger = Logger(service="orders")
def ship_order(order_id: str):
logger.append_keys(order_id=order_id)
logger.info("Received order")
with logger.append_context_keys(order_id="ORD-CHILD"):
logger.info("Processing shipment")
# Expected: original order_id still present.
# Actual: order_id key vanished entirely.
logger.info("Order completed")
if __name__ == "__main__":
ship_order("ORD-123"){"level":"INFO","location":"ship_order:9","message":"Received order","timestamp":"2025-11-15 13:18:57,825+0800","service":"orders","order_id":"ORD-123"}
{"level":"INFO","location":"ship_order:13","message":"Processing shipment","timestamp":"2025-11-15 13:18:57,825+0800","service":"orders","order_id":"ORD-CHILD"}
{"level":"INFO","location":"ship_order:17","message":"Order completed","timestamp":"2025-11-15 13:18:57,825+0800","service":"orders"}
Possible Solution
Root cause appears in LambdaPowertoolsFormatter.append_context_keys: it adds the keys and unconditionally calls remove_keys on exit, rather than restoring previous values. A fix could snapshot overlapping keys before the context and restore them afterward, or only remove keys that didn’t exist beforehand.
Steps to Reproduce
Run the repro scripts.
Powertools for AWS Lambda (Python) version
latest
AWS Lambda function runtime
3.12
Packaging format used
PyPi
Debugging logs
Metadata
Metadata
Assignees
Labels
Type
Projects
Status