尝试使用日志配置文件来实现TimedRotatinigFileHandler
。
只是因为某些原因不接受配置文件。
任何建议都很感激。
x.py公司:
import logging
import logging.config
import logging.handlers
logging.config.fileConfig("x.ini")
MyLog = logging.getLogger('x')
MyLog.debug('Starting')
x.ini:
[loggers]
keys=root
[logger_root]
level=NOTSET
handlers=trfhand
[handlers]
keys=trfhand
[handler_trfhand]
class=handlers.TimedRotatingFileHandler
when=M
interval=1
backupCount=11
formatter=generic
level=DEBUG
args=('/var/log/x.log',)
[formatters]
keys=generic
[formatter_generic]
class=logging.Formatter
format=%(asctime)s %(levelname)s %(message)s
datefmt=
Traceback (most recent call last):
File "x.py", line 5, in ?
logging.config.fileConfig("x.ini")
File "/usr/lib/python2.4/logging/config.py", line 76, in fileConfig
flist = cp.get("formatters", "keys")
File "/usr/lib/python2.4/ConfigParser.py", line 511, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'formatters'
谢谢
是的,@ekhuroo是对的。似乎
logging
需要绝对路径。Python团队应该将此错误消息更改为更可读的内容;它只是看不到我的文件,而不是因为配置文件本身是错误的。我通过在配置文件中定义一个
BASE_DIR
变量并将其作为路径前缀导入来解决这个问题,就像Django所做的那样。而且,如果没有创建日志文件的父目录,请记住创建它们。我在
config.py
中定义路径和记录器:在其他模块中:
这个错误信息严格来说是准确的,但有误导性。
“格式化程序”部分丢失的原因是日志模块找不到您传递给
logging.config.fileConfig
的文件。尝试使用绝对文件路径。
相关问题 更多 >
编程相关推荐