Skip to content

Commit 6e7dc2c

Browse files
committed
pythongh-91513: add taskname to logging.LogRecord attributes
1 parent 30a4358 commit 6e7dc2c

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

Doc/howto/logging.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,8 @@ need:
11001100
| Current process name when using ``multiprocessing`` | Set ``logging.logMultiprocessing`` to ``False``. |
11011101
| to manage multiple processes. | |
11021102
+-----------------------------------------------------+---------------------------------------------------+
1103+
| :class:`asyncio.Task` information. | Set ``logging.logAsyncioTasks`` to ``False`` |
1104+
+-----------------------------------------------------+---------------------------------------------------+
11031105

11041106
Also note that the core logging module only includes the basic handlers. If
11051107
you don't import :mod:`logging.handlers` and :mod:`logging.config`, they won't

Doc/library/logging.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,10 +868,15 @@ the options available to you.
868868
+----------------+-------------------------+-----------------------------------------------+
869869
| threadName | ``%(threadName)s`` | Thread name (if available). |
870870
+----------------+-------------------------+-----------------------------------------------+
871+
| taskname | ``%(taskname)s`` | :class:`asyncio.Task` name (if available). |
872+
+----------------+-------------------------+-----------------------------------------------+
871873

872874
.. versionchanged:: 3.1
873875
*processName* was added.
874876

877+
.. versionchanged:: 3.12
878+
*taskname* was added.
879+
875880

876881
.. _logger-adapter:
877882

Lib/logging/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@
7878
#
7979
logProcesses = True
8080

81+
#
82+
# If you don't want asyncio.Task information in the log, set this to zero
83+
#
84+
logAsyncioTasks = True
85+
86+
8187
#---------------------------------------------------------------------------
8288
# Level related stuff
8389
#---------------------------------------------------------------------------
@@ -360,6 +366,9 @@ def __init__(self, name, level, pathname, lineno,
360366
self.process = os.getpid()
361367
else:
362368
self.process = None
369+
if logAsyncioTasks:
370+
aio = sys.modules.get('asyncio')
371+
self.taskname = aio.current_task().get_name()
363372

364373
def __repr__(self):
365374
return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``taskname`` to ``logging.LogRecord`` attributes.

0 commit comments

Comments
 (0)