-
-
Notifications
You must be signed in to change notification settings - Fork 233
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
Any plan to add taskName
logging attribute as CallsiteParameter
#693
Comments
Not immediately, but open to PRs. Shouldn't be too difficult, but my focus is elsewhere right now. |
Thanks. I’ll try to come out with a proposal. Cheers |
Hi, I have come out with this PR 698 which add a new member def get_taskname() -> Optional[str]:
"""
Get the current asynchronous task if applicable.
Returns:
Optional[str]: asynchronous task name.
"""
task_name = None
with suppress(Exception):
task = asyncio.current_task()
if task:
task_name = task.get_name()
return task_name So I also added into the PR a few test cases which are all passed. However, what I noticed was that it doesn't work as expected when running async. I wrote the attach small Python script If executed synchronously, then no issues. Callsite parameters are equal on both sides: 🟢 python a.py --sync
~~~ Synchrnous Mode ~~~
2025-02-01T21:36:31 [info ] structlog -> Start of async task (sleep: 0.40) filename=a.py func_name=routine lineno=95 module=a process=407380 process_name=n/a task_name=None thread_name=MainThread
2025-02-01T21:36:31 [info ] structlog -> End of async task filename=a.py func_name=routine lineno=100 module=a process=407380 process_name=n/a task_name=None thread_name=MainThread
2025-02-01T21:36:31 INFO 407380 MainProcess a a.py:routine:93 MainThread None logging -> Start of async task (sleep: 0.78)
2025-02-01T21:36:32 INFO 407380 MainProcess a a.py:routine:98 MainThread None logging -> End of async task The differences can be seen if executed asynchrnously: 🟢 python tmp/a.py --async
~~~ Asynchrnous Mode ~~~
2025-02-01T21:36:55 [info ] structlog -> Start of async task (sleep: 0.55) filename=a.py func_name=aroutine lineno=66 module=a process=407651 process_name=n/a task_name=None thread_name=asyncio_0
2025-02-01T21:36:55 [info ] structlog -> End of async task filename=a.py func_name=aroutine lineno=74 module=a process=407651 process_name=n/a task_name=None thread_name=asyncio_0
2025-02-01T21:36:55 INFO 407651 MainProcess a a.py:aroutine:64 MainThread AsyncRoutine logging -> Start of async task (sleep: 0.63)
2025-02-01T21:36:56 INFO 407651 MainProcess a a.py:aroutine:72 MainThread AsyncRoutine logging -> End of async task
As far as I could see, the reason I think is the way So, in short, assuming my analysis is right, there way to add "task name" as another callsite parameter is not feasible. Indeed, neither the "process name" nor "thread name" seems to work. Do you have any other suggestion/idea how it can be added (if any)? Thanks! PS: Sorry for the long post. Cheers, |
Hello,
As of Python 3.12, the standard library
logging
added a new attributetaskName
(see here). Is there any plan to add this attribute as well to structlog at some point?Thanks
Jose M. Prieto
The text was updated successfully, but these errors were encountered: