Writing in Python: can dictConfig be read from a file?

Examples of using dictConfig to configure Python logging show that a dictionary will be created in a Python script. I would like to use a configuration file, but the documentation says that fileConfig is older and less capable than dictConfig. Is it possible to configure dictConfig from a configuration file?

Many thanks.

+4
source share
3 answers

Since it dictConfigjust uses a dictionary, you can build a dictionary in any way convenient for you. One example of reading from a file is a JSON format configuration. Then you can just do something like

import json
import logging

with open('myconfig.json') as f:
    config_dict = json.load(f)
    logging.config.dictConfig(config_dict)

if the JSON file has an appropriate schema.

, YAML, .

+7

, , dictConfig.

, . , ; , dictConfig, dictConfig.

+1

dictConfig dict , . dict , , , , , .

, , , dict (, )

0

Source: https://habr.com/ru/post/1621147/


All Articles