Python 3.2使用配置文件进行日志记录会产生KeyError:' formatters'在Raspbian

时间:2015-03-30 12:48:01

标签: python logging raspberry-pi python-3.2 keyerror

我为我的Python应用程序配备了日志记录功能,它在我的Windows系统上使用Python 3.4完美运行。但是当我使用Raspbian和Python 3.2在我的Raspberry Pi上部署应用程序时,收到以下错误:

Traceback (most recent call last):
  File "aurora/aurora_websocket.py", line 265, in <module>
    logging.config.fileConfig('logging.conf')
  File "/usr/lib/python3.2/logging/config.py", line 70, in fileConfig
    formatters = _create_formatters(cp)
  File "/usr/lib/python3.2/logging/config.py", line 106, in _create_formatters
    flist = cp["formatters"]["keys"]
  File "/usr/lib/python3.2/configparser.py", line 941, in __getitem__
    raise KeyError(key)
KeyError: 'formatters'

logging.conf文件(以无BOM的UTF-8编码):

[loggers]
keys=root,simpleExample

[handlers]
keys=screen

[formatters]
keys=simple,complex

[logger_root]
level=NOTSET
handlers=screen

[logger_simpleExample]
level=DEBUG
handlers=screen
qualname=simpleExample
propagate=0

[handler_screen]
class=StreamHandler
level=DEBUG
formatter=complex
args=(sys.stdout,)

[formatter_simple]
format=%(asctime)s - %(levelname)s - %(message)s
datefmt=

[formatter_complex]
format=%(asctime)s - %(levelname)-8s - <%(module)s : %(lineno)d> - %(message)s
datefmt=

日志记录配置以简单的单行加载:

logging.config.fileConfig('logging.conf')

我在这里不知所措,因为如上所述,我的应用程序在Windows上工作正常,但在RPi上失败。

1 个答案:

答案 0 :(得分:5)

嗯,这并没有持续很长时间......事实证明,我已经在不同的工作目录上运行我的RPi应用程序了。因此,相对于不同的工作目录,logging.conf文件的文件路径被错误地解释。不幸的是,日志库试图继续使用不存在的文件,并且在这种情况下不会抛出有用的异常。