How to get the list all existing loggers using pyt

2020-02-21 02:05发布

Is there a way in python to get the list all defined loggers?
I mean, does something that can be used like logging.getAllLoggers() and which would return a list of Logger objects exist?

I searched the python.logging documentation but couldn't find such a method.

Thank you in advance.

1条回答
该账号已被封号
2楼-- · 2020-02-21 02:29

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]

Calling getLogger(name) ensures that any placeholder loggers held by loggerDict are fully initialized when they are added to the list.

查看更多
登录 后发表回答