Python logging file config KeyError: 'formatters'

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.

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.

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

Does anyone of you guys have a clue?


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

Solution 3:

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.conf is in your current working directory, and with the .. the file cannot be found.

Solution 4:

In my case, I was missing the following from my logging.conf file -

[formatters]
keys = json

Below is the structure of the entire file -

[loggers]
keys = root,__main__

[logger_root]
handlers =

[logger___main__]
level = INFO
handlers = __main__
qualname = __main__

[handlers]
keys = __main__

[handler___main__]
class = StreamHandler
level = INFO
formatter = json
args = (sys.stdout,)

[formatters]
keys = json

[formatter_json]
format=%(asctime)f %(module)s %(levelname)s %(message)s %(funcName)s
class = pythonjsonlogger.jsonlogger.JsonFormatter

Python file that uses the logger -

import logging.config
from os import path

log_file_path = path.join(path.dirname(path.abspath('logging.conf')), 'logging.conf')

logging.config.fileConfig(log_file_path, disable_existing_loggers=False)
logger = logging.getLogger(__name__)

logger.info('random message')  
#{"asctime": "2021-10-22 02:48:26,532", "module": "jsonLogger", "levelname": "INFO", "message": "random message", "funcName": "<module>"}

Don't forget to install python-json-logger btw.