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")