在python中配置日志记录级别

时间:2014-04-01 05:57:42

标签: python

我在python中有以下日志配置文件:

[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] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s

[handlers]
keys=file,screen

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

[handler_screen]
class=StreamHandler
formatter=complex
level=DEBUG
args=(sys.stdout,)

运行以下python程序:

import logging.config

logging.config.fileConfig("logging.conf")
logging.debug("1")
logging.info("2")
logging.warn("3")
logging.error("4")
logging.critical("5")

输出结果为:

[2014-04-01 11:25:04,720] WARNING [root.<module>:11] 3
[2014-04-01 11:25:04,720] ERROR [root.<module>:12] 4
[2014-04-01 11:25:04,720] CRITICAL [root.<module>:13] 5

我的INFO和DEBUG级别日志条目在哪里?

2 个答案:

答案 0 :(得分:1)

尝试将根记录器级别设置为DEBUG:

[logger_root]
level=DEBUG
handlers=screen,file

来自文档:

  

请注意,根记录器的创建级别为WARNING

答案 1 :(得分:0)

试试这个logging.conf文件

[loggers]
keys=root

[logger_root]
handlers=screen,file
level=DEBUG

[formatters]
keys=simple,complex

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

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

[handlers]
keys=file,screen

[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=5
formatter=complex
args=('spartacus.log',)

[handler_screen]
class=StreamHandler
formatter=complex
args=(sys.stdout,)

您需要设置记录器的级别。