Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.4.3:ConfigParser.NoSectionError:无节:';格式化程序&x27;_Python_Logging_Config_Configparser - Fatal编程技术网

Python 2.4.3:ConfigParser.NoSectionError:无节:';格式化程序&x27;

Python 2.4.3:ConfigParser.NoSectionError:无节:';格式化程序&x27;,python,logging,config,configparser,Python,Logging,Config,Configparser,尝试使用日志配置文件实现TimedRotatingFileHandler 只是因为某些原因而不接受配置文件 如有任何建议,我们将不胜感激 x、 py: x、 ini: [loggers] keys=root [logger_root] level=NOTSET handlers=trfhand [handlers] keys=trfhand [handler_trfhand] class=handlers.TimedRotatingFileHandler when=M interval

尝试使用日志配置文件实现
TimedRotatingFileHandler

只是因为某些原因而不接受配置文件

如有任何建议,我们将不胜感激


x、 py:


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=


谢谢

此错误消息准确无误,但具有误导性

“formatters”部分丢失的原因是日志模块找不到传递给
logging.config.fileConfig
的文件


尝试使用绝对文件路径。

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

我通过在一个配置文件中定义一个
BASE\u 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")

怎么了?非常感谢!异常消息没有多大帮助。创建一个名为logging.ini的文件可以解决这个问题。
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")