Python logging file config KeyError: 'formatters'

后端 未结 4 1737
生来不讨喜
生来不讨喜 2021-02-02 08:45

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 rearran

相关标签:
4条回答
  • 2021-02-02 08:59

    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)
    
    0 讨论(0)
  • 2021-02-02 09:02

    in my case this error was showing up when I was trying to run my flask app on Docker container.

    What helped me is adding:

    flask db init
    

    into the boot.sh that runs at the beginning of container creation.

    0 讨论(0)
  • 2021-02-02 09:04

    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

    0 讨论(0)
  • 2021-02-02 09:10

    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.

    0 讨论(0)
提交回复
热议问题