没有特定的功能可以做您想做的事,但您拥有所需的一切logging._levelNames
。
看一下addLevelName
定义,例如:
def addLevelName(level, levelName):
"""
Associate 'levelName' with 'level'.
This is used when converting levels to text during message formatting.
"""
_acquireLock()
try: #unlikely to cause an exception, but you never know...
_levelNames[level] = levelName
_levelNames[levelName] = level
finally:
_releaseLock()
所以 agetLevelNames()
可以这样实现:
import logging
def getLevelNames():
for k, v in sorted(logging._levelNames.iteritems()):
if isinstance(v, basestring):
yield v, k
import pprint
pprint.pprint(list(getLevelNames()))
示例输出:
[('NOTSET', 0),
('DEBUG', 10),
('INFO', 20),
('WARNING', 30),
('ERROR', 40),
('CRITICAL', 50)]