【问题标题】:Log all requests to file Django记录所有对文件 Django 的请求
【发布时间】:2016-10-10 16:53:21
【问题描述】:

当我运行 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,所以我对所有内容都使用了“控制台”日志记录处理程序,然后像这样启动 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 做同样的事情?

【问题讨论】:

    标签: python django apache logging wsgi


    【解决方案1】:

    对于标准的 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 邮件列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-18
      • 2013-03-12
      • 2017-02-27
      • 2018-02-08
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      相关资源
      最近更新 更多