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 'asyncio' taskName to logging LogRecord attributes. (GH-93193) #93193

Merged
merged 10 commits into from
May 26, 2022

Conversation

jackh-ncl
Copy link
Contributor

@jackh-ncl jackh-ncl commented May 25, 2022

For: #91513

Continuing on from carlbordum@6e7dc2c 😃

Can test using:

import asyncio
import logging

FORMAT = '%(processName)s - %(process)s - %(threadName)s - %(thread)d - %(taskName)s - %(pathname)s - %(asctime)s - %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


async def log_some_stuff(name):
    logger.info(name)
    await asyncio.sleep(1)


async def spawn_tasks():
    await asyncio.gather(
        log_some_stuff('hello'),
        log_some_stuff('cheese')
    )


if __name__ == '__main__':
    asyncio.run(spawn_tasks())
    logger.info('Finished')
$ uname -a
Darwin ... 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:47:26 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T8101 arm64

$ ./python.exe -m test -j8
...
== Tests result: SUCCESS ==

407 tests OK.

29 tests skipped:
    test_curses test_dbm_gnu test_devpoll test_epoll test_gdb
    test_idle test_ioctl test_launcher test_msilib
    test_multiprocessing_fork test_ossaudiodev test_smtpnet
    test_socketserver test_spwd test_ssl test_startfile test_tcl
    test_tix test_tk test_ttk_guionly test_ttk_textonly test_turtle
    test_urllib2net test_urllibnet test_winconsoleio test_winreg
    test_winsound test_xmlrpc_net test_zipfile64

Total duration: 3 min 35 sec
Tests result: SUCCESS

@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

logging.logAsyncioTasks = False
runner.run(log_record(self.assertIsNone))
finally:
asyncio.set_event_loop_policy(None)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tearing down, else we end up with:

== Tests result: SUCCESS ==

406 tests OK.

1 test altered the execution environment:
    test_logging

29 tests skipped:
    test_curses test_dbm_gnu test_devpoll test_epoll test_gdb
    test_idle test_ioctl test_launcher test_msilib
    test_multiprocessing_fork test_ossaudiodev test_smtpnet
    test_socketserver test_spwd test_ssl test_startfile test_tcl
    test_tix test_tk test_ttk_guionly test_ttk_textonly test_turtle
    test_urllib2net test_urllibnet test_winconsoleio test_winreg
    test_winsound test_xmlrpc_net test_zipfile64

@jackh-ncl jackh-ncl changed the title Add 'asyncio' taskName to logging attributes 91513 - Add 'asyncio' taskName to logging attributes May 25, 2022
@jackh-ncl jackh-ncl changed the title 91513 - Add 'asyncio' taskName to logging attributes #91513 - Add 'asyncio' taskName to logging attributes May 25, 2022
@jackh-ncl jackh-ncl changed the title #91513 - Add 'asyncio' taskName to logging attributes gh-91513: Add 'asyncio' taskName to logging attributes May 25, 2022
@jackh-ncl jackh-ncl marked this pull request as ready for review May 25, 2022 06:33
@jackh-ncl jackh-ncl requested a review from vsajip as a code owner May 25, 2022 06:33
Doc/library/logging.rst Outdated Show resolved Hide resolved
Lib/logging/__init__.py Show resolved Hide resolved
Lib/test/test_logging.py Outdated Show resolved Hide resolved
Lib/test/test_logging.py Show resolved Hide resolved
Lib/test/test_logging.py Show resolved Hide resolved
Lib/test/test_logging.py Show resolved Hide resolved
@bedevere-bot
Copy link

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.

Minor refactor in LogRecord.__init__ as per suggestions.

Fixed typo in docs.
@jackh-ncl jackh-ncl requested a review from vsajip May 25, 2022 08:36
Copy link
Member

@vsajip vsajip left a comment

Choose a reason for hiding this comment

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

Just one suggested minor change, and one question.

with open('test.log', encoding='utf-8') as f:
data = f.read().strip()
os.remove('test.log')
self.assertEqual('Task-1 - hello world', data)
Copy link
Member

Choose a reason for hiding this comment

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

How can we be sure the task name will always be Task-1? What if more asyncio tasks are added in other tests which run before this one? Will the numbering still start at 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have changed it to an assertRegex, similar to:

match1 = re.match(r"^<Task pending name='Task-(\d+)'", repr(t1))
self.assertIsNotNone(match1)

…istent.

Removed percent styling from News entry.
@jackh-ncl jackh-ncl requested a review from vsajip May 25, 2022 15:41
Copy link
Member

@vsajip vsajip left a comment

Choose a reason for hiding this comment

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

The Azure pipeline check is failing - this is usually due to whitespace normalization issues. Can you please run make patchcheck and fixup anything that's found?

@jackh-ncl
Copy link
Contributor Author

Pipelines looking green after pulling most recent main branch.

@jackh-ncl jackh-ncl requested a review from vsajip May 25, 2022 17:27
@vsajip vsajip changed the title gh-91513: Add 'asyncio' taskName to logging attributes gh-91513: Add 'asyncio' taskName to logging LogRecord attributes. (GH-93193) May 26, 2022
@vsajip vsajip merged commit cc37706 into python:main May 26, 2022
with open('test.log', encoding='utf-8') as f:
data = f.read().strip()
os.remove('test.log')
self.assertRegex(data, r'Task-\d+ - hello world')
Copy link
Contributor

Choose a reason for hiding this comment

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

handler = None
try:
    ...
finally:
    asyncio.set_event_loop_policy(None)
    if handler is not None:
        handler.close()
    if os.path.exists('test.log'):
        ....

to prevent sub-exceptions in the finally block if there is an exception/assertion error in the try block ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would have put the self.assertRegex(data, r'Task-\d+ - hello world') at the end of the try block too.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's a fair point. This test probably needs tidying up a little.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was following the pattern of the encoding tests above this one.
Happy to raise a new PR and fix it up along with the others.

Copy link
Member

Choose a reason for hiding this comment

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

That's OK, I've just done it. I'm working on logging tests for something else.

Copy link
Member

Choose a reason for hiding this comment

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

It's #93265.

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

Successfully merging this pull request may close these issues.

4 participants