Skip to content

Feature request: convenience reset_state method in Logger #4134

Description

@dreamorosi

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

Activity

  1. added
    triage
    feature-request
    on Apr 15, 2024
  2. rubenfonseca commented on Apr 15, 2024

    @rubenfonseca
    Contributor

    Awesome, thank you for opening this @dreamorosi ! We'll work on this tomorrow

  3. removed
    triage
    on Apr 15, 2024
  4. moved this from Triage to Working on it in Powertools for AWS Lambda (Python)on Apr 15, 2024
  5. self-assigned this
    on Apr 15, 2024
  6. heitorlessa commented on May 9, 2024

    @heitorlessa
    Contributor

    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

  7. leandrodamascena commented on May 29, 2024

    @leandrodamascena
    Contributor

    Hello @heitorlessa and @dreamorosi! I noticed in the issue aws-powertools/powertools-lambda-typescript#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.

  8. dreamorosi commented on May 29, 2024

    @dreamorosi
    ContributorAuthor

    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.

  9. leandrodamascena commented on May 31, 2024

    @leandrodamascena
    Contributor

    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.

  10. changed the title [-]Feature request: convenience `clear_state` method in Logger[/-] [+]Feature request: convenience `reset_state` method in Logger[/+] on Jun 9, 2024
  11. added
    help wanted
    on Jun 9, 2024
  12. self-assigned this
    on Jan 24, 2025
  13. moved this from Backlog to Working on it in Powertools for AWS Lambda (Python)on Jan 30, 2025
  14. linked a pull request that will close this issuefeat(logger): add clear_state method #5956on Jan 30, 2025
  15. github-actions commented on Jan 31, 2025

    @github-actions
    Contributor

    ⚠️COMMENT VISIBILITY 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 tag a team member or open a new issue that references this one.

    If you wish to keep having a conversation with other community members under this issue feel free to do so.

  16. added
    pending-release
    on Jan 31, 2025
  17. github-actions commented on Feb 11, 2025

    @github-actions
    Contributor

    This is now released under 3.6.0 version!

  18. removed
    pending-release
    on Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature-requesthelp wantedlogger

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions