我正在处理一个包含大量图像的页面,因此这会在此类型的控制台中生成大量输出。在我的开发环境中,我使用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
...
寻找有意义的输出非常繁琐。
我想在我的环境中过滤掉那些消息,所以我只看到了视图和输出的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,
},
}
}
甚至可以过滤掉那种输出吗?
谢谢!
答案 0 :(得分:13)
Django的最新版本可以很容易地使用您自己的LOGGING
设置覆盖默认日志记录。
要过滤掉/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,
},
}
}
以下是自1.10开始的所有Django默认记录器,您可以使用相同的方式覆盖它们: https://github.com/django/django/blob/32265361279b3316f5bce8efa71f2049409461e3/django/utils/log.py#L18
以下是Django默认内置记录器的功能描述: 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
答案 1 :(得分:3)
作为解决方法,您可以使用this snippet (from 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)
您需要使用此命令来运行服务器。它基本上只覆盖日志行为以删除以MEDIA_URL
设置开头的请求。将整个文件放在已安装的应用程序中并使用./manage.py runserver_quiet
运行(如果命令文件为runserver_quiet.py
)
您无法使用logging
模块,因为WSGIRequestHandler
并未使用它来显示这些消息。 It is directly written in the stderr
stream
答案 2 :(得分:1)
从Django 1.10开始,您可以将django.server日志记录配置为“处理由runserver命令调用的服务器接收的请求”。这是过滤媒体请求并专注于记录器信息的快速方法。
添加到django日志记录设置:
loggers: {
....
'django.server': {
'handlers': ['console'],
'level': 'ERROR' # or INFO if you want to see the log
},
}
Django文档参考:Logging: django-server
答案 3 :(得分: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