diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst index 70a703dde18a03..313cff4ff9658f 100644 --- a/Doc/library/logging.rst +++ b/Doc/library/logging.rst @@ -1111,6 +1111,14 @@ functions. .. 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*. diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 555f598de7a925..723d9ba5118c2a 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -37,7 +37,7 @@ 'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass', 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown', 'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory', - 'lastResort', 'raiseExceptions'] + 'lastResort', 'raiseExceptions', 'getLevelNamesMapping'] import threading @@ -116,6 +116,9 @@ 'NOTSET': NOTSET, } +def getLevelNamesMapping(): + return _nameToLevel.copy() + def getLevelName(level): """ Return the textual or numeric representation of logging level 'level'. diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index ee00a32026f65e..6d111908e7c395 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -4390,6 +4390,14 @@ def rec(): 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): diff --git a/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst new file mode 100644 index 00000000000000..d864e1b4e51e3d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst @@ -0,0 +1 @@ +Added a function that returns a copy of a dict of logging levels: :func:`logging.getLevelNamesMapping` pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy