Python 2.4.3:ConfigParser.NoSectionError:没有部分:“格式化程序”
尝试使用日志配置文件来实现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'
谢谢
Trying to use a logging configuration file to implement TimedRotatinigFileHandler
.
Just won't take the config file for some reason.
Any suggestions appreciated.
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'
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误消息严格准确,但具有误导性。
缺少“formatters”部分的原因是日志记录模块找不到您传递给
logging.config.fileConfig
的文件。尝试使用绝对文件路径。
The error message is strictly accurate but misleading.
The reason the "formatters" section is missing, is because the logging module can't find the file you passed to
logging.config.fileConfig
.Try using an absolute file path.
是的,@ekhumoro 是对的。 似乎
logging
需要绝对路径。Python团队应该将此错误消息更改为更具可读性的内容;它只是看不到我的文件,不是因为配置文件本身是错误的。我设法通过在配置文件中定义 BASE_DIR 变量并将其作为路径的前缀导入来解决这个问题,就像 Django 所做的那样。并且,如果未创建日志文件的父目录,请记住创建它们。
我在 config.py 中定义路径和记录器:
在其他模块中:
Yes, @ekhumoro was right. It seems that
logging
expects an absolute path. Python team should change this error message to something more readable; it just cannot see my file, not because the config file itself is wrong.I managed to solve this by defining a
BASE_DIR
variable in a config file and import it as the prefix of the path, just as Django does. And, remember to create the log file's parent dir(s) if they are not created.I define the path and the logger in
config.py
:In other modules: