How to convert python logging level name to integer code

11,192

Solution 1

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
>>> 

Solution 2

There are a couple of attributes in the logging module that allow this functionality. They are prefixed with an underscore, implying privacy, so using them is not a good idea. However:

At the highest level, there is _checkLevel, which takes a level that is either a string or an integer and either returns the corresponding existing level or raises a ValueError.

_checkLevel wraps the dictionary _nameToLevel, which contains all the registered levels (and gets updated by addLevelName).

There is an additional member called _levelToName, which contains the reverse mapping. It is publicly accessible via the getLevelName method.

Solution 3

.level returns the numeric level of the logging event.

Demo:

>>> logger = logging.getLogger()
>>> logger.setLevel('INFO')
>>> logger.level == logging.INFO
True

To avoid the concerns that OP raised in his comment you could use a temporary logger like this:

import logging
import uuid

logging.basicConfig()
logger = logging.getLogger(__file__)


def logLevelToInt(level):
    '''convert given level to int'''
    _id = str(uuid.uuid4())
    logger = logging.getLogger(_id)
    logger.setLevel(level)
    rval = logger.level
    logging.Logger.manager.loggerDict.pop(_id)
    return rval

example:

>>> from basic_args import logLevelToInt
>>> logLevelToInt('DEBUG')
10
>>> logLevelToInt('CRITICAL')
50
>>> import logging
>>> logLevelToInt(logging.INFO)
20
>>>
Share:
11,192

Related videos on Youtube

Mad Physicist
Author by

Mad Physicist

Programming is fun. See my GitHub profile for, among other things, a list of SE posts that turned into open source contributions.

Updated on September 16, 2022

Comments

  • Mad Physicist
    Mad Physicist over 1 year

    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
    
  • Mad Physicist
    Mad Physicist about 8 years
    This is very useful and answers my literal question. However, I want to be able to get the number without setting the log level.
  • Mad Physicist
    Mad Physicist about 8 years
    This is exactly what I was looking for. A query without modifying the state of any logger.
  • shrewmouse
    shrewmouse almost 3 years
    @MadPhysicist, the solution to that would be to create a temporary logger, set its level, look at the logger.level, then delete the logger. might be better than using those private methods.
  • Mad Physicist
    Mad Physicist almost 3 years
    @shrewmouse. Sounds like overkill, and the logging API has now been updated
  • shrewmouse
    shrewmouse almost 3 years
    @MadPhysicist yeah, you're probably right. I just wanted to show how to do it. I'm stuck with an older API.