[英]apache2 with django - can't write to log file
I have django running on apache2 server. 我在apache2服务器上运行了django。
my settings.py
looks like this: 我的settings.py
看起来像这样:
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. 我的想法基本上是创建一个日志,该日志将以INFO级别写入django应用程序的所有组件。
The issue I am facing is, when running the server, the log is created with root
permissions: 我面临的问题是,在运行服务器时,将使用root
权限创建日志:
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 /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 . 我确实发现了这些问题: 写入日志文件时权限被拒绝,而Django:为生产中的网站设置了有效的日志记录系统 。
and if I change the permissions of the file to be owned by www-data and not root , it works. 如果我将文件的权限更改为www-data而不是root ,则可以使用。
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
? 也许在settings.py
或apache2-config
中的某个地方?
I am looking for the best practice of django logging. 我正在寻找Django日志记录的最佳实践。
EDIT: ps aux | 编辑: ps aux | grep apache2 shows: grep apache2显示:
root 1444 0.0 0.0 97916 7452 ? Ss 13:22 0:00 /usr/sbin/apache2 -k start
ps aux | ps aux | grep wsgi shows: grep wsgi显示:
www-data 1447 0.0 0.2 510528 23692 ? Sl 13:22 0:00 (wsgi:name -k start
Thanks! 谢谢!
事先在服务器中创建一个具有www-data
权限的日志文件夹,并将其路径保存在环境变量中,在settings.py文件中使用python的os.getenv('LOG_PATH')
使用该路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.