2010-12-14 14 views
23

Wydaje mi się, że mam pewne problemy przy próbie wdrożenia logowania do mojego projektu Pythona.Plik konfiguracyjny rejestrowania w języku Python

Ja po prostu próbuje naśladować następującą konfigurację:

Python Logging to Multiple Destinations

Jednak zamiast robić to w kodzie, chciałbym mieć to w pliku konfiguracyjnym.

Poniżej jest mój plik konfiguracyjny:

[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,) 

Problemem jest to, że moje wyjście ekran wygląda następująco:
2010-12-14 11: 39: 04.066 - root - UWAGA - 3
2010-12 -14 11: 39: 04.066 - root - BŁĄD - 4
2010-12-14 11: 39: 04.066 - korzeń - Krytyczny - 5

Mój plik jest odtwarzany, ale wygląda tak samo jak wyżej (choć z dodatkowe informacje zawarte). Jednak poziomy debugowania i informacji nie są wysyłane do żadnej z nich.

jestem na Pythonie 2.7

Oto mój prosty przykład pokazujący niepowodzenie:

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

Odpowiedz

16

To wygląda na to, że ustawiłeś poziomy dla swoich programów obsługi, ale nie logger. Poziom rejestratora filtruje każdą wiadomość, zanim dotrze do swoich programów obsługi, a wartość domyślna to WARNING i wyższa (jak widać). Ustawienie poziomu rejestratora głównego na NOTSET, podobnie jak w przypadku ustawienia go na DEBUG (lub cokolwiek jest najniższym poziomem do zapisania), powinno rozwiązać problem.

11

dodając następującą linię do rejestratora głównego zadbał o mój problem:

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 

Myślę, że powinieneś dodać disable_existing_loggers do false.

Powiązane problemy