Repository navigation
Closed as not planned
Labels
feature-requestfeature requestfeature requestloggerneed-customer-feedbackRequires more customers feedback before making or revisiting a decisionRequires more customers feedback before making or revisiting a decisionon-holdThis item is on-hold and will be revisited in the futureThis item is on-hold and will be revisited in the futurerevisit-in-3-monthsRequires more customers feedback before making or revisiting a decisionRequires more customers feedback before making or revisiting a decision
Description
Use case
Add custom keys to logs that should persist on every lambda execution and NOT be cleared when using clear_state.
This functionality already exists in the TypeScript version of powertools and can be useful for Python as well.
Solution/User Experience
Just like in the TypeScript version, add a new optional parameter to the Logger initializer. For example, the new option can be called persistent_keys.
This parameter could be a simple flat dict(or similar type?), and if not provided, default to an empty dict.
The value of this new parameter could either be added to self._default_log_keys(since what ever is in that already persists through a state clearing) or be maintained separately but treated just like self._default_log_keys.
Example init:
import os
from aws_lambda_powertools import Logger
logger = Logger(persistent_keys={"role": os.getenv("MY_ROLE", "MY_ROLE")Example storing keys:
class Logger:
...
def __init__(
...
persistent_keys: dict[str, Any] = {},
...
) -> None:
...
self._default_log_keys = {"service": self.service, "sampling_rate": self.sampling_rate}
if persistent_keys:
self._default_log_keys.update(persistent_keys)
...Alternative solutions
Acknowledgment
- This feature request meets Powertools for AWS Lambda (Python) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Java, TypeScript, and .NET
Metadata
Metadata
Assignees
Labels
feature-requestfeature requestfeature requestloggerneed-customer-feedbackRequires more customers feedback before making or revisiting a decisionRequires more customers feedback before making or revisiting a decisionon-holdThis item is on-hold and will be revisited in the futureThis item is on-hold and will be revisited in the futurerevisit-in-3-monthsRequires more customers feedback before making or revisiting a decisionRequires more customers feedback before making or revisiting a decision
Type
Projects
- StatusShow more project fieldsComing soon
Activity
boring-cyborg commentedon Feb 3, 2025
Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link
leandrodamascena commentedon Feb 10, 2025
Hi @jth08527! Thanks a lot for opening this issue! I'll need to do some research on the impact of this issue because I don't know if the API will be confused with
append_keysandpersistent_keys. I'm adding this to our backlog and aiming to take a look at it later this month.dcabib commentedon Aug 29, 2025
Hey @leandrodamascena! 👋 I've done a comprehensive analysis of the potential API confusion between
append_keysandpersistent_keysthat you mentioned in issue #6002. Here's my detailed investigation and recommendations.🔍 Issue Summary
The request is to add a
persistent_keysparameter to Logger (similar to TypeScript version) that would persist even whenclear_state()is called, unlike the currentappend_keys()which gets cleared.🎯 Current Python Implementation Analysis
Detailed Code Flow Analysis:
1. Logger Constructor (
__init__)2.
append_keys()Method Chain:3.
clear_state()Method Chain:4.
structure_logs()Method (Key Restoration):Critical Current Behaviors:
append_keys()→ Directly updatesformatter.log_formatdict (temporary keys)clear_state()→ Resets formatter, then restores_default_log_keysviastructure_logs()_default_log_keys→{"service": self.service, "sampling_rate": self.sampling_rate}- ALWAYS surviveContextVarfor thread-local temporary keysCurrent State Management:
_default_log_keysonlylog_formatdict + thread-localContextVarCurrent Flow Example:
🚨 API Confusion Concerns (Validated!)
1. Naming & Semantic Confusion
2. TypeScript vs Python Inconsistency
resetKeys()vs Python:clear_state()appendKeys()ANDappendPersistentKeys()append_keys()currently3. Multiple Ways to Set Persistent Data
🔧 Technical Implementation Challenges
1. State Management Complexity
2. Key Conflict Resolution
3. Clear State Behavior
💡 Discovered Issues from Code Analysis
1. Formatter vs Logger Responsibility
formatter.clear_state()2.
structure_logs()Method Overloading3. Thread Safety with Context Variables
The formatter uses
ContextVarfor thread-local keys:Persistent keys would need similar thread-safe handling.
🎯 Recommendations
Option 1: Follow TypeScript API Exactly ⭐ RECOMMENDED
Pros:
Option 2: Enhance Current API with Clear Naming
Pros:
Option 3: Single Method with Scope Parameter
Pros:
🚧 Implementation Strategy (Option 1)
Phase 1: Internal Refactoring
Phase 2: Add New Methods
Phase 3: Formatter Updates
Backwards Compatibility Strategy:
persistent_keys=None(optional, no breaking change)append_keys()andclear_state()working exactly as beforeappend_temporary_keys()Migration Path:
🔍 Edge Cases to Test
📊 Risk Assessment
🎯 Next Steps
💭 Final Thoughts
Your concern about API confusion is 100% valid! The distinction between temporary and persistent keys isn't immediately obvious. Following the TypeScript approach with explicit method names (
append_persistent_keys,remove_persistent_keys) would provide the clearest API while maintaining consistency across Powertools languages.The technical implementation is definitely feasible, but requires careful coordination between Logger and Formatter classes to maintain backwards compatibility while adding the new persistent behavior.
Ready to dive deeper into any specific aspect! 🚀
jth08527 commentedon Sep 3, 2025
Wow, thank you for the incredibly detailed analysis and write-up, @dcabib!
For what it's worth, my 2 cents as a user is to have the different languages to be as similar as possible. The fewer amount of differences, the easier it is to use this library if users ever have to hop around between projects of different languages.
dreamorosi commentedon Sep 8, 2025
Hi both, feature parity and having different versions of Powertools for AWS being as similar as possible to each others is definitely something important to us.
For this specific item we're not yet ready to make a decision because we're working on some items that we'll share over the coming weeks and that might impact this area of the code.
For now I'll put this issue on hold, but we'll revisit it before end of the year.
leandrodamascena commentedon Nov 15, 2026
This has been on hold for over a year waiting for more people to ask for it, and nobody did. Closing to keep the backlog honest. Reopen if you still need it and describe the case;
append_keysplusclear_state=Falsecovers most of what was asked here.powertools-for-aws-oss-automation commentedon Nov 15, 2026
Warning
This issue is now closed. Please be mindful that future comments are hard for our team to see.
If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.