I have django running on apache2 server.
my settings.py
looks like this:
WSGI_APPLICATION = 'my_app.wsgi.application'
...
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s: %(message)s'
}
},
'handlers': {
'file': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '../django.log',
'formatter': 'verbose',
'maxBytes': 1024 * 1024 * 5, # 5 MB
'backupCount': 5,
},
},
'loggers': {
'my_app': {
'handlers': ['file'],
'level': 'INFO',
'propagate': True,
},
},
}
My idea was basically to create a log that all the components of my django app will write to, with level INFO.
The issue I am facing is, when running the server, the log is created with root
permissions:
ll ../django.log
-rw-r--r-- 1 root root 0 Jan 9 10:17 ../django.log
And so, what happens when I try to log to it is:
/var/log/apache2/error.log
[Wed Jan 09 11:37:43.677755 2019] [:error] [pid 1457:tid 140554321598208] [remote 192.168.254.52:60257] ValueError: Unable to configure handler 'file': [Errno 13] Permission denied: '../django.log'
I did find those issues: Permission Denied when writing log file and Django: Setup an efficient logging system for a website in production.
and if I change the permissions of the file to be owned by www-data and not root, it works.
My question is - where is the right place to set this in production? should it be changing it manually? maybe somewhere in the settings.py
or the apache2-config
?
I am looking for the best practice of django logging.
EDIT:
ps aux | grep apache2 shows:
root 1444 0.0 0.0 97916 7452 ? Ss 13:22 0:00 /usr/sbin/apache2 -k start
ps aux | grep wsgi shows:
www-data 1447 0.0 0.2 510528 23692 ? Sl 13:22 0:00 (wsgi:name -k start
Thanks!
Copyright Notice:Content Author:「Ofek Agmon」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54110184/apache2-with-django-cant-write-to-log-file