django - 如何通过日志记录筛选出GET静态和媒体消息?

时间:2022-03-09 21:58:46

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:

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

...
[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.

寻找有意义的输出非常繁琐。

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:

我想在我的环境中过滤掉那些消息,所以我只看到了视图和输出的GET,但到目前为止看到我看到的日志记录,我可能会影响来自django的其他日志记录但不是这个。我甚至试过这个,但它不起作用:

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?

甚至可以过滤掉那种输出吗?

Thanks!!

谢谢!!

4 个解决方案

#1


8  

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

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

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

要过滤掉对/ static /目录的所有GET请求,请将以下内容添加到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,
        },
    }
}

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

以下是1.10版本中Django的所有默认记录器,您可以用相同的方式覆盖它们:https://github.com/django/django/blob/32265361279b3316f5bce8efa71f2049409461e3/django/utils/log.py#L18

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

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

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的CallbackFilter的文档,用于挂钩自定义过滤器:https://docs.djangoproject.com/en/1.10/topics/logging/#django.utils.log.CallbackFilter

#2


3  

As a workaround, you can use this snippet (from Django Snippets):

作为解决方法,您可以使用此代码段(来自Django Snippets):

from django.conf import settings
from django.core.servers import basehttp
from django.core.management.commands.runserver import Command as BaseCommand

class QuietWSGIRequestHandler(basehttp.WSGIRequestHandler):
    def log_message(self, format, *args):
        # Don't bother logging requests for paths under MEDIA_URL.
        if self.path.startswith(settings.MEDIA_URL):
            return
        # can't use super as base is old-style class, so call method explicitly
        return basehttp.WSGIRequestHandler.log_message(self, format, *args)

def run(addr, port, wsgi_handler):
    server_address = (addr, port)
    httpd = basehttp.WSGIServer(server_address, QuietWSGIRequestHandler)
    httpd.set_app(wsgi_handler)
    httpd.serve_forever()

class Command(BaseCommand):
    def handle(self, addrport='', *args, **options):
        # monkeypatch Django to use our quiet server
        basehttp.run = run
        return super(Command, self).handle(addrport, *args, **options)

You need to user this command to run the server. It basically only override the log behaviour to drop requests which begins by the MEDIA_URL setting. Put this whole file in an installed app and run it with ./manage.py runserver_quiet (if the command file is runserver_quiet.py)

您需要使用此命令来运行服务器。它基本上只覆盖日志行为以删除以MEDIA_URL设置开头的请求。将整个文件放在已安装的应用程序中并使用./manage.py runserver_quiet运行它(如果命令文件是runserver_quiet.py)

You can't play with the logging module, because the WSGIRequestHandler doesn't use it to display these messages. It is directly written in the stderr stream.

您无法使用日志记录模块,因为WSGIRequestHandler不会使用它来显示这些消息。它直接写在stderr流中。

#3


1  

From Django 1.10 you can configure django.server logging for "the handling of requests received by the server invoked by the runserver command". This is a quick way to filter out the media requests and to focus on your logger info.

从Django 1.10开始,您可以将django.server日志记录配置为“处理由runserver命令调用的服务器接收的请求”。这是过滤媒体请求并专注于记录器信息的快速方法。

Add to django logging settings:

添加到django日志记录设置:

loggers: {
....
    'django.server': {
        'handlers': ['console'],
        'level': 'ERROR'  # or INFO if you want to see the log
    },
}

Django Documentation ref: Logging: django-server

Django文档参考:记录:django-server

#4


0  

from django.core.servers.basehttp import WSGIRequestHandler

# Grab the original log_message method.
_log_message = WSGIRequestHandler.log_message

def log_message(self, *args):
    # Don't log if path starts with /static/
    if self.path.startswith("/static/"):
        return
    else:
        return _log_message(self, *args)

# Replace log_message with our custom one.
WSGIRequestHandler.log_message = log_message

# Import the original runserver management command
from django.core.management.commands.runserver import Command

Hat tip https://code.djangoproject.com/ticket/25704

帽子小贴士https://code.djangoproject.com/ticket/25704

#1


8  

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

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

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

要过滤掉对/ static /目录的所有GET请求,请将以下内容添加到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,
        },
    }
}

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

以下是1.10版本中Django的所有默认记录器,您可以用相同的方式覆盖它们:https://github.com/django/django/blob/32265361279b3316f5bce8efa71f2049409461e3/django/utils/log.py#L18

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

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

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的CallbackFilter的文档,用于挂钩自定义过滤器:https://docs.djangoproject.com/en/1.10/topics/logging/#django.utils.log.CallbackFilter

#2


3  

As a workaround, you can use this snippet (from Django Snippets):

作为解决方法,您可以使用此代码段(来自Django Snippets):

from django.conf import settings
from django.core.servers import basehttp
from django.core.management.commands.runserver import Command as BaseCommand

class QuietWSGIRequestHandler(basehttp.WSGIRequestHandler):
    def log_message(self, format, *args):
        # Don't bother logging requests for paths under MEDIA_URL.
        if self.path.startswith(settings.MEDIA_URL):
            return
        # can't use super as base is old-style class, so call method explicitly
        return basehttp.WSGIRequestHandler.log_message(self, format, *args)

def run(addr, port, wsgi_handler):
    server_address = (addr, port)
    httpd = basehttp.WSGIServer(server_address, QuietWSGIRequestHandler)
    httpd.set_app(wsgi_handler)
    httpd.serve_forever()

class Command(BaseCommand):
    def handle(self, addrport='', *args, **options):
        # monkeypatch Django to use our quiet server
        basehttp.run = run
        return super(Command, self).handle(addrport, *args, **options)

You need to user this command to run the server. It basically only override the log behaviour to drop requests which begins by the MEDIA_URL setting. Put this whole file in an installed app and run it with ./manage.py runserver_quiet (if the command file is runserver_quiet.py)

您需要使用此命令来运行服务器。它基本上只覆盖日志行为以删除以MEDIA_URL设置开头的请求。将整个文件放在已安装的应用程序中并使用./manage.py runserver_quiet运行它(如果命令文件是runserver_quiet.py)

You can't play with the logging module, because the WSGIRequestHandler doesn't use it to display these messages. It is directly written in the stderr stream.

您无法使用日志记录模块,因为WSGIRequestHandler不会使用它来显示这些消息。它直接写在stderr流中。

#3


1  

From Django 1.10 you can configure django.server logging for "the handling of requests received by the server invoked by the runserver command". This is a quick way to filter out the media requests and to focus on your logger info.

从Django 1.10开始,您可以将django.server日志记录配置为“处理由runserver命令调用的服务器接收的请求”。这是过滤媒体请求并专注于记录器信息的快速方法。

Add to django logging settings:

添加到django日志记录设置:

loggers: {
....
    'django.server': {
        'handlers': ['console'],
        'level': 'ERROR'  # or INFO if you want to see the log
    },
}

Django Documentation ref: Logging: django-server

Django文档参考:记录:django-server

#4


0  

from django.core.servers.basehttp import WSGIRequestHandler

# Grab the original log_message method.
_log_message = WSGIRequestHandler.log_message

def log_message(self, *args):
    # Don't log if path starts with /static/
    if self.path.startswith("/static/"):
        return
    else:
        return _log_message(self, *args)

# Replace log_message with our custom one.
WSGIRequestHandler.log_message = log_message

# Import the original runserver management command
from django.core.management.commands.runserver import Command

Hat tip https://code.djangoproject.com/ticket/25704

帽子小贴士https://code.djangoproject.com/ticket/25704