To have a list of logging levels in Python 2 I have to call this:
logging._levelNames.keys()
While Python 3 logging module has this:
list(logging._nameToLevel)
I am just adding six
to my project to provide compatibility between Python 2 and Python 3. What is the cleanest way to figure out this compatibility issue?
You shouldn’t deliberately access a attribute that starts with a _
, it usually means that it’s a private attribute and that shouldn’t be used elsewhere.
And since the logging levels is just a short list, you can just copy and paste and create your own list instead:
levels = ['CRITICAL', 'FATAL', 'ERROR', 'WARN', 'WARNING', 'INFO', 'DEBUG', 'NOTSET']
Btw, if you noticed, the logging._levelNames.keys()
and list(logging._nameToLevel)
provides different results, the former also contains some integers [0, 10, 20, 30, 40, 50]
while the latter does not.