Python 2.4.3:ConfigParser.NoSectionError:没有部分:“格式化程序”

发布于 2024-12-12 18:40:12 字数 1122 浏览 0 评论 0原文

尝试使用日志配置文件来实现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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此岸叶落 2024-12-19 18:40:12

该错误消息严格准确,但具有误导性。

缺少“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.

触ぅ动初心 2024-12-19 18:40:12

是的,@ekhumoro 是对的。 似乎logging需要绝对路径。Python团队应该将此错误消息更改为更具可读性的内容;它只是看不到我的文件,不是因为配置文件本身是错误的。

我设法通过在配置文件中定义 BASE_DIR 变量并将其作为路径的前缀导入来解决这个问题,就像 Django 所做的那样。并且,如果未创建日志文件的父目录,请记住创建它们。

我在 config.py 中定义路径和记录器:

import os
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

import logging
import logging.config
logging.config.fileConfig(os.path.join(BASE_DIR, 'utils', 'logger.conf')) # the `logger.conf` locates under 'myproject/utils/'
logger = logging.getLogger("mylog") # 'mylog' should exist in `logger.conf` in the logger part

在其他模块中:

from config import logger
...

logger.info("Your loggings modules should work now!! - WesternGun")

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:

import os
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

import logging
import logging.config
logging.config.fileConfig(os.path.join(BASE_DIR, 'utils', 'logger.conf')) # the `logger.conf` locates under 'myproject/utils/'
logger = logging.getLogger("mylog") # 'mylog' should exist in `logger.conf` in the logger part

In other modules:

from config import logger
...

logger.info("Your loggings modules should work now!! - WesternGun")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文