Repository navigation
Provide method to get list of logging level names #88024
Description
Activity
It would be useful to have something like a
getLevelNamesmethod to return the current list of level names, ordered by priority.This currently appears to be available only by accessing a private member, like
_levelToNameor_nameToLevel.This functionality is useful, for example, in populating a
choiceslist for anargparseoption to allow a user to select a logging level when launching a program from a command line.- added3.8 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Apr 15, 2021 shreyanavigyan commented
on Apr 15, 2021 shreyanavigyanmannequinMannequinMore actionsI 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.
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..
@andrei.avk Yes, that sounds just fine. Many thanks.
I can add getLevelNamesDict() which would return a copy of _nameToLevel
That seem like the best approach.
- added3.11only security fixesonly security fixesand removed3.8 (EOL)end of lifeend of life
on Jun 3, 2021 - added3.11only security fixesonly security fixestype-featureA feature request or enhancementA feature request or enhancementand removed3.8 (EOL)end of lifeend of life
on Jun 3, 2021 - addedtype-featureA feature request or enhancementA feature request or enhancement
on Jun 3, 2021
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: