简体   繁体   English

有没有办法将使用“fileConfig”读取的日志配置格式转换为“dictConfig”所需的格式?

[英]Is there a way to convert logging config format read using "fileConfig" to format needed for "dictConfig"?

I have a config file config.ini as follows,我有一个配置文件config.ini如下,

[loggers]
keys=root

[handlers]
keys=consoleHandler

[formatters]
keys=sampleFormatter

[logger_root]
level=INFO
handlers=consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=sampleFormatter
args=(sys.stderr,)

[formatter_sampleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

which I'm reading from my Python script as follows:我正在从我的 Python 脚本中读取如下内容:

import logging

logging.config.fileConfig(fname="config.ini", disable_existing_loggers=False)

Now, I want to send this format read from the above file as a dictionary.现在,我想将从上述文件中读取的这种格式作为字典发送。 So I use configparser to read this file and then convert to a dictionary as follows,所以我使用configparser来读取这个文件,然后如下转换为字典,

import configparser

parser = configparser.RawConfigParser()
parser.read(filenames="config.ini")
conf_dict = {section: dict(parser.items(section)) for section in parser.sections()}

Then, I learned that logging module's dictConfig requires the key version , so I add it as follows,然后,我了解到logging模块的dictConfig需要 key version ,所以我添加如下,

conf_dict["version"] = 1

Then, I try to read this dictionary into dictConfig as follows,然后,我尝试将这本字典读入dictConfig ,如下所示,

logging.config.dictConfig(config_dict)

However, this results in the following error,但是,这会导致以下错误,

  File "run.py", line 41, in load
    logging.config.dictConfig(config_dict)
  File "/python3.7/logging/config.py", line 799, in dictConfig
  File "/python3.7/logging/config.py", line 545, in configure
ValueError: Unable to configure formatter 'keys'

This seems to indicate that the two utilities of the logging module, fileConfig and dictConfig , accept two different formats.这似乎表明logging模块的两个实用程序fileConfigdictConfig接受两种不同的格式。 Is there a way to translate between these two formats?有没有办法在这两种格式之间进行转换? If so, how?如果是这样,如何?

No, there is no parser in the standard library that'll take an ini file and spit out a dictConfig compatible dictionary.不,标准库中没有解析器会接受一个ini文件并吐出一个 dictConfig 兼容字典。 You will have to create your own json file format to populate your dictionary.您必须创建自己的 json 文件格式来填充您的字典。

You can find a good JSON file example here .您可以在此处找到一个很好的 JSON 文件示例。

As of Python 3.7 the two (dictConfig and fileConfig) cannot be expressed equivalently.从 Python 3.7 开始,这两个(dictConfig 和 fileConfig)不能等价表达。 For example, the "args" key can only be provided with the fileConfig.例如,“args”键只能与 fileConfig 一起提供。 It will be ignored when provided with dictConfig.当与 dictConfig 一起提供时,它将被忽略。

As a side note, I came across this when trying to specify "args" for a dictConfig so that I could specify a file path that uses environment variables for a FileHandler.作为旁注,我在尝试为 dictConfig 指定“args”时遇到了这个问题,以便我可以为 FileHandler 指定一个使用环境变量的文件路径。

Reference: config.py参考: config.py

_install_handlers - looks for the args key to evaluate it _install_handlers - 寻找args键来评估它

configure_handler - does not look for the args key in the dict configure_handler - 不在字典中查找args

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM