The Python Oracle

Determining if root logger is set to DEBUG level in Python?

This video explains
Determining if root logger is set to DEBUG level in Python?

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Mysterious Puzzle

--

Chapters
00:00 Question
00:36 Accepted answer (Score 125)
00:54 Answer 2 (Score 127)
01:43 Answer 3 (Score 3)
01:55 Thank you

--

Full question
https://stackoverflow.com/questions/1987...

Accepted answer links:
[http://docs.python.org/library/logging.h...]: http://docs.python.org/library/logging.h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #logging #decorator



ANSWER 1

Score 135


Actually, there's one better: use the code logging.getLogger().isEnabledFor(logging.DEBUG). I found it while trying to understand what to do with the result of getEffectiveLevel().

Below is the code that the logging module itself uses.

def getEffectiveLevel(self):
    """
    Get the effective level for this logger.

    Loop through this logger and its parents in the blogger hierarchy,
    looking for a non-zero logging level. Return the first one found. 
    """
    logger = self
    while logger:
        if logger.level:
            return logger.level
        logger = logger.parent
    return NOTSET

def isEnabledFor(self, level):
    """
    Is this logger enabled for level ‘level’?
    """
    if self.manager.disable >= level:
        return 0
    return level >= self.getEffectiveLevel()



ACCEPTED ANSWER

Score 128


logging.getLogger().getEffectiveLevel()

logging.getLogger() without arguments gets the root level logger.

http://docs.python.org/library/logging.html#logging.Logger.getEffectiveLevel




ANSWER 3

Score 3


Just

logging.getLogger().level == logging.DEBUG