[英]Python 2.4.3: ConfigParser.NoSectionError: No section: 'formatters'
嘗試使用日志記錄配置文件來實現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'
謝謝
錯誤消息嚴格准確但有誤導性。
缺少“formatters”部分的原因是因為日志記錄模塊找不到您傳遞給logging.config.fileConfig
的文件。
嘗試使用絕對文件路徑。
是的,@ yehumoro是對的。 似乎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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.