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

How to Access Contextualized Values Within do_something() Function? #1179

Open
tstrg opened this issue Jul 19, 2024 · 2 comments
Open

How to Access Contextualized Values Within do_something() Function? #1179

tstrg opened this issue Jul 19, 2024 · 2 comments
Labels
question Further information is requested

Comments

@tstrg
Copy link

tstrg commented Jul 19, 2024

Hello,

I'm trying to understand how to read contextualized values within the do_something() function when using the logger.contextualize() method. Here is an example of my code:

from loguru import logger

task_id = "12345"

def do_something():
    # How can I access the contextualized 'task' value here?
    logger.info("Doing something")

with logger.contextualize(task=task_id):
    do_something()
    logger.info("End of task")
@Delgan
Copy link
Owner

Delgan commented Jul 20, 2024

The logger should not be used as a way to store / access values. It mainly meant to provide context to the logged message.

There is no way to access the contextualized value outside of the sink itself (and function such as filter).

I would advise to use contextvars directly instead.

@Delgan Delgan added the question Further information is requested label Jul 20, 2024
@tstrg
Copy link
Author

tstrg commented Jul 21, 2024

Thank you for your feedback.

I understand that the logger is primarily intended to provide context to logged messages rather than storing or accessing values. I'll explore using contextvars directly for managing contextual values.

Thanks again for your guidance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants