This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python 3.4 logging.getLevelName() no longer maps string to level.
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alex, cboylan, larry, python-dev, vinay.sajip
Priority: release blocker Keywords:

Created on 2014-09-11 01:09 by cboylan, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg226739 - (view) Author: Clark Boylan (cboylan) Date: 2014-09-11 01:09
Prior to http://hg.python.org/cpython/rev/5629bf4c6bba?revcount=60 logging.getLevelName(lvl) would map string lvl args like 'INFO' to the integer level value 20. After this change the string to int level mapping is removed and you can only map level to string. This removes the only public method for doing this mapping in the logging module.

Old Behavior:
>>> logging.getLevelName('INFO')
20
>>> logging.getLevelName(20)
'INFO'
>>> 

New Behavior:
>>> logging.getLevelName('INFO')
'Level INFO'
>>> logging.getLevelName(20)
'INFO'
>>> logging.getLevelName(logging.INFO)
'INFO'
>>> 

The old behavior is valuable because it allows you to sanity check log levels provided as strings before attempting to use them. It seems that without this public mapping you have to rely on Logger.setLevel(lvl) throwing a ValueError which it seems to do only in 2.7 and greater.
msg226740 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2014-09-11 01:12
I believe something like the following diff restores the previous behavior (untested!):

diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index a61c2b0..4a8f83e 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -129,7 +129,7 @@ def getLevelName(level):

     Otherwise, the string "Level %s" % level is returned.
     """
-    return _levelToName.get(level, ("Level %s" % level))
+    return _levelToName.get(level, _nameToLevel.get(level, ("Level %s" % level)))

 def addLevelName(level, levelName):
     """
msg226804 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-09-11 22:09
New changeset a4c5effb8698 by Vinay Sajip in branch '3.4':
Issue #22386: fixed regression.
http://hg.python.org/cpython/rev/a4c5effb8698

New changeset 070fed5b7b9d by Vinay Sajip in branch 'default':
Closes #22386: merged fix from 3.4.
http://hg.python.org/cpython/rev/070fed5b7b9d
History
Date User Action Args
2022-04-11 14:58:07adminsetgithub: 66580
2014-09-11 22:09:00python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg226804

resolution: fixed
stage: resolved
2014-09-11 18:51:36pitrousetpriority: normal -> release blocker
nosy: + larry

versions: + Python 3.5
2014-09-11 07:13:47ned.deilysetnosy: + vinay.sajip
2014-09-11 01:12:34alexsetnosy: + alex
messages: + msg226740
2014-09-11 01:09:58cboylancreate