Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get logger level as string value

Tags:

python

logging

Using getEffectiveLevel I can query what the logger current level is:

import logging
logger = logging.getLogger('top')
level_number = logger.getEffectiveLevel()

The returned by getEffectiveLevel method value is the integer. I wonder if there is a way of querying a corresponding the string value such as "DEBUG" or "INFO" instead of an integer.

like image 769
alphanumeric Avatar asked Dec 23 '16 19:12

alphanumeric


1 Answers

If you place the result of a call to the object's getEffectiveLevel() method into the logging module's getLevelName() class method, you get returned to you the string representation of the level:

import logging

log = logging.getLogger('blah')
str_level = logging.getLevelName(log.getEffectiveLevel())

print(str_level)

Output:

WARNING
like image 79
stevieb Avatar answered Sep 19 '22 10:09

stevieb