Determines if the root log is set to DEBUG in Python?

If I installed the registration module in DEBUG with a command line parameter as follows:

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

How can I later tell if logger has been set to DEBUG? I am writing a decorator that will time the function if the True flag is passed to it, and if the flag is not specified, it is by default to print information about the time when the root log is set to DEBUG.

+77
python logging decorator
Dec 31 '09 at 23:24
source share
3 answers
 logging.getLogger().getEffectiveLevel() 

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

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

+94
Dec 31 '09 at 23:31
source share

There is actually one better thing: use the code logging.getLogger().isEnabledFor(logging.DEBUG) . I found this while trying to figure out what to do with the result of getEffectiveLevel() .

Below is the code that uses the registration module itself.

 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() 
+86
Jan 08 '15 at 21:29
source share

Simply

 logging.getLogger().level == logging.DEBUG 
+2
Dec 27 '18 at 6:15
source share



All Articles