mirror of
https://github.com/python/cpython.git
synced 2025-08-24 18:55:00 +00:00
[3.9] bpo-43353: Document that logging.getLevelName() accepts string representation of logging level. (GH-24693) (GH-24826)
(cherry picked from commit bbba28212c
)
Automerge-Triggered-By: GH:vsajip
This commit is contained in:
parent
ad83fde754
commit
4d7f11e057
2 changed files with 23 additions and 10 deletions
|
@ -1106,18 +1106,27 @@ functions.
|
||||||
|
|
||||||
.. function:: getLevelName(level)
|
.. function:: getLevelName(level)
|
||||||
|
|
||||||
Returns the textual representation of logging level *level*. If the level is one
|
Returns the textual or numeric representation of logging level *level*.
|
||||||
of the predefined levels :const:`CRITICAL`, :const:`ERROR`, :const:`WARNING`,
|
|
||||||
:const:`INFO` or :const:`DEBUG` then you get the corresponding string. If you
|
If *level* is one of the predefined levels :const:`CRITICAL`, :const:`ERROR`,
|
||||||
have associated levels with names using :func:`addLevelName` then the name you
|
:const:`WARNING`, :const:`INFO` or :const:`DEBUG` then you get the
|
||||||
have associated with *level* is returned. If a numeric value corresponding to one
|
corresponding string. If you have associated levels with names using
|
||||||
of the defined levels is passed in, the corresponding string representation is
|
:func:`addLevelName` then the name you have associated with *level* is
|
||||||
returned. Otherwise, the string 'Level %s' % level is returned.
|
returned. If a numeric value corresponding to one of the defined levels is
|
||||||
|
passed in, the corresponding string representation is returned.
|
||||||
|
|
||||||
|
The *level* parameter also accepts a string representation of the level such
|
||||||
|
as 'INFO'. In such cases, this functions returns the corresponding numeric
|
||||||
|
value of the level.
|
||||||
|
|
||||||
|
If no matching numeric or string value is passed in, the string
|
||||||
|
'Level %s' % level is returned.
|
||||||
|
|
||||||
.. note:: Levels are internally integers (as they need to be compared in the
|
.. note:: Levels are internally integers (as they need to be compared in the
|
||||||
logging logic). This function is used to convert between an integer level
|
logging logic). This function is used to convert between an integer level
|
||||||
and the level name displayed in the formatted log output by means of the
|
and the level name displayed in the formatted log output by means of the
|
||||||
``%(levelname)s`` format specifier (see :ref:`logrecord-attributes`).
|
``%(levelname)s`` format specifier (see :ref:`logrecord-attributes`), and
|
||||||
|
vice versa.
|
||||||
|
|
||||||
.. versionchanged:: 3.4
|
.. versionchanged:: 3.4
|
||||||
In Python versions earlier than 3.4, this function could also be passed a
|
In Python versions earlier than 3.4, this function could also be passed a
|
||||||
|
|
|
@ -118,7 +118,7 @@ _nameToLevel = {
|
||||||
|
|
||||||
def getLevelName(level):
|
def getLevelName(level):
|
||||||
"""
|
"""
|
||||||
Return the textual representation of logging level 'level'.
|
Return the textual or numeric representation of logging level 'level'.
|
||||||
|
|
||||||
If the level is one of the predefined levels (CRITICAL, ERROR, WARNING,
|
If the level is one of the predefined levels (CRITICAL, ERROR, WARNING,
|
||||||
INFO, DEBUG) then you get the corresponding string. If you have
|
INFO, DEBUG) then you get the corresponding string. If you have
|
||||||
|
@ -128,7 +128,11 @@ def getLevelName(level):
|
||||||
If a numeric value corresponding to one of the defined levels is passed
|
If a numeric value corresponding to one of the defined levels is passed
|
||||||
in, the corresponding string representation is returned.
|
in, the corresponding string representation is returned.
|
||||||
|
|
||||||
Otherwise, the string "Level %s" % level is returned.
|
If a string representation of the level is passed in, the corresponding
|
||||||
|
numeric value is returned.
|
||||||
|
|
||||||
|
If no matching numeric or string value is passed in, the string
|
||||||
|
'Level %s' % level is returned.
|
||||||
"""
|
"""
|
||||||
# See Issues #22386, #27937 and #29220 for why it's this way
|
# See Issues #22386, #27937 and #29220 for why it's this way
|
||||||
result = _levelToName.get(level)
|
result = _levelToName.get(level)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue