I read on the Django docs how to configure logging. I want to write error message to a file so I can inspect the file. So I copied the code on the docs:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/logs/debug.log',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
},
}
Running python manage.py check gives me the following error:
ValueError: Unable to configure handler 'file'
The directory exists and I even created the file which the docs said will be created.
How are you supposed to make it work if then?
What import statement do you have for the logging module in your settings file?
What version of Python are you running?
Is this in a development or production environment? (Using runserver
or something else? If something else, what?)
(Also please confirm that the directory you created (/logs
) is directly off root and not within your project structure. The path within the filename
attribute is an absolute path and not a relative path.)
from pathlib import Path
import os
python 3.8
django 4.1
debug=True
runserver
Directory structure
root folder
/project
/project/settings.py
/app1
/app2
/logs/debug.log
Thank you
Fixed it my mistake sorry the path in the logging setting must be relative not absolute
That’s not an accurate statement.
The path is whatever you specify. If you specify an absolute path, then you need to ensure that the directory specified by that absolute path exists, and that the process running Django has the appropriate permissions to that path.
In a production-quality deployment on a *nix system, you would be using an absolute path to something like /var/logs/
.
ok apologies for my statement then and thanks for the advice.
No apologies necessary - I’m merely providing clarification for those following behind us who happen to find this thread looking for information.