记录对文件 Django 的所有请求
Log all requests to file Django
当我 运行 django 开发服务器 (./manage.py runserver
) 时,所有请求的 url 都方便地登录到进程 stdout,具有准确的时间和响应代码:
[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,所以我使用 'console' 日志记录处理程序,然后像这样启动 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,
},
}
有没有办法用apache+WSGI+django实现nginx+uwsgi+django的日志行为?或者唯一的方法是将 apache access.log 和我的日志保存在单独的文件中?
我猜第一种情况是开发服务器记录了请求,第二种情况是 uwsgi 进程。也许有办法告诉 WSGIDaemonProcess 做同样的事情?
对于标准的 Apache 安装,您将通过尝试混合访问日志和错误日志来违背被视为最佳实践的做法。传统上,它们是分开的,以便可以对服务器流量的访问日志进行分析。
也就是说,您是否尝试过更改 Apache 中的 ErrorLog
和 CustomLog
指令以使用相同的文件?
当我使用 mod_wsgi-express 命令时:
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
如果要启用访问日志记录(默认情况下关闭,因为通常只是容器部署的噪音)并且它会担心将访问日志和错误日志发送到终端以便 Docker 可以捕获它。
如果需要更多信息或帮助,请使用 mod_wsgi 邮件列表。
当我 运行 django 开发服务器 (./manage.py runserver
) 时,所有请求的 url 都方便地登录到进程 stdout,具有准确的时间和响应代码:
[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,所以我使用 'console' 日志记录处理程序,然后像这样启动 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,
},
}
有没有办法用apache+WSGI+django实现nginx+uwsgi+django的日志行为?或者唯一的方法是将 apache access.log 和我的日志保存在单独的文件中?
我猜第一种情况是开发服务器记录了请求,第二种情况是 uwsgi 进程。也许有办法告诉 WSGIDaemonProcess 做同样的事情?
对于标准的 Apache 安装,您将通过尝试混合访问日志和错误日志来违背被视为最佳实践的做法。传统上,它们是分开的,以便可以对服务器流量的访问日志进行分析。
也就是说,您是否尝试过更改 Apache 中的 ErrorLog
和 CustomLog
指令以使用相同的文件?
当我使用 mod_wsgi-express 命令时:
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
如果要启用访问日志记录(默认情况下关闭,因为通常只是容器部署的噪音)并且它会担心将访问日志和错误日志发送到终端以便 Docker 可以捕获它。
如果需要更多信息或帮助,请使用 mod_wsgi 邮件列表。