使用配置文件的Python 3.2日志记录在Raspbian上导致KeyError:'formatters' [英] Python 3.2 logging with config file results in KeyError: 'formatters' on Raspbian

查看:182
本文介绍了使用配置文件的Python 3.2日志记录在Raspbian上导致KeyError:'formatters'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I equipped my Python application with logging capability and it works flawlessly on my Windows system with Python 3.4. But when I deploy the application on my Raspberry Pi with Raspbian and Python 3.2, I receive the following error:

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编码):

The logging.conf file (encoded in UTF-8 without BOM):

[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=

将日志记录配置加载到一个简单的一列式中:

The logging configuration is loaded in a simple one-liner:

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

我很茫然,因为如上所述,我的应用程序在Windows上可以正常运行,但在RPi上却无法运行.

I'm at a loss here, since, as mentioned above already, my application works fine on Windows, but fails on the RPi.

推荐答案

好吧,这并没有持续很长时间...原来,我已经从另一个工作目录在RPi上运行我的应用程序.因此,相对于不同的工作目录,logging.conf文件的文件路径被错误地解释.不幸的是,日志记录库试图继续处理不存在的文件,并且在这种情况下不会引发有用的异常.

Well, this didn't last long... Turns out, I've been running my application on the RPi from a different working directory. Consequently, the file path to the logging.conf file is interpreted incorrectly relative to the differing working directory. Unfortunately, the logging library tries to continue on with non-existent files and doesn't throw a useful exception in that case.

这篇关于使用配置文件的Python 3.2日志记录在Raspbian上导致KeyError:'formatters'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆