Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Doc/howto/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,8 @@ need:
| Current process name when using ``multiprocessing`` | Set ``logging.logMultiprocessing`` to ``False``. |
| to manage multiple processes. | |
+-----------------------------------------------------+---------------------------------------------------+
| :class:`asyncio.Task` information. | Set ``logging.logAsyncioTasks`` to ``False`` |
+-----------------------------------------------------+---------------------------------------------------+

Also note that the core logging module only includes the basic handlers. If
you don't import :mod:`logging.handlers` and :mod:`logging.config`, they won't
Expand Down
5 changes: 5 additions & 0 deletions Doc/library/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -868,10 +868,15 @@ the options available to you.
+----------------+-------------------------+-----------------------------------------------+
| threadName | ``%(threadName)s`` | Thread name (if available). |
+----------------+-------------------------+-----------------------------------------------+
| taskname | ``%(taskname)s`` | :class:`asyncio.Task` name (if available). |
+----------------+-------------------------+-----------------------------------------------+

.. versionchanged:: 3.1
*processName* was added.

.. versionchanged:: 3.12
*taskname* was added.


.. _logger-adapter:

Expand Down
9 changes: 9 additions & 0 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
#
logProcesses = True

#
# If you don't want asyncio.Task information in the log, set this to zero
#
logAsyncioTasks = True


#---------------------------------------------------------------------------
# Level related stuff
#---------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Copy Markdown
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.


def __repr__(self):
return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``taskname`` to ``logging.LogRecord`` attributes.