Python 3.11+loggingfunction
logging.getLevelNamesMapping()
levels
Returns a dict mapping level names to numbers, such as {'INFO': 20}. Handy for parsing a level from settings or the command line.
Syntax
mapping = logging.getLevelNamesMapping()
Each call returns a fresh copy: editing it changes nothing in logging, and new levels are added with addLevelName(). Keys are uppercase only, so call upper() on a level read from configuration before looking it up. The dict includes the aliases WARN and FATAL, so its values repeat. Available since 3.11.
Examples
import logging
levels = logging.getLevelNamesMapping()
print(levels['debug'.upper()], levels['WARN'], levels['FATAL']) # → 10 30 50
levels['TRACE'] = 5
print('TRACE' in logging.getLevelNamesMapping()) # → False — это копия
See also
Open in the full glossarydocs.python.orgReport an inaccuracy