将所有请求记录到文件Django [英] Log all requests to file Django

查看:351
本文介绍了将所有请求记录到文件Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行django开发服务器( ./ manage.py runserver )时,所有请求的URL都可以方便地登录到进程stdout中,具有精确的时间和响应代码:

When I run the django development server (./manage.py runserver) all requested urls are conveniently logged into process stdout, with the precise time and the response code:

[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

它非常方便,因为在分析输出时,您会立即看到与您的日志消息相对应的请求,例如:

It is very handy because when analysing output you immediately see the request corresponding to your log messages, for example:

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这样:

I used to work with uwsgi+nginx, so I used 'console' logging handler for everything, and then started uwsgi like this:

exec uwsgi --master --die-on-term --logto /var/log/uwsgi.log

作为结果我得到了l在 /var/log/uwsgi.log ,uwsgi的请求记录和我自己的日志记录中的必要日志记录。

As a result I got all the necessary logging in /var/log/uwsgi.log, uwsgi's request records and my own logging messages.

现在我想用Apache + mod WSGI + django来实现同样的结果。我想要唯一的文件在一个地方包含所有请求和我的django应用程序的所有日志。

Now I want to achieve the same result with Apache+mod WSGI+django. I want the only file to contain all requests and all the logs from my django app in one place.

我已经尝试通过Django日志记录配置来实现这一点,当我将django.requests重定向到同一个文件时,我在日志中只收到我自己的消息,根本没有请求。这是配置的一部分:

I've tried to achieve this with Django logging configuration, but even when I redirect django.requests to the same file I get only my own messages in the logs, no requests at all. Here is the portion of the configuration:

'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和我的日志保存在单独的文件中?

Is there a way to achieve nginx+uwsgi+django logging behavior with apache+WSGI+django? Or the only way is to keep apache access.log and my logs in separate files?

我猜第一种情况是开发服务器记录请求,在第二种情况是uwsgi进程。也许有一种方法可以让WSGIDaemonProcess做同样的事情?

I guess in the first case it was development server who logged requests, and in the second case it was uwsgi process. Maybe there's a way to tell WSGIDaemonProcess to do the same?

推荐答案

对于标准的Apache安装,尝试混合访问日志和错误日志的最佳做法。传统上它们是分开的,以便在服务器流量的访问日志上进行分析。

For a standard Apache installation you are going against what is regarded as best practice by trying to mingle both access logs and error logs. Traditionally they have been left separate so that analysis could be done on access logs on server traffic.

这就是说,你是否尝试更改 ErrorLog CustomLog 指令在Apache中使用相同的文件?

That said, have you tried changing the ErrorLog and CustomLog directives in Apache to use the same file?

当我使用mod_wsgi-用命令表达:

When I used mod_wsgi-express with the command:

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 设置为相同的文件,同时将错误和访问日志都转到同一个文件文件。

So technically it should work for a standalone Apache installation with both error and access log going to the same file just by setting ErrorLog and CustomLog to the same file.

对于其他Django日志记录,由于异常可能会在内部产生,您仍然需要:

As for additional Django logging it may generate internally due to exceptions, you still would also need:

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错误日志,以上将是组合的应用程序日志。

This tells Django to notionally log to the terminal, which mod_wsgi will intercept and send to the Apache error log, which with the above will be the combined application log.

BT W,如果想在需要进行标准输出的记录容器中运行Apache / mod_wsgi,请不要自己动手。使用mod_wsgi-express,因为它专门用于容器。在这种情况下,您只需使用:

BTW, if wanting to run Apache/mod_wsgi in a container where logging needs to go to standard output, do not do it yourself. Use mod_wsgi-express as it is specifically designed for use in containers. In that case you would just use:

mod_wsgi-express start-server --access-log --log-to-terminal

如果要启用访问日志记录(默认情况下关闭,通常只是容器部署的噪音),它将担心发送访问和错误日​​志到终端,因此Docker可以捕获它。

if want to enable access logging (off by default as generally just noise for container deployments) and it will worry about sending both access and error logs to the terminal so Docker can capture it.

如果需要更多信息或帮助,请使用mod_wsgi邮件列表。

If need more information or assistance, use the mod_wsgi mailing list.

这篇关于将所有请求记录到文件Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆