Madhusudhan
Madhusudhan

Reputation: 8563

django - disable staticfiles logging

noob question:

I want to disable logging on static assets on the console, I only want to see normal http requests (but not the ones for static files).

I always get this on the console for all the static file loading

[24/Sep/2014 22:18:55] "GET / HTTP/1.1" 200 39816
[24/Sep/2014 22:18:55] "GET /static/2.5.11/socialschools/css/molengo/molengo-regular-webfont.css HTTP/1.1" 200 509
[24/Sep/2014 22:18:55] "GET /static/2.5.11/socialschools/js/less.1.7.0.min.js HTTP/1.1" 200 101854
[24/Sep/2014 22:18:55] "GET /static/2.5.11/socialschools/js/html5shiv.3.7.0.js HTTP/1.1" 200 2428
[24/Sep/2014 22:18:55] "GET /static/2.5.11/socialschools/js/jquery.1.9.1.min.js HTTP/1.1" 200 92629
[24/Sep/2014 22:18:55] "GET /static/2.5.11/socialschools/js/respond.1.4.2.min.js HTTP/1.1" 200 4377
[24/Sep/2014 22:18:55] "GET /static...

Is there any way to disable this?

For example in express (nodejs), if I put the logging middleware before the static middleware, it doesn't log the static files. Any help appreciated, thanks!

Upvotes: 6

Views: 2421

Answers (2)

Alex Bender
Alex Bender

Reputation: 906

I see that this question is pretty old (4 years and 8 months) I'd like to add possible solutions for that task. Since logging on static assets on the console is governed by the logging module, the answer is in logging configurations. There is a concept of filter in python logging, which allows record to be passed further by loggers chain, or be suppressed simply by returning Boolean True or False.

I've got this problem a few times, so I'd like to put a solution here:

  1. Let's write a Filter in somemodule.
from logging import Filter

class SkipStaticFilter(Filter):
    """Logging filter to skip logging of staticfiles"""
    def filter(self, record):
        return not record.getMessage().startswith('"GET /static/')

Edit: For Django 2.2: replace GET /static/ with HTTP GET /static/

  1. Now let's add this filter into the settings:

LOGGING = {
    # Definition of filters
    'filters': {    
        'hide_staticfiles': {    
            '()': 'somemodule.SkipStaticFilter'    
        }
    },
    'version': 1,    
    'disable_existing_loggers': False,    
    'handlers': {    
        'console': {    
            'class': 'logging.StreamHandler',

            # Usage of that filter
            'filters': ['hide_staticfiles']    
        },    
    },    
    'loggers': {    
        'django': {
            # Usage of the handler with our filter  
            'handlers': ['console'],   
        }    
    },    

}   
 

Now you will not see any log record which starts with "GET /static/ You can add more filters, with sophisticated rules to decide if you want to see that record or not (Do not hide the static requests if they are 404, for example)

Hope that would help someone to get rid of logging pollution.

Upvotes: 3

Brandon Taylor
Brandon Taylor

Reputation: 34573

Unfortunately, the runserver command doesn't respect the --verbosity option that other management commands do.

For more information on the --verbosity option, check out: https://docs.djangoproject.com/en/1.7/ref/django-admin/#displaying-debug-output

The core developers have marked the bug as "wontfix": https://code.djangoproject.com/ticket/15132

Upvotes: 0

Related Questions