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
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.
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.
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.
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
>>>
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.
.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
>>>
You can also use:
import logging
logging.getLevelName('INFO')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With