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: Provide method to get list of logging level names
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: andrei.avk, andylowry, eric.smith, miss-islington, rhettinger, shreyanavigyan, vinay.sajip
Priority: normal Keywords: patch

Created on 2021-04-15 17:46 by andylowry, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26459 merged andrei.avk, 2021-05-31 04:46
Messages (6)
msg391145 - (view) Author: Andy Lowry (andylowry) Date: 2021-04-15 17:46
It would be useful to have something like a `getLevelNames` method to return the current list of level names, ordered by priority.

This currently appears to be available only by accessing a private member, like `_levelToName` or `_nameToLevel`.

This functionality is useful, for example, in populating a `choices` list for an `argparse` option to allow a user to select a logging level when launching a program from a command line.
msg391147 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-04-15 18:13
I agree with you. It would be nice to have a function to list or dictionary of level names. Moreover I suggest that there should be an argument to the function like format_level which can take in "dict:level_name" for returning a dictionary with the level as keys and name as values, "dict:name_level" for returning a dictionary with the name as keys and level as values or "list" for returning just the list of level names ordered by priority.
msg393833 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-05-17 21:22
I can add getLevelNamesDict() which would return a copy of _nameToLevel, which is currently

_nameToLevel = {
    'CRITICAL': CRITICAL,
    'FATAL': FATAL,
    'ERROR': ERROR,
    'WARN': WARNING,
    'WARNING': WARNING,
    'INFO': INFO,
    'DEBUG': DEBUG,
    'NOTSET': NOTSET,
}

I think it may be best not to return a list of levels because it's not obvious that some of them are aliases, if the caller is responsible for making the list out of the dict, they will be able to decide how to deal with alias levels.

If that sounds good I can make a PR..
msg394764 - (view) Author: Andy Lowry (andylowry) Date: 2021-05-30 14:31
@andrei.avk Yes, that sounds just fine. Many thanks.
msg394765 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-05-30 14:46
> I can add getLevelNamesDict() which would return a copy of _nameToLevel

That seem like the best approach.
msg394989 - (view) Author: miss-islington (miss-islington) Date: 2021-06-03 08:13
New changeset 8b93f0e696d3fc60fd311c13d5238da73a35e3b3 by andrei kulakov in branch 'main':
bpo-43858: Add logging.getLevelNamesMapping() (GH-26459)
https://github.com/python/cpython/commit/8b93f0e696d3fc60fd311c13d5238da73a35e3b3
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 88024
2021-06-03 08:59:30vinay.sajipsetstatus: open -> closed
type: enhancement
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.11, - Python 3.8
2021-06-03 08:13:16miss-islingtonsetnosy: + miss-islington
messages: + msg394989
2021-05-31 04:46:42andrei.avksetkeywords: + patch
stage: patch review
pull_requests: + pull_request25054
2021-05-30 14:46:03rhettingersetassignee: vinay.sajip

messages: + msg394765
nosy: + rhettinger
2021-05-30 14:31:47andylowrysetmessages: + msg394764
2021-05-17 21:22:42andrei.avksetnosy: + andrei.avk
messages: + msg393833
2021-04-15 18:15:01eric.smithsetnosy: + vinay.sajip
2021-04-15 18:13:38eric.smithsetnosy: + eric.smith
2021-04-15 18:13:05shreyanavigyansetnosy: + shreyanavigyan
messages: + msg391147
2021-04-15 17:46:57andylowrycreate