Feature request: convenience `reset_state` method in Logger
Use case
[!note] This is a cross-language feature request that was originally brought up under aws-powertools/powertools-lambda-typescript#2337
As a customer who uses canonical logging - aka wide logging - I would like to be able to clear any key I might have appended to the Logger instance regardless of me using the default formatter or the decorator - both of which either expose a method along these lines or can be configured to automatically clear state.
Solution/User Experience
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger()
def lambda_handler(event: dict, context: LambdaContext) -> str:
try:
logger.append_keys(order_id=event.get('order_id', None)
finally:
logger.info('CANONICAL')
logger.clear_state()
Alternative solutions
N/A
Acknowledgment
- [X] 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
Awesome, thank you for opening this @dreamorosi ! We'll work on this tomorrow
Meanwhile we don't submit a PR for a top level method, this is possible today like this:
logger.registered_formatter.clear_state()
The top level method should delegate to formatter
Hello @heitorlessa and @dreamorosi! I noticed in the issue https://github.com/aws-powertools/powertools-lambda-typescript/issues/2337 that you two discussed how to resolve a "conceptual" bug with our current method names (persistent vs non-persistent) and their behavior. I see that some things are marked as deprecated to be removed in the next major release. As we're about to release Powertools for AWS Lambda (Python) v3, we have an opportunity to review this here.
My idea is:
1 - Create a new method and call it add_persistent_keys so that those keys will persist even the clear_state() method is invoked. This method will allow developers to explicitly define the keys that should remain until the Logger instance is destroyed.
2 - Regarding the current append_keys method, I don't think we need to change its functionality. However, we should improve the documentation and potentially add a workflow example to clarify that this method behaves like a "temporary keys" mechanism. The keys added through append_keys will be removed when clear_state() is called, as it is currently designed.
I might be misunderstanding something regarding the TypeScript issue, and please correct me if this is the case. If not, and you agree with my proposal, I think we can proceed with implementing this change for Powertools for AWS Lambda (Python) v3.
Hey Leo, I'm unsure on how to move forward with that feature and even more unsure whether to recommend that you skip it entirely in Python or not.
The clear_state method is fine, but the persistent/temporary distinction is a bit messy due to how it was naively implemented in our repo.
With Heitor we already hashed out the new API and wording (my second to last comment in the PR), but now there are some questions on how to actually implement it as fixing the implementation requires a different merging strategy for the attributes. I plan on fixing this later next week, or the week after once I'm done with the Madrid Summit.
It's important to highlight here that this distinction between persistent and temporary attributes was added to TS as a deviation by the original implementer and not in response to customer demand, at least afaik.
While there are valid use cases for this type of feature, I don't know that we have evidence of customers actually needing/using it, so the decision to add it to Python is up to you.
Hello @dreamorosi! Thank you for your always valuable insights! I'm going to leave this here on hold until you make progress on the TypeScript PR and see if we can adopt the same in Python. If possible, it's good because we can start to reduce the differences in language implementations, if not I'll move forward and implement only the clear_state method.