[New-bugs-announce] [issue45095] Easier loggers traversal tree with a logger.getChildren method

Stéphane Blondon report at bugs.python.org
Fri Sep 3 13:01:49 EDT 2021


New submission from Stéphane Blondon <stephane.blondon at gmail.com>:

Currently, logging.root.manager.loggerDict is usable to do a homemade traversal of the loggers tree. However, it's not a public interface. Adding a 'logger.getChildren()' method would help to implement the traversal. The method would return a set of loggers.


Usage example:
>>> import logging
>>> logging.basicConfig(level=logging.CRITICAL)
>>> root_logger = logging.getLogger()
<RootLogger root (CRITICAL)>
>>> root_logger.getChildren()
set()
>>> a_logger = logging.getLogger("a")
>>> root_logger.getChildren()
{<Logger a. (CRITICAL)>}
>>> logging.getLogger('a.b').setLevel(logging.DEBUG)
>>> _ = logging.getLogger('a.c')
>>> a_logger.getChildren()
{<Logger a.c (CRITICAL)>, <Logger a.b (DEBUG)>}


With such method, traverse the tree will be obvious to write with a recursive function.


Use cases:
 - to check all the loggers are setted up correctly. I wrote a small function to get all loggers, and log on every level to check the real behaviour.
 - to draw the loggers tree like logging_tree library (https://pypi.org/project/logging_tree/). I didn't ask to logging_tree's maintainer but I don't think he would use such function because the library works for huge range of python releases.


I plan to write a PR if someone thinks it's a good idea.

----------
components: Library (Lib)
messages: 401006
nosy: sblondon
priority: normal
severity: normal
status: open
title: Easier loggers traversal tree with a logger.getChildren method
type: enhancement
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45095>
_______________________________________


More information about the New-bugs-announce mailing list