-
Notifications
You must be signed in to change notification settings - Fork 403
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
Allow logging additional keys as a context manager #3719
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Thanks @CaptainDriftwood for submitting this, we will discuss it during our next gathering. |
Simon and I discussed this a minute ago... got a question @CaptainDriftwood - is your intent to have a given set of keys temporarily added for N log statements? OR add N keys for a given log statement? This feature would make sense for the former*, but the latter is already possible by passing arbitrary keyword arguments to any log statement *caveat being that this is not thread-safe ( from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger()
def lambda_handler(event: dict, context: LambdaContext) -> str:
# these keys will only be available in this log statement, subsequent `logger.info` won't have them
logger.info("Collecting payment", request_id="1123", some_key="some_value")
return "hello world" |
@heitorlessa It would be temporarily added for N log statements. I'm not sure how ya'll would get around the thread safety aspect. That is a good point. |
gotcha! In this case, until we refactor Logger to use Updating to something like this from your earlier example: with logger.with_keys(some_key="some_value") as _logger: # new logger with some_key="some_value" appended
# 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 Would this work for you? or would you prefer to wait for a refactor to use Edit: add question. |
@heitorlessa Can this be done without the |
@heitorlessa Apologies for the delayed response. I would prefer to wait for a refactor to use |
I'm also interested in this feature. For example I know it from loguru in Python or zerolog in Go.
|
Use case
The built in logger supports adding additional context using the
append_keys
and removing that additional context using theremove_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'scontextmanager
decorator as documented here:https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager
Example:
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
The text was updated successfully, but these errors were encountered: