django-如何使用日志记录过滤掉GET静态消息和媒体消息? [英] django - how to filter out GET static and media messages with logging?

查看:382
本文介绍了django-如何使用日志记录过滤掉GET静态消息和媒体消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个包含大量图像的页面,因此这会在这种类型的控制台中生成大量输出.在我的开发环境中,我使用django提供静态和媒体服务,因此我在控制台中获得了很多:

I'm working on a page that has lots of images so this generates lots of output in the console of this type. In my dev environment I use django to serve static and media, so I get a LOT of this in my console:

...
[23/May/2014 12:41:54] "GET /static/css/style.css HTTP/1.1" 304 0
[23/May/2014 12:41:55] "GET /static/js/jquery-1.7.1.min.js HTTP/1.1" 304 0
[23/May/2014 12:41:55] "GET /static/js/jquery.form.js HTTP/1.1" 304 0
...
[23/May/2014 12:41:57] "GET /media/producto/Tapa_Santiago_Vazquez_SV.jpg HTTP/1.1" 304 0
[23/May/2014 12:41:57] "GET /media/CACHE/images/producto/Barcos_y_mariposas_DVD_baja/2e3e3894ca08f88c03459e00f9018427.jpg HTTP/1.1" 304 0
[23/May/2014 12:41:56] "GET /media/CACHE/images/producto/tapaDEJA_VU/fb67e92ffd47808a263db02ca016bc24.jpg HTTP/1.1" 304 0
...

寻找有意义的输出变得非常乏味.

making it very tedious to look for meaningful output.

我想过滤掉我环境中的那些消息,所以我只看到视图和输出的GET,但是到目前为止,我看到的日志记录可能影响django的其他日志记录,但没有影响.我什至尝试过,但是没有用:

I would like to filter out those messages in my environment so I only see the GET for the view and my output, but so far looking at the logging I saw that I could affect other logging from django but not this. I even tried this but it didn't work:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'handlers': {
        'null': {
            'level': 'ERROR',
            'class': 'django.utils.log.NullHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['null'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

甚至有可能过滤掉这种输出?

is it even possible to filter that kind of output out?

谢谢!

推荐答案

Django的最新版本非常容易用您自己的LOGGING设置覆盖默认日志记录.

Recent versions of Django make it really easy to override the default logging with your own LOGGING settings.

要过滤掉所有对/static/目录的GET请求,请将以下内容添加到您的settings.py:

To filter out all GET requests to the /static/ directory, add the following to your settings.py:

def skip_static_requests(record):
    if record.args[0].startswith('GET /static/'):  # filter whatever you want
        return False
    return True

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        # use Django's built in CallbackFilter to point to your filter 
        'skip_static_requests': {
            '()': 'django.utils.log.CallbackFilter',
            'callback': skip_static_requests
        }
    },
    'formatters': {
        # django's default formatter
        'django.server': {
            '()': 'django.utils.log.ServerFormatter',
            'format': '[%(server_time)s] %(message)s',
        }
    },
    'handlers': {
        # django's default handler...
        'django.server': {
            'level': 'INFO',
            'filters': ['skip_static_requests'],  # <- ...with one change
            'class': 'logging.StreamHandler',
            'formatter': 'django.server',
        },
    },
    'loggers': {
        # django's default logger
        'django.server': {
            'handlers': ['django.server'],
            'level': 'INFO',
            'propagate': False,
        },
    }
}

自1.10起,所有这些都是Django的默认记录器,您可以使用相同的方法来覆盖它们: > https://github.com/django/django/blob /32265361279b3316f5bce8efa71f2049409461e3/django/utils/log.py#L18

Here are all of Django's default loggers as of 1.10, which you can override in the same way: https://github.com/django/django/blob/32265361279b3316f5bce8efa71f2049409461e3/django/utils/log.py#L18

以下是Django默认内置记录器的功能说明: https://docs.djangoproject.com/en/1.10/topics/logging/#id3

Here are descriptions of what Django's default built-in loggers do: https://docs.djangoproject.com/en/1.10/topics/logging/#id3

以下是Django CallbackFilter上的文档,用于挂钩自定义过滤器: https://docs.djangoproject.com/en/1.10/topics/logging/#django.utils.log.CallbackFilter

Here's are the docs on Django's CallbackFilter used above to hook in the custom filter: https://docs.djangoproject.com/en/1.10/topics/logging/#django.utils.log.CallbackFilter

这篇关于django-如何使用日志记录过滤掉GET静态消息和媒体消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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