簡體   English   中英

確定Python中的根記錄器是否設置為DEBUG級別?

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

如果我使用命令行參數將日志記錄模塊設置為DEBUG:

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

我怎么能告訴記錄器是否設置為DEBUG? 我正在編寫一個裝飾器,如果傳遞了True標志,它將為一個函數計時,如果沒有給出標志,它將默認為在根記錄器設置為DEBUG時打印定時信息。

logging.getLogger().getEffectiveLevel()

不帶參數的logging.getLogger()獲取根級別記錄器。

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

實際上,還有一個更好: 使用代碼logging.getLogger().isEnabledFor(logging.DEBUG) 我在試圖理解如何處理getEffectiveLevel()的結果時發現了它。

以下是日志記錄模塊本身使用的代碼。

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()

只是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM