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

后端 未结 3 356
小鲜肉
小鲜肉 2020-12-23 19:55

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

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


        
相关标签:
3条回答
  • 2020-12-23 20:43

    Just

    logging.getLogger().level == logging.DEBUG
    
    0 讨论(0)
  • 2020-12-23 20:55

    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()
    
    0 讨论(0)
  • 2020-12-23 20:56
    logging.getLogger().getEffectiveLevel()
    

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

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

    0 讨论(0)
提交回复
热议问题