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

gh-91513: add taskname to logging.LogRecord attributes #92646

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

carlbordum
Copy link
Contributor

@carlbordum carlbordum commented May 10, 2022

No description provided.

@carlbordum carlbordum requested a review from vsajip as a code owner May 10, 2022
@carlbordum carlbordum force-pushed the 91513-logrecord-taskname-attr branch from 4a9cae1 to 27b9704 Compare May 10, 2022
@carlbordum carlbordum changed the title gh-91578: add taskname to logging.LogRecord attributes gh-91513: add taskname to logging.LogRecord attributes May 10, 2022
@carlbordum carlbordum force-pushed the 91513-logrecord-taskname-attr branch from 27b9704 to 6e7dc2c Compare May 10, 2022
@@ -360,6 +366,9 @@ def __init__(self, name, level, pathname, lineno,
self.process = os.getpid()
else:
self.process = None
if logAsyncioTasks:
aio = sys.modules.get('asyncio')
self.taskname = aio.current_task().get_name()
Copy link
Contributor

@Akuli Akuli May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • If asyncio hasn't been imported, aio is None and this will fail with AttributeError.
  • If we aren't running any asyncio task right now, current_task() will raise RuntimeError.

In other words, this fails with or without the asyncio import:

#import asyncio
import logging
logging.error("blah blah")

I think we should also make sure that self.taskname is always set to something, and just set it to None if logAsyncTasks is set to False or no asyncio task is running. See how it sets self.processName = None above.

Copy link
Member

@vsajip vsajip left a comment

This PR needs tests (which would have exposed the point that @Akuli makes) and it would also make sense to use taskName in preference to taskname. I know it's not PEP-8 compliant and all, but logging predated PEP-8 and it makes more sense to keep things consistent at a local level.

It would make sense to do something like:

if not logAsyncioTasks:
    self.taskName = None
else:
    aio = sys.modules.get('asyncio')
    if aio is None:
        self.taskName = None
    else:
        self.taskName = aio.current_task().get_name()

or, more concisely,

self.taskName = None
if logAsyncioTasks:
    aio = sys.modules.get('asyncio')
    if aio:
        self.taskName = aio.current_task().get_name() 

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented May 13, 2022

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants