2010-12-14 180 views
23

我似乎在尝试将日志记录到我的python项目时遇到了一些问题。Python日志记录配置文件

我只是试图模仿如下配置:

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:04066 - 根 - 警告 - 3
2010-12 -14 11:39:04066 - 根 - 错误 - 4
2010-12-14 11:39:04066 - 根 - CRITICAL - 5

我的文件被输出,但与看起来与上述相同的(尽管包括额外信息)。但是,调试和信息级别不会输出到任何一个。

我对Python的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。