Skip to content

Feature request: logger apppend_key context manager #5864

Closed
@jusdino

Description

@jusdino

Use case

There are a lot of cases where it could be useful to log extra context-specific keys such as identifiers in a way that is protected from contaminating across other execution contexts. Uses could include looping over a number of jobs, or even enhancing some branch-specific logic with an key that doesn't make sense when the branch didn't execute. In those cases, it would be really handy to be able to concisely and safely manage log keys in a context manager style.

Solution/User Experience

We could enhance the logger interface to include a standard python context manager so use could look something like:

from aws_lambda_powertools.logging import Logger


logger = Logger()

def event_handler(event, context):
    # ... stuff before processing jobs
    for some_job in a_bunch_of_jobs:
        with logger.append_keys(some_id=some_job['id']):
            do_processing(some_job)

    logger.info("Log doesn't contain some_id")
    # ... non-job-specific stuff


def do_processing(some_job):
    logger.info('Log does contain some_id')
    # ... job-specific stuff

I'm not sure modifying the actual append_keys method makes sense as an interface, but some method/attribute/whatever on the Logger based on your judgement would probably serve equally as well.

Alternative solutions

Acknowledgment

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions