带有 Apache 和 mod_wsgi 的 Django:除非设置 Debug = True,否则没有系统日志

Tho*_*mas 5 python logging django mod-wsgi apache-2.4

我正在使用 apache 网络服务器和 mod_wsgi 将请求传输到 django。

$ apache2ctl -v
Server version: Apache/2.4.10 (Raspbian)
Server built:   Sep 17 2016 16:40:43
Run Code Online (Sandbox Code Playgroud)

我正在使用这个 apache 站点来声明 django 应用程序:

ServerName example.com
DocumentRoot /srv/webapps/myapp

<Directory /srv/wepapps/myapp/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess example.com python-path=/srv/webapps/myapp:/usr/local/lib/python3.4/dist-packages:/usr/lib/python3/dist-packages
WSGIProcessGroup example.com
WSGIScriptAlias / /srv/webapps/myapp/mysite/wsgi.py

LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但如果引发异常,它会显示错误 500,但我的example.com_error.log. 如果我将设置修改为 set Debug = True,我可以在 Web 浏览器中看到错误,但在我的 /var/log/apache2 文件中也可以看到错误。但我真的不想在我的生产环境中保留这个设置。

你知道为什么我必须Debug = True允许 django 写入系统日志吗?

在此先感谢您的回答,如果我在英语方面犯了一些错误,我深表歉意;-)

小智 2

在 settings.py 文件中,您需要将以下内容添加到 LOGGING 配置中:

LOGGING = {
    #  ... omitting the formatters and handlers for brevity ...
    'loggers': {
        # ...  you may have other loggers here as well ...
        'django': {
            'handlers': ['name_of_your_file_handler_goes_here'],
            'level': 'DEBUG',
            'propagate': True,
        }
    }
Run Code Online (Sandbox Code Playgroud)

您可能应该在生产中将级别设置为 ERROR 或 WARN。