2011-08-10 7 views
33

파이썬 스크립트에서 필자는 로깅 모듈을 사용하여 이벤트를 로깅하려고합니다. 내가 logging.debug("Some string")를 실행하려고하면 this page in the docslogging.debug 루트 로거 출력에게 메시지를 가져야한다고 말한다해도, 나는 콘솔에 아무런 출력을 얻을,파이썬 로깅이 아무것도 출력되지 않습니다.

ERROR_FORMAT = "%(levelname)s at %(asctime)s in %(funcName)s in %(filename) at line %(lineno)d: %(message)s" 
DEBUG_FORMAT = "%(lineno)d in %(filename)s at %(asctime)s: %(message)s" 
LOG_CONFIG = {'version':1, 
       'formatters':{'error':{'format':ERROR_FORMAT}, 
          'debug':{'format':DEBUG_FORMAT}}, 
       'handlers':{'console':{'class':'logging.StreamHandler', 
            'formatter':'debug', 
            'level':logging.DEBUG}, 
          'file':{'class':'logging.FileHandler', 
            'filename':'/usr/local/logs/DatabaseUpdate.log', 
            'formatter':'error', 
            'level':logging.ERROR}}, 
       'root':{'handlers':('console', 'file')}} 
logging.config.dictConfig(LOG_CONFIG) 

: 내 로거를 구성하려면 다음 코드가 있습니다. 프로그램에서 아무 것도 출력하지 않는 이유는 무엇입니까? 어떻게 해결할 수 있습니까?

답변

54

기본 로깅 수준은 경고입니다. 레벨을 변경하지 않았으므로 루트 로거 레벨은 계속 경고합니다. 즉, 디버그 로깅을 포함하여 경고보다 낮은 레벨 로깅을 무시합니다.

tutorial에 설명되어 있습니다 : 레벨이 정보를보다 높기 때문에

import logging 
logging.warning('Watch out!') # will print a message to the console 
logging.info('I told you so') # will not print anything 

'정보'라인은 아무것도 인쇄되지 않습니다.

그냥 루트 로거에서 설정, 레벨을 변경하려면 :이 수준 = DEBUG와 핸들러를 정의하는 것만으로는 충분하지 않습니다, 즉

'root':{'handlers':('console', 'file'), 'level':'DEBUG'} 

, 실제 로깅 수준은 순서에 DEBUG해야합니다 그것을 출력하기 위해서.

+0

설명서에 따르면 기본 수준은 NOTSET이며 모든 것을 출력해야하는 0 레벨입니다 ... 왜 이것이 사실이 아니며? – Ben

+0

@ 벤 어디서 그런 말을하니? "기본 수준은 WARNING입니다. 즉, 로깅 패키지가 달리 구성되지 않는 한이 수준 이상의 이벤트 만 추적됩니다." –

+0

https://docs.python.org/3.6/library/logging.html#logging.Logger.setLevel – Ben

관련 문제