diff --git a/Doc/howto/logging.rst b/Doc/howto/logging.rst index 4d76c27332ccd3..44d3c30b5af326 100644 --- a/Doc/howto/logging.rst +++ b/Doc/howto/logging.rst @@ -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 diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst index b82b90b47dd160..e21f8b2eba38f9 100644 --- a/Doc/library/logging.rst +++ b/Doc/library/logging.rst @@ -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: diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 79e4b7d09bfa43..56ba4852751048 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -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 #--------------------------------------------------------------------------- @@ -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() def __repr__(self): return ''%(self.name, self.levelno, diff --git a/Misc/NEWS.d/next/Library/2022-05-11-00-31-40.gh-issue-91513.6WqNoG.rst b/Misc/NEWS.d/next/Library/2022-05-11-00-31-40.gh-issue-91513.6WqNoG.rst new file mode 100644 index 00000000000000..4d6d6afeb57fc0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-11-00-31-40.gh-issue-91513.6WqNoG.rst @@ -0,0 +1 @@ +Add ``taskname`` to ``logging.LogRecord`` attributes.