2010-12-14 9 views
23

내 파이썬 프로젝트에 로깅을 구현하는 동안 몇 가지 문제가있는 것 같습니다.파이썬 로깅 설정 파일

단순히 다음과 같은 구성을 모방하려고 해요 :

Python Logging to Multiple Destinations

을하지만 대신에이 코드 내부를하고, 나는 구성 파일에 그것을 가지고 싶습니다. 다음은

내 설정 파일입니다

[loggers] 
keys=root 

[logger_root] 
handlers=screen,file 

[formatters] 
keys=simple,complex 

[formatter_simple] 
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s 

[formatter_complex] 
format=%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s 

[handlers] 
keys=file,screen 

[handler_file] 
class=handlers.TimedRotatingFileHandler 
interval=midnight 
backupCount=5 
formatter=complex 
level=DEBUG 
args=('logs/testSuite.log',) 

[handler_screen] 
class=StreamHandler 
formatter=simple 
level=INFO 
args=(sys.stdout,) 

문제는 내 화면 출력의 모습이다 :
2010-12-14 11 : 39 : 04,066 - 루트 - 경고 - 3
2010-12 -14 11 : 39 : 04,066 - 루트 - ERROR - 4
2010-12-14 11 : 39 : 04,066 - 루트 - CRITICAL - 5

내 파일이 출력이지만, 함께 있지만 (위와 동일한 보이는 추가 정보 포함). 그러나 디버그 및 정보 수준은 출력되지 않습니다.

내가 여기

파이썬 2.7에 나는 내 간단한 예를 실패 보여주고있다 : 루트 로거에 다음 줄을 추가

import os 
import sys 
import logging 
import logging.config 

sys.path.append(os.path.realpath("shared/")) 
sys.path.append(os.path.realpath("tests/")) 

class Main(object): 

    @staticmethod 
    def main(): 
    logging.config.fileConfig("logging.conf") 
    logging.debug("1") 
    logging.info("2") 
    logging.warn("3") 
    logging.error("4") 
    logging.critical("5") 

if __name__ == "__main__": 
    Main.main() 

답변

16

이 핸들러의 레벨을 설정 한 것처럼 보이지만 로거는 설정하지 않은 것입니다. 로거의 레벨은 핸들러에 도달하기 전에 모든 메시지를 필터링하며 기본값은 WARNING 이상입니다 (보시다시피). 루트 로거의 레벨을 NOTSET으로 설정하고 DEBUG (또는 기록하고자하는 가장 낮은 레벨)으로 설정하면 문제가 해결됩니다.

11

나의 문제를 처리했다 :

level=NOTSET 
0
#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import logging 
import logging.handlers 
from logging.config import dictConfig 

logger = logging.getLogger(__name__) 

DEFAULT_LOGGING = { 
    'version': 1, 
    'disable_existing_loggers': False, 
} 
def configure_logging(logfile_path): 
    """ 
    Initialize logging defaults for Project. 

    :param logfile_path: logfile used to the logfile 
    :type logfile_path: string 

    This function does: 

    - Assign INFO and DEBUG level to logger file handler and console handler 

    """ 
    dictConfig(DEFAULT_LOGGING) 

    default_formatter = logging.Formatter(
     "[%(asctime)s] [%(levelname)s] [%(name)s] [%(funcName)s():%(lineno)s] [PID:%(process)d TID:%(thread)d] %(message)s", 
     "%d/%m/%Y %H:%M:%S") 

    file_handler = logging.handlers.RotatingFileHandler(logfile_path, maxBytes=10485760,backupCount=300, encoding='utf-8') 
    file_handler.setLevel(logging.INFO) 

    console_handler = logging.StreamHandler() 
    console_handler.setLevel(logging.DEBUG) 

    file_handler.setFormatter(default_formatter) 
    console_handler.setFormatter(default_formatter) 

    logging.root.setLevel(logging.DEBUG) 
    logging.root.addHandler(file_handler) 
    logging.root.addHandler(console_handler) 



[31/10/2015 22:00:33] [DEBUG] [yourmodulename] [yourfunction_name():9] [PID:61314 TID:140735248744448] this is logger infomation from hello module 

나는 disable_existing_loggers를 false로 추가해야한다고 생각합니다.

관련 문제