G.A.
G.A.

Reputation: 1523

Django: Shut off console output of http response messages

So we made an app with django and it prints all these http response messages on the console everytime it gets a request.

[Date String] 'GET /urlpath/..blah blah ' 200 216
[Date String] 'DELETE /anotherurl/..blah blah ' 200 205 
...
..

We have disabled all logger outputs. Set Debug=False. Tried 'python manage.py runserver --verbosity 0'. Even tried changing sys.stdout to a NullDevice() class. Yet, we can't seem to turn off these response messages which are slowing the server to a crawl. All other messages get turned off, except these. Any suggestions?

I realize that the django internal webserver is meant only for development and not for production, but we would like to get a fair amount of speed just with the developmental version itself (without having to go into the intricacies of deploying django behind Apache/lighttpd).

Upvotes: 5

Views: 4382

Answers (3)

naib khan
naib khan

Reputation: 1128

All you need just to Add in Django logging settings:

    'loggers': {
        # django's default logger
        'django.server': {
            'handlers': ['django.server'],
            'level': 'INFO',
            'propagate': False,
        },
    }

you can override Django's default loggers: https://github.com/django/django/blob/32265361279b3316f5bce8efa71f2049409461e3/django/utils/log.py#L18

How Django's default built-in loggers works: https://docs.djangoproject.com/en/1.10/topics/logging/#id3

using custom CallbackFilter filter: https://docs.djangoproject.com/en/1.10/topics/logging/#django.utils.log.CallbackFilter

Upvotes: 2

Hartmut Pfarr
Hartmut Pfarr

Reputation: 6139

You can let console logging turned on, but with this small patch You can suppress the noisy lines like

GET /urlpath/..blah blah ' 200 216
...

completely, which all have HTTP result code = 200. In Your Site-packages or VirtualEnv (if You're using it) directory, go to folder structure django / core / servers / basehttp.py

then goto Class WSGIRequestHandler, function def log_message(self, format, *args)

After

# Don't bother logging requests for admin images or the favicon.
if (self.path.startswith(self.admin_media_prefix)
        or self.path == '/favicon.ico'):
    return

Insert

# START ----- Don't show page requests which have run successfully
if args[1] == '200':
    return
# END ----- Don't show page requests which have run successfully

This is a dirty work-around for Python 2.7.3, Django 1.4.3, I am interested in a clean customization, also :-)

Upvotes: 3

Zach Kelling
Zach Kelling

Reputation: 53859

The built-in development server was not designed for performance, instead use gunicorn. You can add it as an app to your Django project and it will make a command run_gunicorn available to you, as an alternative to runserver. It's a fair bit faster and more responsive than the built-in development server. If you want you can also set gunicorn's logging level with --log-level. It's also fairly simple to deploy, and suitable for production.

Upvotes: 2

Related Questions