Python日志文件配置KeyError:'formatters' [英] Python logging file config KeyError: 'formatters'

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

问题描述

我目前正在处理python项目,并使用配置文件设置了日志记录.它已经可以工作,并且可以根据需要记录我的消息.

I'm currently working on a python project and I set up logging using a config file. It has already worked and was logging my messages as wanted.

但是,在重新排列了一些软件包和模块之后,我只得到一个关键错误.

But then, after rearranging some of the packages and modules, I only get a key error.

完整追溯:

    Traceback (most recent call last):
  File "/Volumes/Daten/Eclipse/workspace/Carputer/src/pyboard/__init__.py", line 42, in <module>
    logging.config.fileConfig('../logging.conf', disable_existing_loggers=False)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/logging/config.py", line 70, in fileConfig
    formatters = _create_formatters(cp)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/logging/config.py", line 103, in _create_formatters
    flist = cp["formatters"]["keys"]
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/configparser.py", line 937, in __getitem__
    raise KeyError(key)
KeyError: 'formatters'

这是我的日志文件:

[loggers]
keys=root,pyBoard

[handlers]
keys=consoleHandler

[formatters]
keys=detailedFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_pyBoard]
level=DEBUG
handlers=consoleHandler
qualname=pyBoard
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=detailedFormatter
args=(sys.stdout,)

[formatter_detailedFormatter]
format=%(asctime)s - %(name)s - %(levelname)s : Line %(lineno)s - %(message)s
datefmt=

以及相关代码:

if __name__ == '__main__':

    logging.config.fileConfig('../logging.conf', disable_existing_loggers=False)
    logger = logging.getLogger(__name__)

    obc = Onboard_computer('/dev/ttys001')
    obc.run()

它与 Python日志记录指南. 我真的不明白为什么它不起作用,它会发疯.它可以正常工作,我在代码或设置上都没有进行任何更改,它只是停止工作,Python抛出了此KeyError.

It is almost the same as from the Python Logging Tutorial. I really don't get why it is not working and it drives crazy. It worked, I changed nothing on the code nor on the setup, and it just stopped working and Python throws this KeyError.

我的设置:Mac OS X 10.9.2,带有PyDev的Eclipse Kepler和Python 3.3.我还在带有Raspbian Wheezy和Python 3.2的Raspberry Pi以及带有Python 2.7的Eclipse中测试了它(相同的错误).

My setup: Mac OS X 10.9.2, Eclipse Kepler with PyDev and Python 3.3. I also tested it on a Raspberry Pi with Raspbian Wheezy and Python 3.2 and in Eclipse with Python 2.7 (same error).

你们中有人有线索吗?

推荐答案

我遇到了这个问题,因为Python找不到我的配置文件,尽管错误消息永远不会告诉您.显然,它不是在相对于运行代码的文件中查找配置文件,而是相对于当前工作目录(您可以从os.getcwd()获取)查找该配置文件.我使用以下代码初始化记录器. log.config文件与运行以下代码的文件位于同一目录中:

I had this issue because Python couldn't find my config file, though you would never know it by the error message. Apparently it does not look for the config file relative to the file in which the code is running, but rather relative to the current working directory (which you can get from os.getcwd()). I used the following code to initialize the logger. The log.config file is in the same directory as the file running this code:

from os import path
log_file_path = path.join(path.dirname(path.abspath(__file__)), 'log.config')
logging.config.fileConfig(log_file_path)

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

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