Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Django LiveServerTestCase log requests

Tags:

django

Django has a feature to log each http request. Eg.:

INFO 2017-08-31 11:52:40,725 arnaud basehttp "GET /design/projects/ HTTP/1.1" 200 3852

Unfortunately, it seems to be disabled when running a LiveServerTestCase. In order to ease the debugging of my end-to-end tests, I would like this feature to remain activated.

like image 781
Arnaud P Avatar asked Sep 21 '25 10:09

Arnaud P


1 Answers

As it turns out, this behavior doesn't seem configurable. I couldn't find any doc on the subject, and resorted to dive inside Django.

Solution

(disclaimer: this is unofficial, therefore brittle)

Override the server_thread_class attribute of your LiveServerTestCase

from django.core.servers.basehttp import ThreadedWSGIServer, WSGIRequestHandler
from django.test.testcases import LiveServerTestCase, LiveServerThread

class End2End(LiveServerTestCase):

    class VersboseLiveServerThread(LiveServerThread):
        def _create_server(self):
            return ThreadedWSGIServer((self.host, self.port), WSGIRequestHandler, allow_reuse_address=False)

    server_thread_class = VersboseLiveServerThread

A bit more details

This solution and the feeling that there is no way to configure that behavior properly stem from observing how LiveServerTestCase is built (Django 1.11):

class LiveServerTestCase(TransactionTestCase):
    # [...]
    server_thread_class = LiveServerThread

class LiveServerThread(threading.Thread):
    # [...]
    def _create_server(self):
        # an extension point returning the WSGIRequestHandler would have been nice :)
        return ThreadedWSGIServer((self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False)

class QuietWSGIRequestHandler(WSGIRequestHandler):
    def log_message(*args):
        pass
like image 150
Arnaud P Avatar answered Sep 23 '25 10:09

Arnaud P