8000 Allow logging additional keys as a context manager · Issue #3719 · aws-powertools/powertools-lambda-python · GitHub
[go: up one dir, main page]

Skip to content
Allow logging additional keys as a context manager #3719
Closed
@CaptainDriftwood

Description

@CaptainDriftwood

Use case

The built in logger supports adding additional context using the append_keys and removing that additional context using the remove_keys methods, as documented here:
https://docs.powertools.aws.dev/lambda/python/latest/core/logger/#appending-additional-keys

Allow the built in logger to support logging keys as a context manager using the with keyword.

Solution/User Experience

Just adding an additional method that implements the contextlib module's contextmanager decorator as documented here:
https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager

Example:

    with logger.with_keys(some_key="some_value"):
        # do something
        logger.info("Something was done")
        # the log output would include the 'some_key' context
    # Do some other thing
    logger.info("Some other thing was done")
    # the log output would not include the 'some_key' context

This would reduce the need to remember to call remove_keys when specific context no longer wants to be logged.

Alternative solutions

No response

Acknowledgment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Shipped

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0