Skip to content

Feature request: Add persistent keys option to Logger #6002

@jth08527

Description

@jth08527

Use case

Add custom keys to logs that should persist on every lambda execution and NOT be cleared when using clear_state.

This functionality already exists in the TypeScript version of powertools and can be useful for Python as well.

Solution/User Experience

Just like in the TypeScript version, add a new optional parameter to the Logger initializer. For example, the new option can be called persistent_keys.

This parameter could be a simple flat dict(or similar type?), and if not provided, default to an empty dict.

The value of this new parameter could either be added to self._default_log_keys(since what ever is in that already persists through a state clearing) or be maintained separately but treated just like self._default_log_keys.

Example init:

import os
from aws_lambda_powertools import Logger

logger = Logger(persistent_keys={"role": os.getenv("MY_ROLE", "MY_ROLE")

Example storing keys:

class Logger:
    ...
    def __init__(
        ...
        persistent_keys: dict[str, Any] = {},
        ...
    ) -> None:
        ...
        self._default_log_keys = {"service": self.service, "sampling_rate": self.sampling_rate}
        if persistent_keys:
            self._default_log_keys.update(persistent_keys)
        ...

Alternative solutions

Acknowledgment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Ideas

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions