Skip to content

clear logger keys between lambda invocations #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LironEr opened this issue Jun 4, 2021 · 4 comments
Closed

clear logger keys between lambda invocations #20

LironEr opened this issue Jun 4, 2021 · 4 comments
Assignees
Labels
Proposed Community submited Python

Comments

@LironEr
Copy link

LironEr commented Jun 4, 2021

Runtime: Python

Is your feature request related to a problem? Please describe
Currently, logger keys are kept between Lambda invocations unless I manually call remove_keys or structure_logs.

def handler(event, context):
  logger.info('log info') # will include key1 & key2 after first invocation

  logger.append_keys(key1=body['something'])

  logger.info('log log log')

  # some code...

  logger.append_keys(key2=some_value)

  logger.info('log log log')

Currently, if I want to remove all keys added during the lambda I need to run at the start of the lambda:

logger.remove_keys(['key1', 'key2'])
# or
logger.structure_logs()

The problem with the first option is that I don't want to specify all keys manually, I might even forget some keys.

The problem with the second option is that it will be removed in the next major version and it would also delete lambda injected context (if I use @logger.inject_lambda_context)

Describe the solution you'd like
An easy way to remove all keys at the start of a lambda execution

  • If using @logger.inject_lambda_context remove all keys by default (can be changed with parameter) at the start of each lambda
  • create a new method: logger.restart_keys(exclude=[])
    logger.restart_keys() # will remove all keys
    logger.restart_keys(['key2']) # will remove all keys except key2
    

If you provide guidance, is this something you'd like to contribute?
Yes

Additional context
Related issue: aws-powertools/powertools-lambda-python#407 (comment)

@LironEr LironEr added Pending/Triage Pending triage Proposed Community submited labels Jun 4, 2021
@heitorlessa
Copy link
Contributor

Thanks a lot for opening that up @LironEr - just added it to the backlog :)

Following our tenets, I'll implement a toggle you can use in the @inject_lambda_context to clear the state but not as a default, otherwise it'll break customers' current reliance on that today -- We can however make this a default in v2.

@heitorlessa heitorlessa removed the Pending/Triage Pending triage label Jun 8, 2021
@heitorlessa heitorlessa self-assigned this Jun 8, 2021
@heitorlessa
Copy link
Contributor

hey @LironEr, could you double check the UX before we release it today?

aws-powertools/powertools-lambda-python#467

@LironEr
Copy link
Author

LironEr commented Jun 8, 2021

Awesome, thank you!

@heitorlessa
Copy link
Contributor

hey @LironEr this is now available in PyPi as part of 1.17.0: https://github.com/awslabs/aws-lambda-powertools-python/releases/tag/v1.17.0

Added a new section in the docs including caveats when using Layers (one of the reasons it's hard to make it the default behaviour): https://awslabs.github.io/aws-lambda-powertools-python/latest/core/logger/#clearing-all-state

Thanks a lot for helping us improve everyone's experience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposed Community submited Python
Projects
None yet
Development

No branches or pull requests

2 participants