log: Introduce _iter_loggers

Ic58dafceefde1b109721a58631c223522bf4cc9c introduces this function
for Victor's rate_limit module. I also want it for my global_filter
and mutable logging config patches so it seems sensible to have it
centrally and reuse it.

Change-Id: I9571badcfc437fb7d565d8dc9632792862e20b3f
Co-Authored-By: Victor Stinner <vstinner@redhat.com>
This commit is contained in:
Alexis Lee 2016-03-16 16:34:20 +00:00
parent 15ef5855fe
commit fc490c03b7
2 changed files with 22 additions and 0 deletions

View File

@ -79,6 +79,22 @@ def _get_log_file_path(conf, binary=None):
return None
def _iter_loggers():
"""Iterate on existing loggers."""
# Sadly, Logger.manager and Manager.loggerDict are not documented,
# but there is no logging public function to iterate on all loggers.
# The root logger is not part of loggerDict.
yield logging.getLogger()
manager = logging.Logger.manager
for logger in manager.loggerDict.values():
if isinstance(logger, logging.PlaceHolder):
continue
yield logger
class BaseLoggerAdapter(logging.LoggerAdapter):
warn = logging.LoggerAdapter.warning

View File

@ -215,6 +215,12 @@ class LogHandlerTestCase(BaseTestCase):
binary=prefix),
expected)
def test_iter_loggers(self):
mylog = logging.getLogger("abc.cde")
loggers = list(log._iter_loggers())
self.assertIn(logging.getLogger(), loggers)
self.assertIn(mylog, loggers)
class SysLogHandlersTestCase(BaseTestCase):
"""Test the standard Syslog handler."""