close

[Solved] Python logging file config Key: ‘formatters’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python logging file config Key: ‘formatters’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python logging file config Key: ‘formatters’ Error Occurs?

Today I get the following error Python logging file config Key: ‘formatters’ in python.

How To Solve Python logging file config Key: ‘formatters’ Error ?

  1. How To Solve Python logging file config Key: 'formatters' Error ?

    To Solve Python logging file config Key: 'formatters' Error 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

  2. Python logging file config Key: 'formatters'

    To Solve Python logging file config Key: 'formatters' Error 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

Solution 1

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)

Solution 2

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:

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read