I have a config file config.ini
as follows,
[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:
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,
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,
conf_dict["version"] = 1
Then, I try to read this dictionary into dictConfig
as follows,
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. Is there a way to translate between these two formats? If so, how?