Django详细请求日志记录 [英] Django verbose request logging

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

问题描述

问题与这个问题非常相似,但是我想要django将其详细的请求信息记录到我的 logging.FileHandler 中,而不必查看我的Web服务器日志。我尝试并未能设置默认记录器 django.requests 将不同于4xx和5xx
的请求信息写入处理程序。

Question is much the same as this, however I'm wanting django to log it's verbose request information to my logging.FileHandler - ie rather than having to look at my webservers logs. I've tried and failed to setup the default logger django.requests to write something other than 4xx and 5xx request information to the handlers.

因此,在浏览文档和记录器和级别后,我得出的结论是,django实际上并没有做很多日志语句 - 所以我正在寻找确认django不会内部记录请求的非错误情况。

So the conclusion I've come to after going through the docs and fiddling with the loggers and levels is that django does not actually make a lot of log statements - so I'm looking to confirm that django does not internally log non error scenarios for requests.

推荐答案

我不能说我做了详尽的搜索,我可以在django 1.4.1的 django / core / handlers / base.py 中看到, django.request 记录器确实只用于警告和错误条件(4xx / 5xx)。

I can't say I did an exhaustive search, but from what I can see at django/core/handlers/base.py of django 1.4.1, the django.request logger is indeed only used for warnings and error conditions (4xx/5xx).

这就是说,编写可以为您进行各种日志记录的中间件很简单。让你开始的东西可能只是:

That said, it's trivial to write middleware that will do all manner of logging for you. Something very simple to get you started could be just:

from time import time
from logging import getLogger

class LoggingMiddleware(object):
    def __init__(self):
        # arguably poor taste to use django's logger
        self.logger = getLogger('django.request')

    def process_request(self, request):
        request.timer = time()
        return None

    def process_response(self, request, response):
        self.logger.info(
            '[%s] %s (%.1fs)',
            response.status_code,
            request.get_full_path(),
            time() - request.timer
        )
        return response

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

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