Skip to content
Advertisement

How to convert python logging level name to integer code

As of Python 3.2, logging.Logger.setLevel accepts a string level such as ‘INFO’ instead of the corresponding integer constant. This is very handy except that you can’t compare the levels numerically that way and most other logging methods accept integers only. How do I convert a level string to a numerical level using the functions provided by the logging package? Specifically, I would like something that does this:

>>> logging.???('INFO') == logging.INFO
True

Advertisement

Answer

How about using something like

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> getattr(logging, 'INFO')
20
>>> getattr(logging, 'DEBUG')
10
>>> getattr(logging, 'ERROR')
40
>>> 
5 People found this is helpful
Advertisement