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