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: logging - public function to get level from name
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: rhettinger, varunagrawal, vinay.sajip, zach.ware
Priority: low Keywords:

Created on 2018-01-15 18:01 by varunagrawal, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (7)
msg310005 - (view) Author: Varun Agrawal (varunagrawal) * Date: 2018-01-15 18:01
Currently, the logging module has a function `getLevelName` and directly accessing the `_nameToLevel` dict is poor practice bordering on non-pythonic.

It would be nice to have a simple method like `getNameLevel` in the module to retrieve the logger level given the name. This can then be used in things such as argument parsers so someone can pass in the logging level name and the logging level can be appropriately set.
msg310051 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-01-16 08:54
The use case of setting the level by name was already fulfilled in Python 3.2.   The logging.setLevel() function takes either an int or a string.
msg310093 - (view) Author: Varun Agrawal (varunagrawal) * Date: 2018-01-16 16:38
The request is not to set the level but to get the level given the level name aka textual representation. For example, passing in INFO should return 20 and passing in CRITICAL should return 50.
msg310096 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2018-01-16 17:26
You could use `getattr(logging, levelname)` for that purpose.
msg310104 - (view) Author: Varun Agrawal (varunagrawal) * Date: 2018-01-16 19:36
@zach.ware: True, but since we already have a getLevelName method, having a symmetric method to do the opposite made sense. Plus the method can perform error checking and other things to ensure much cleaner code.
msg310138 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-01-17 04:42
> The request is not to set the level but to get the level 
> given the level name aka textual representation. 
> For example, passing in INFO should return 20 and passing
> in CRITICAL should return 50.

Why do we care about this?
msg310142 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2018-01-17 06:41
I don't think a strong enough case has been made for adding this enhancement.
History
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76740
2018-01-17 06:41:09vinay.sajipsetstatus: open -> closed
resolution: rejected
messages: + msg310142

stage: resolved
2018-01-17 04:42:22rhettingersetmessages: + msg310138
2018-01-16 19:36:01varunagrawalsetmessages: + msg310104
2018-01-16 17:26:48zach.waresetnosy: + zach.ware
messages: + msg310096
2018-01-16 16:38:29varunagrawalsetmessages: + msg310093
2018-01-16 08:54:08rhettingersetpriority: normal -> low

nosy: + rhettinger, vinay.sajip
versions: - Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.8
messages: + msg310051

assignee: vinay.sajip
2018-01-15 18:01:51varunagrawalcreate