Skip to content
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

Feature request: Implement a context manager to bind variables to a logger #2062

Open
1 of 2 tasks
wesleyhamburger-okta opened this issue Mar 28, 2023 · 3 comments
Open
1 of 2 tasks
Labels
feature-request feature request need-customer-feedback Requires more customers feedback before making or revisiting a decision need-more-information Pending information to continue

Comments

@wesleyhamburger-okta
Copy link

wesleyhamburger-okta commented Mar 28, 2023

Use case

I wan't to add variables to the logger for the duration of a request and get clean up for free without needing to explicitly call delete.

This is similar to https://www.structlog.org/en/stable/api.html#structlog.contextvars.bound_contextvars

Solution/User Experience

@contextmanager
def bind(self, **args):
    logger_instance.append_keys(**args)
    yield logger_instance
    logger_instance.remove_keys(args.keys())
with logger.bind(user_id=123):
    perform_operation():

Alternative solutions

No response

Acknowledgment

@wesleyhamburger-okta wesleyhamburger-okta added feature-request feature request triage Pending triage from maintainers labels Mar 28, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Mar 28, 2023

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our AWS Lambda Powertools Discord: Invite link

@heitorlessa
Copy link
Contributor

heitorlessa commented Mar 31, 2023

hey @wesleyhamburger-okta thanks for raising this feature request. Please allow me to ask some questions to make sure I understand it correctly:

  1. Are you also intending to use with threads? see RFC: Support thread-local keys for logger #991
  2. Are you simply looking for a shortcut to cut boilerplate? OR, a completely isolated logger instance within that operation?
    • 2A. If the latter, would you want existing keys to be carried over when you use bind()? e.g., lambda request id, etc.,

Reason I ask that is structlog.bind() returns an entirely new logger instance.

stateDiagram
    [*] --> Logger
    Logger --> new_logger
    new_logger --> append(user_id)
    append(user_id) --> perform_operation()
    perform_operation() --> remove(user_id)
    new_logger --> [*]
Loading

We, on the other hand, would append user_id to the registered Formatter state, and that isn't thread-safe (yet!).

stateDiagram
    [*] --> Logger
    Logger --> append(user_id)
    append(user_id) --> perform_operation()
    perform_operation() --> remove(user_id)
    remove(user_id) --> [*]

    [*] --> LoggerElsewhere
    LoggerElsewhere --> bi_directional_state
    LoggerElsewhere --> receives_user_id_too
	receives_user_id_too --> user_id_removed
    user_id_removed --> [*]
Loading

The former would need a better name to not confuse existing structlog cystiners, since they would be led to believe our with logger.bind would yield a completely isolated state.

Looking forward to hearing from you.

PS: If it's primarily boilerplate, we'd welcome a contribution and can guide/co-develop you all the way through to expedite this for the next release :)

Thanks a lot!!


proof of concept for a .bind that yields back a new isolated Logger instance while retaining the service etc.

Example for question 2

with logger.bind(user_id="123") as new_logger:
    # new_logger.info() would have `user_id` key... but if I do logger.info()... `user_id` would not be present
    # similarly, perform_operation() OR any other function with a `logger` instance wouldn't have `user_id`
    perform_operation()

@heitorlessa heitorlessa added need-more-information Pending information to continue need-customer-feedback Requires more customers feedback before making or revisiting a decision and removed triage Pending triage from maintainers labels Mar 31, 2023
@sthulb sthulb moved this from Triage to On hold in Powertools for AWS Lambda (Python) Jun 19, 2023
@trallnag
Copy link

trallnag commented Jul 2, 2024

Related #3719

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request feature request need-customer-feedback Requires more customers feedback before making or revisiting a decision need-more-information Pending information to continue
Projects
Status: On hold
Development

No branches or pull requests

3 participants