Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29f1b0b

Browse files
vsajipmerwok
andauthored
gh-89258: Add a getChildren() method to logging.Logger. (GH-96444)
Co-authored-by: Éric <[email protected]>
1 parent f7e7bf1 commit 29f1b0b

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

Doc/library/logging.rst

+12
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@ is the module's name in the Python package namespace.
170170
.. versionadded:: 3.2
171171

172172

173+
.. method:: Logger.getChildren()
174+
175+
Returns a set of loggers which are immediate children of this logger. So for
176+
example ``logging.getLogger().getChildren()`` might return a set containing
177+
loggers named ``foo`` and ``bar``, but a logger named ``foo.bar`` wouldn't be
178+
included in the set. Likewise, ``logging.getLogger('foo').getChildren()`` might
179+
return a set including a logger named ``foo.bar``, but it wouldn't include one
180+
named ``foo.bar.baz``.
181+
182+
.. versionadded:: 3.12
183+
184+
173185
.. method:: Logger.debug(msg, *args, **kwargs)
174186

175187
Logs a message with level :const:`DEBUG` on this logger. The *msg* is the

Lib/logging/__init__.py

+19
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,25 @@ def getChild(self, suffix):
18281828
suffix = '.'.join((self.name, suffix))
18291829
return self.manager.getLogger(suffix)
18301830

1831+
def getChildren(self):
1832+
1833+
def _hierlevel(logger):
1834+
if logger is logger.manager.root:
1835+
return 0
1836+
return 1 + logger.name.count('.')
1837+
1838+
d = self.manager.loggerDict
1839+
_acquireLock()
1840+
try:
1841+
# exclude PlaceHolders - the last check is to ensure that lower-level
1842+
# descendants aren't returned - if there are placeholders, a logger's
1843+
# parent field might point to a grandparent or ancestor thereof.
1844+
return set(item for item in d.values()
1845+
if isinstance(item, Logger) and item.parent is self and
1846+
_hierlevel(item) == 1 + _hierlevel(item.parent))
1847+
finally:
1848+
_releaseLock()
1849+
18311850
def __repr__(self):
18321851
level = getLevelName(self.getEffectiveLevel())
18331852
return '<%s %s (%s)>' % (self.__class__.__name__, self.name, level)

Lib/test/test_logging.py

+14
Original file line numberDiff line numberDiff line change
@@ -3717,6 +3717,20 @@ def test_child_loggers(self):
37173717
self.assertIs(c2, logging.getLogger('abc.def.ghi'))
37183718
self.assertIs(c2, c3)
37193719

3720+
def test_get_children(self):
3721+
r = logging.getLogger()
3722+
l1 = logging.getLogger('foo')
3723+
l2 = logging.getLogger('foo.bar')
3724+
l3 = logging.getLogger('foo.bar.baz.bozz')
3725+
l4 = logging.getLogger('bar')
3726+
kids = r.getChildren()
3727+
expected = {l1, l4}
3728+
self.assertEqual(expected, kids & expected) # might be other kids for root
3729+
self.assertNotIn(l2, expected)
3730+
kids = l1.getChildren()
3731+
self.assertEqual({l2}, kids)
3732+
kids = l2.getChildren()
3733+
self.assertEqual(set(), kids)
37203734

37213735
class DerivedLogRecord(logging.LogRecord):
37223736
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added a :meth:`~logging.Logger.getChildren` method to
2+
:class:`logging.Logger`, to get the immediate child loggers of a logger.

0 commit comments

Comments
 (0)