Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make more improvements based on feedback.
  • Loading branch information
vsajip committed May 31, 2022
commit 42d43b93a9a5e9717153b48424c318ca9c5f78c7
6 changes: 6 additions & 0 deletions Doc/library/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,12 @@ functions.

.. versionadded:: 3.12

.. function:: getHandlerNames()

Returns an immutable set of all known handler names.

.. versionadded:: 3.12

.. function:: makeLogRecord(attrdict)

Creates and returns a new :class:`LogRecord` instance whose attributes are
Expand Down
10 changes: 9 additions & 1 deletion Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown',
'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory',
'lastResort', 'raiseExceptions', 'getLevelNamesMapping',
'getHandlerByName']
'getHandlerByName', 'getHandlerNames']

import threading

Expand Down Expand Up @@ -895,6 +895,14 @@ def getHandlerByName(name):
return _handlers.get(name)


def getHandlerNames():
"""
Return all known handler names as an immutable set.
"""
result = set(_handlers.keys())
return frozenset(result)


class Handler(Filterer):
"""
Handler instances dispatch logging events to specific destinations.
Expand Down
8 changes: 6 additions & 2 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3430,6 +3430,8 @@ def test_bad_format_with_dollar_style(self):
self.assertIsInstance(handler.target, logging.Handler)
self.assertIsInstance(handler.formatter._style,
logging.StringTemplateStyle)
self.assertEqual(sorted(logging.getHandlerNames()),
['bufferGlobal', 'fileGlobal'])

def test_custom_formatter_class_with_validate(self):
self.apply_config(self.custom_formatter_class_validate)
Expand Down Expand Up @@ -3569,18 +3571,20 @@ def test_config_queue_handler(self):
if lspec:
cd['handlers']['ah']['listener'] = lspec
qh = None
delay = 0.01
try:
self.apply_config(cd)
qh = logging.getHandlerByName('ah')
self.assertEqual(sorted(logging.getHandlerNames()), ['ah', 'h1'])
self.assertIsNotNone(qh.listener)
qh.listener.start()
# Need to let the listener thread get started
time.sleep(0.001)
time.sleep(delay)
logging.debug('foo')
logging.info('bar')
logging.warning('baz')
# Need to let the listener thread finish its work
time.sleep(0.001)
time.sleep(delay)
with open(fn, encoding='utf-8') as f:
data = f.read().splitlines()
self.assertEqual(data, ['foo', 'bar', 'baz'])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Add the ability for :func:`logging.config.dictConfig` to usefully configure
:class:`~logging.handlers.QueueHandler` and :class:`~logging.handlers.QueueListener`
as a pair, and add a :func:`logging.getHandlerByName` API to allow access to handlers by name.
as a pair, and add :func:`logging.getHandlerByName` and :func:`logging.getHandlerNames`
APIs to allow access to handlers by name.