Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 352
Mad Physicist Avatar asked Feb 28 '16 05:02

Mad Physicist


People also ask

What is level in logging Python?

There are six log levels in Python; each level is associated with an integer that indicates the log severity: NOTSET=0, DEBUG=10, INFO=20, WARN=30, ERROR=40, and CRITICAL=50. All the levels are rather straightforward (DEBUG < INFO < WARN ) except NOTSET, whose particularity will be addressed next.

What are the five levels of logging in Python?

In Python, the built-in logging module can be used to log events. Log messages can have 5 levels - DEBUG, INGO, WARNING, ERROR and CRITICAL. They can also include traceback information for exceptions. Logs can be especially useful in case of errors to help identify their cause.

How do I print logging INFO in Python?

Python - Print Logs in a File. If you want to print python logs in a file rather than on the console then we can do so using the basicConfig() method by providing filename and filemode as parameter. The format of the message can be specified by using format parameter in basicConfig() method.


Video Answer


4 Answers

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
>>> 
like image 97
Vinay Sajip Avatar answered Oct 11 '22 11:10

Vinay Sajip


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.

like image 39
Mad Physicist Avatar answered Oct 11 '22 11:10

Mad Physicist


.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
>>>
like image 7
styvane Avatar answered Oct 11 '22 09:10

styvane


You can also use:

import logging
logging.getLevelName('INFO')
like image 1
ronkov Avatar answered Oct 11 '22 11:10

ronkov