code

Python에서 루트 로거가 DEBUG 수준으로 설정되어 있는지 확인합니까?

codestyles 2020. 9. 18. 08:13
반응형

Python에서 루트 로거가 DEBUG 수준으로 설정되어 있는지 확인합니까?


다음과 같은 명령 줄 매개 변수를 사용하여 로깅 모듈을 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

참고 URL : https://stackoverflow.com/questions/1987468/determining-if-root-logger-is-set-to-debug-level-in-python

반응형