gh-91513: Add 'asyncio' taskName to logging LogRecord attributes. (GH-93193)

This commit is contained in:
jackh-ncl 2022-05-26 09:30:51 +01:00 • committed by GitHub
parent 5185956527
commit cc377063ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 7 deletions

View file

@ -64,20 +64,25 @@ _startTime = time.time()
raiseExceptions = True
#
# If you don't want threading information in the log, set this to zero
# If you don't want threading information in the log, set this to False
#
logThreads = True
#
# If you don't want multiprocessing information in the log, set this to zero
# If you don't want multiprocessing information in the log, set this to False
#
logMultiprocessing = True
#
# If you don't want process information in the log, set this to zero
# If you don't want process information in the log, set this to False
#
logProcesses = True
#
# If you don't want asyncio task information in the log, set this to False
#
logAsyncioTasks = True
#---------------------------------------------------------------------------
# Level related stuff
#---------------------------------------------------------------------------
@ -361,6 +366,15 @@ class LogRecord(object):
else:
self.process = None
self.taskName = None
if logAsyncioTasks:
asyncio = sys.modules.get('asyncio')
if asyncio:
try:
self.taskName = asyncio.current_task().get_name()
except Exception:
pass
def __repr__(self):
return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
self.pathname, self.lineno, self.msg)
@ -566,6 +580,7 @@ class Formatter(object):
(typically at application startup time)
%(thread)d Thread ID (if available)
%(threadName)s Thread name (if available)
%(taskName)s Task name (if available)
%(process)d Process ID (if available)
%(message)s The result of record.getMessage(), computed just as
the record is emitted