如何使用python.logging模块列出所有现有记录器 [英] How to list all existing loggers using python.logging module

查看:150
本文介绍了如何使用python.logging模块列出所有现有记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆