Python 3.2 logging with config file results in KeyError: 'formatters' on Raspbian

假装没事ソ 提交于 2019-12-20 03:52:15

问题


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'

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/29346678/python-3-2-logging-with-config-file-results-in-keyerror-formatters-on-raspbia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!