如何使用python.logging模块列出所有现有记录器 [英] How to list all existing loggers using python.logging module
问题描述
Python中是否有一种方法可以获取所有已定义记录器的列表?
Is there a way in Python to get a list of all defined loggers?
我的意思是,是否存在诸如logging.getAllLoggers()
这样的东西,该东西会返回Logger
个对象的列表?
I mean, does something exist such as logging.getAllLoggers()
which would return a list of Logger
objects?
我搜索了python.logging文档,但找不到这种方法.
I searched the python.logging documentation but couldn't find such a method.
谢谢.
推荐答案
记录器由logging.Manager
实例按层次结构保存.您可以在根记录器上查询manager
以了解它所知道的记录器.
Loggers are held in a hierarchy by a logging.Manager
instance. You can interrogate the manager
on the root logger for the loggers it knows about.
import logging
loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
调用getLogger(name)
可以确保loggerDict
拥有的所有占位记录器在添加到列表时都被完全初始化.
Calling getLogger(name)
ensures that any placeholder loggers held by loggerDict
are fully initialized when they are added to the list.
这篇关于如何使用python.logging模块列出所有现有记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!