记录所有请求提交的Django
当我运行Django开发服务器(./manage.py runserver
)所有请求的网址方便地登录到工艺标准输出,配合精确的时间和响应代码:记录所有请求提交的Django
[09/Jun/2016 23:35:53] "GET /api/game/ HTTP/1.1" 404 3185
[09/Jun/2016 23:36:01] "GET /api/game/123/ HTTP/1.1" 404 1735
这是非常方便,因为分析输出时,您可以立即看到该请求相对应的日志信息,例如:
WARNING:2016-06-09 23:41:27,806:views:7449:140139847718656: No such object in the database: u'123'
[09/Jun/2016 23:41:27] "GET /api/game/123/ HTTP/1.1" 404 1735
我使用uwsgi工作+ nginx的,所以我用“控制台”日志处理程序的一切,然后开始uwsgi这样:
exec uwsgi --master --die-on-term --logto /var/log/uwsgi.log
因此,我在/var/log/uwsgi.log
,uwsgi的请求记录和我自己的日志消息中获得了所有必要的日志记录。
现在我想用Apache + mod WSGI + django实现相同的结果。我希望唯一的文件包含来自我的django应用程序的所有请求和所有日志。
我试图用Django的日志记录配置实现这一点,但即使当我将django.requests重定向到同一个文件时,我在日志中只获取自己的消息,根本没有请求。下面是配置的一部分:
'handlers': {
'file_handler': {
'level': DEBUG and 'DEBUG' or 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': join(LOG_DIRECTORY, 'api_log.log'),
'maxBytes': 1024 * 1024 * 5, # 5 MB
'backupCount': 15,
'formatter': 'verbose',
},
},
'loggers': {
'api': {
'handlers': ['file_handler'],
'level': DEBUG and 'DEBUG' or 'INFO',
},
'django': {
'handlers': ['file_handler'],
'level': DEBUG and 'DEBUG' or 'INFO',
},
'django.request': {
'handlers': ['file_handler'],
'level': DEBUG and 'DEBUG' or 'INFO',
},
'django.db.backends': {
'handlers': ['file_handler'],
'level': DEBUG and 'INFO' or 'WARNING',
'propagate': False,
},
}
有没有办法实现的nginx + uwsgi + Django的登录行为与Apache + WSGI + Django的?或者唯一的办法是保持apache access.log和我的日志在单独的文件?
我想在第一种情况下,它是开发服务器谁记录了请求,在第二种情况下,它是uwsgi进程。也许有一种方法可以告诉WSGIDaemonProcess执行相同的操作吗?
对于标准的Apache安装,您正试图混合访问日志和错误日志,从而违背了最佳实践。传统上,它们已经分开,因此可以对服务器流量上的访问日志进行分析。
这就是说,你有没有试过改变Apache中的ErrorLog
和CustomLog
指令来使用相同的文件?
当我使用mod_wsgi的快车与命令:
mod_wsgi-express start-server --access-log --access-log-name application.log --error-log-name application.log
它产生:
<IfDefine MOD_WSGI_ROTATE_LOGS>
ErrorLog "|/usr/sbin/rotatelogs \
/tmp/mod_wsgi-localhost:8000:502/application.log.%Y-%m-%d-%H_%M_%S 5M"
</IfDefine>
<IfDefine !MOD_WSGI_ROTATE_LOGS>
ErrorLog "/tmp/mod_wsgi-localhost:8000:502/application.log"
</IfDefine>
LogLevel warn
<IfDefine MOD_WSGI_ACCESS_LOG>
<IfModule !log_config_module>
LoadModule log_config_module ${MOD_WSGI_MODULES_DIRECTORY}/mod_log_config.so
</IfModule>
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
LogFormat "undefined" custom
<IfDefine MOD_WSGI_ROTATE_LOGS>
CustomLog "|/usr/sbin/rotatelogs \
/tmp/mod_wsgi-localhost:8000:502/application.log.%Y-%m-%d-%H_%M_%S 5M" common
</IfDefine>
<IfDefine !MOD_WSGI_ROTATE_LOGS>
CustomLog "/tmp/mod_wsgi-localhost:8000:502/application.log" common
</IfDefine>
</IfDefine>
与所得到的application.log
之中:
[Fri Jun 10 07:17:30.845264 2016] [mpm_prefork:notice] [pid 84334] AH00163: Apache/2.4.18 (Unix) mod_wsgi/4.5.2 Python/2.7.10 configured -- resuming normal operations
[Fri Jun 10 07:17:30.845518 2016] [core:notice] [pid 84334] AH00094: Command line: 'httpd (mod_wsgi-express) -f /tmp/mod_wsgi-localhost:8000:502/httpd.conf -D MOD_WSGI_ACCESS_LOG -D FOREGROUND'
::1 - - [10/Jun/2016:07:17:36 +1000] "GET/HTTP/1.1" 200 709
::1 - - [10/Jun/2016:07:17:37 +1000] "GET/HTTP/1.1" 200 709
::1 - - [10/Jun/2016:07:17:37 +1000] "GET/HTTP/1.1" 200 709
::1 - - [10/Jun/2016:07:17:38 +1000] "GET/HTTP/1.1" 200 709
::1 - - [10/Jun/2016:07:17:38 +1000] "GET/HTTP/1.1" 200 709
[Fri Jun 10 07:17:39.784486 2016] [mpm_prefork:notice] [pid 84334] AH00169: caught SIGTERM, shutting down
因此从技术上讲它应该工作对于同时出现错误和访问日志的独立Apache安装只需将ErrorLog
和CustomLog
设置为相同的文件即可。
至于额外的Django的记录也可能在内部产生因异常,你仍然还需要:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
},
},
}
这告诉Django,以名义上登录到终端,这mod_wsgi的将拦截和发送到Apache的错误日志,这与上面的将是组合的应用程序日志。
顺便说一句,如果想在运行Apache/mod_wsgi的日志需要转到标准输出的容器中,请不要自己动手。使用mod_wsgi-express,因为它专门设计用于容器。在这种情况下,您只需使用:
mod_wsgi-express start-server --access-log --log-to-terminal
如果要启用访问日志记录(默认关闭的通常只是容器部署噪声),它会担心派出了访问和错误日志发送到终端,从而泊坞窗可以捕获它。
如果需要更多信息或帮助,请使用mod_wsgi邮件列表。