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

Skip to content

Commit 9bdb580

Browse files
authored
bpo-43353: Document that logging.getLevelName() accepts string representation of logging level. (GH-24693) (#24825)
(cherry picked from commit bbba282) Automerge-Triggered-By: GH:vsajip
1 parent dc8558e commit 9bdb580

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

Doc/library/logging.rst

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,18 +1100,27 @@ functions.
11001100

11011101
.. function:: getLevelName(level)
11021102

1103-
Returns the textual representation of logging level *level*. If the level is one
1104-
of the predefined levels :const:`CRITICAL`, :const:`ERROR`, :const:`WARNING`,
1105-
:const:`INFO` or :const:`DEBUG` then you get the corresponding string. If you
1106-
have associated levels with names using :func:`addLevelName` then the name you
1107-
have associated with *level* is returned. If a numeric value corresponding to one
1108-
of the defined levels is passed in, the corresponding string representation is
1109-
returned. Otherwise, the string 'Level %s' % level is returned.
1103+
Returns the textual or numeric representation of logging level *level*.
1104+
1105+
If *level* is one of the predefined levels :const:`CRITICAL`, :const:`ERROR`,
1106+
:const:`WARNING`, :const:`INFO` or :const:`DEBUG` then you get the
1107+
corresponding string. If you have associated levels with names using
1108+
:func:`addLevelName` then the name you have associated with *level* is
1109+
returned. If a numeric value corresponding to one of the defined levels is
1110+
passed in, the corresponding string representation is returned.
1111+
1112+
The *level* parameter also accepts a string representation of the level such
1113+
as 'INFO'. In such cases, this functions returns the corresponding numeric
1114+
value of the level.
1115+
1116+
If no matching numeric or string value is passed in, the string
1117+
'Level %s' % level is returned.
11101118

11111119
.. note:: Levels are internally integers (as they need to be compared in the
11121120
logging logic). This function is used to convert between an integer level
11131121
and the level name displayed in the formatted log output by means of the
1114-
``%(levelname)s`` format specifier (see :ref:`logrecord-attributes`).
1122+
``%(levelname)s`` format specifier (see :ref:`logrecord-attributes`), and
1123+
vice versa.
11151124

11161125
.. versionchanged:: 3.4
11171126
In Python versions earlier than 3.4, this function could also be passed a

Lib/logging/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118

119119
def getLevelName(level):
120120
"""
121-
Return the textual representation of logging level 'level'.
121+
Return the textual or numeric representation of logging level 'level'.
122122
123123
If the level is one of the predefined levels (CRITICAL, ERROR, WARNING,
124124
INFO, DEBUG) then you get the corresponding string. If you have
@@ -128,7 +128,11 @@ def getLevelName(level):
128128
If a numeric value corresponding to one of the defined levels is passed
129129
in, the corresponding string representation is returned.
130130
131-
Otherwise, the string "Level %s" % level is returned.
131+
If a string representation of the level is passed in, the corresponding
132+
numeric value is returned.
133+
134+
If no matching numeric or string value is passed in, the string
135+
'Level %s' % level is returned.
132136
"""
133137
# See Issues #22386, #27937 and #29220 for why it's this way
134138
result = _levelToName.get(level)

0 commit comments

Comments
 (0)