Python 日志文件配置 KeyError: 'formatters'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23161745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:22:31  来源:igfitidea点击:

Python logging file config KeyError: 'formatters'

pythoneclipseloggingkeyerror

提问by moritzrupp

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.

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

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

但是,在重新排列一些包和模块后,我只收到一个关键错误。

Full Traceback:

完整追溯:

    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'

Here is my logging file:

这是我的日志文件:

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

And the relevant code:

以及相关代码:

if __name__ == '__main__':

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

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

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.

它几乎与Python Logging Tutorial 中的相同。我真的不明白为什么它不起作用并且它会发疯。它起作用了,我对代码和设置都没有进行任何更改,它只是停止工作并且 Python 抛出了这个 KeyError。

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

我的设置:Mac OS X 10.9.2、Eclipse Kepler with PyDev 和 Python 3.3。我还在带有 Raspbian Wheezy 和 Python 3.2 的 Raspberry Pi 和带有 Python 2.7 的 Eclipse 中测试了它(同样的错误)。

Does anyone of you guys have a clue?

你们中有人有线索吗?

回答by José Luis

Try to replace

尝试更换

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

with this

有了这个

logging.config.fileConfig('logging.conf', disable_existing_loggers=False)

Not sure, but probably your logging.confis in your current working directory, and with the ..the file cannot be found.

不确定,但可能您logging.conf在当前工作目录中,并且..找不到该文件。

回答by d512

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.configfile is in the same directory as the file running this code:

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

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

回答by Steven Chen

d512 was correct. And when running the code and based on the PYTHONPATH, Python treats your project root directory as the 'current path' no matter where is your running file located. So another way is to add the relative path either as following:

d512 是正确的。并且在运行代码并基于 PYTHONPATH 时,无论您的运行文件位于何处,Python 都会将您的项目根目录视为“当前路径”。所以另一种方法是添加相对路径,如下所示:

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

or relative to the current file:

或相对于当前文件:

LOGGING_CONFIG = Path(__file__).parent / 'logging.conf'
...
logging.config.fileConfig(LOGGING_CONFIG)

Hope it helps

希望能帮助到你