[Django]-ValueError: Unable to configure handler 'file': [Errno 2] No such file or directory:

54πŸ‘

βœ…

You do not have path to a log file for some reason (/Users/deon/Documents/PyCharmProjects/Developments/deonproject/log). Make sure that all directories exist (if no, create them) and create an empty debug.log log file (just in case).

What happens is there is some problem with your code happening. Handler catches this error to save it to your log file so that you can analyze it. However, the path to log file it is trying to open does not exist. Thus, exception occures during handling of another exception.

πŸ‘€Dmitry Torba

5πŸ‘

I also faced the same issue, in my case the permissions of the logging file was not right.
so i changed the permissions to read and write to all user with the cmd

sudo chmod a+rwx debug.log

2πŸ‘

In case someone get it useful: In my case the error was triggered by a permissions issue. Once i change permissions and let app user write in folder this was solved.

πŸ‘€Gamugo

2πŸ‘

In my Django project, I implemented some logic that would handle a situation like this:

we can declare some log-path, and if it doesn’t exist we can create the path:

# LOGGING
LOG_DIR = os.path.join(BASE_DIR, 'log')
LOG_FILE = '/api.log'
LOG_PATH = LOG_DIR + LOG_FILE

if not os.path.exists(LOG_DIR):
    os.mkdir(LOG_DIR)

if not os.path.exists(LOG_PATH):
    f = open(LOG_PATH, 'a').close() #create empty log file
else:
    f = open(LOG_PATH,"w").close() #clear log file

Then in our Logging dict we can add the following to our "file" section of the handlers

LOGGING = {
  ...
    'handlers': {
        'file': {
           ...
            'filename': LOG_PATH, 
           ...           
        },
        'console': {
           ...
        }
    },
    
}
πŸ‘€Abrar Mahi

0πŸ‘

  1. Check whether log file exist in /Users/deon/Documents/PyCharmProjects/Developments/deonproject/log/debug.log location.
  2. Check these location have proper write permission
    In my case the ransomware protection was turned on and it preventing it from permission issue
  3. Open the Code editor RUN AS ADMINISTRATOR mode

0πŸ‘

your log path does not exist.
Make sure this path exists

'/Users/deon/Documents/PyCharmProjects/Developments/deonproject/log/debug.log'
πŸ‘€Makan dianka

0πŸ‘

In my case it was due to permission denied as django commands were executed as not owner of the logger files

πŸ‘€P.Netev

0πŸ‘

For example if your handler is configured as

"handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "console",
        },
        "file": {
            "level": "INFO",
            "class": "logging.FileHandler",
            "formatter": "file",
            "filename": "logs/jirate.log",<-----
        },
        "django.server": DEFAULT_LOGGING["handlers"]["django.server"],
    },

ensure that you have the folder logs created in the same level as your manage.py. Django will automatically create the jirate.log file

πŸ‘€Ajani Timothy

0πŸ‘

This error can be caused if you haven’t created the folder of logs file

  • Go to the setting.py file and check the path of your logs file
  • Follow the path and create the folder
πŸ‘€Abdullah Rafi

Leave a comment