Added a function that returns a copy of a dict of logging levels.
.. note:: If you are thinking of defining your own levels, please see the
section on :ref:`custom-levels`.
+.. function:: getLevelNamesMapping()
+
+ Returns a mapping from level names to their corresponding logging levels. For example, the
+ string "CRITICAL" maps to :const:`CRITICAL`. The returned mapping is copied from an internal
+ mapping on each call to this function.
+
+ .. versionadded:: 3.11
+
.. function:: getLevelName(level)
Returns the textual or numeric representation of logging level *level*.
'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass',
'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown',
'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory',
- 'lastResort', 'raiseExceptions']
+ 'lastResort', 'raiseExceptions', 'getLevelNamesMapping']
import threading
'NOTSET': NOTSET,
}
+def getLevelNamesMapping():
+ return _nameToLevel.copy()
+
def getLevelName(level):
"""
Return the textual or numeric representation of logging level 'level'.
self.assertNotIn("Cannot recover from stack overflow.", err)
self.assertEqual(rc, 1)
+ def test_get_level_names_mapping(self):
+ mapping = logging.getLevelNamesMapping()
+ self.assertEqual(logging._nameToLevel, mapping) # value is equivalent
+ self.assertIsNot(logging._nameToLevel, mapping) # but not the internal data
+ new_mapping = logging.getLevelNamesMapping() # another call -> another copy
+ self.assertIsNot(mapping, new_mapping) # verify not the same object as before
+ self.assertEqual(mapping, new_mapping) # but equivalent in value
+
class LogRecordTest(BaseTest):
def test_str_rep(self):
--- /dev/null
+Added a function that returns a copy of a dict of logging levels: :func:`logging.getLevelNamesMapping`