Description
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)