Search code examples
pythonlogging

get list of named loglevels


In my application, I'm using python.logging for logging.

Now I want to control the loglevel interactively, so i created a combobox hat lets the user select "ERROR", "WARN", "INFO",...

What I don't really like is that currently the values in the combobox are hardcoded. Instead, I would like to have a list of all "named" loglevels (e.g. both the system defaults, but also those added via logging.addLevelName; but not the fake generated loglevels like "Level 42")

The best I have come up with so far is to use the logging._levelNames dictionary.

But then this seems to be a private member, and I somehow have a bad feeling accessing it directly.

So my question is: what's the proper way to list all currently defined "named" loglevels in Python.


Solution

  • As you are only reading values, logging._levelNames looks an appropriate solution to me. Keep going with logging.addLevelName for setting new values though.