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

3

107

If I set the logging module to DEBUG with a command line parameter like this:

if (opt["log"] == "debug"):
  logging.basicConfig(level=logging.DEBUG)

How can I later tell if the logger was set to DEBUG? I'm writing a decorator that will time a function if True flag is passed to it, and if no flag is given, it defaults to printing timing information when the root logger is set to DEBUG.

Wreckfish answered 31/12, 2009 at 23:24 Comment(1)
You will eventually want to use something specific instead of coupling this to the logger, such as opt["time_functions"] (which you might default to True/False based on some other option).Mopboard
D
129
logging.getLogger().getEffectiveLevel()

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

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

Duong answered 31/12, 2009 at 23:31 Comment(4)
Excellent, thanks! I was doing something like that (except passing explicit "root" to getLogger), but I was doing it in the init function of my decorator, before the logger was set to debug :\Wreckfish
If you want the name of level, not the number, you can use this to convert the number to a string (like 'INFO'): logging.getLevelName()Nasho
@guettli, getLevelName() requires one argument containing the level whose textual representation you want to get. So the call is actually this beast: logging.getLevelName(logging.getLogger().getEffectiveLevel()). It would be nice to have a simpler syntax when all you want is the string for the current level.Removal
To convert the level integer to the name: docs.python.org/3/library/logging.html#levelsMaddie
D
134

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()
Dilatant answered 8/1, 2015 at 21:29 Comment(1)
This should be the accepted answer, since it does the same thing with lower runtime complexity.Plot
D
129
logging.getLogger().getEffectiveLevel()

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

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

Duong answered 31/12, 2009 at 23:31 Comment(4)
Excellent, thanks! I was doing something like that (except passing explicit "root" to getLogger), but I was doing it in the init function of my decorator, before the logger was set to debug :\Wreckfish
If you want the name of level, not the number, you can use this to convert the number to a string (like 'INFO'): logging.getLevelName()Nasho
@guettli, getLevelName() requires one argument containing the level whose textual representation you want to get. So the call is actually this beast: logging.getLevelName(logging.getLogger().getEffectiveLevel()). It would be nice to have a simpler syntax when all you want is the string for the current level.Removal
To convert the level integer to the name: docs.python.org/3/library/logging.html#levelsMaddie
R
3

Just

logging.getLogger().level == logging.DEBUG
Romantic answered 27/12, 2018 at 6:15 Comment(1)
This is a bad answer; it only works to check an exact level not effective like a comment elsewhere. isEnabledFor() exists for this reason!Print

© 2022 - 2024 — McMap. All rights reserved.