Python KeyError log file configuration: 'formatters'

I am currently working on a Python project and I have configured logging using a configuration file. It already worked and logged my posts as wanted.

But then, after rearranging some packages and modules, I get only a key error.

Full trace:

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 log 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 corresponding code:

 if __name__ == '__main__': logging.config.fileConfig('../logging.conf', disable_existing_loggers=False) logger = logging.getLogger(__name__) obc = Onboard_computer('/dev/ttys001') obc.run() 

This is almost the same as from the Python logging tutorial . I really don't understand why this is not working, and it is crazy. This worked, I did not change anything in the code or in the settings, and it just stopped working, and Python throws this KeyError.

My installation: Mac OS X 10.9.2, Eclipse Kepler with PyDev and Python 3.3. I also tested it on Raspberry Pi with Raspbian Wheezy and Python 3.2 and in Eclipse with Python 2.7 (same error).

Do any of you guys have a key?

+20
source share
4 answers

I had this problem because Python could not find my configuration file, although you will never know about this with an error message. Apparently, it is not looking for a configuration file relative to the file in which the code is running, but rather refers to the current working directory (which you can get from os.getcwd() ). I used the following code to initialize the registrar. The log.config file is in the same directory as the file with this code:

 from os import path log_file_path = path.join(path.dirname(path.abspath(__file__)), 'log.config') logging.config.fileConfig(log_file_path) 
+31
source

Try replacing

 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 .. file cannot be found.

+2
source

d512 was true. And when you run the code and based on PYTHONPATH, Python treats the root directory of your project as the "current path", regardless of where the executable is located. Thus, you can add a relative path as follows:

 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 this helps

0
source

In my case, logging.config is present in the grandparent directory and creates a problem even if the correct path is specified in logging.config.fileconfig (). Any help?

0
source

All Articles