djangodjango-viewsdjango-middlewareasgidjango-logging

How to know which django middlewares are asynchronous enabled and which are not?


To see what middleware Django has to adapt, you can turn on debug logging for the django. request logger and look for log messages about “Synchronous middleware … adapted” .

I have been trying to do just the same but without any luck.

This is my settings.py file:

LOGGING = {  
 'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'DEBUG',
    },
    'loggers': {
        'django.request': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

Even though I have set up the LOGGING variable, I am not getting the output like mentioned in the documentation.

Starting server at tcp:port=8000:interface=127.0.0.1
HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Configuring endpoint tcp:port=8000:interface=127.0.0.1
Listening on TCP address 127.0.0.1:8000
HTTP b'GET' request for ['127.0.0.1', 42684]
HTTP 200 response started for ['127.0.0.1', 42684]
HTTP close for ['127.0.0.1', 42684]
HTTP response complete for ['127.0.0.1', 42684]
127.0.0.1:42684 - - [22/Mar/2022:12:11:47] "GET /admin/" 200 3550
HTTP b'GET' request for ['127.0.0.1', 42684]
HTTP 200 response started for ['127.0.0.1', 42684]
HTTP close for ['127.0.0.1', 42684]
HTTP response complete for ['127.0.0.1', 42684]
127.0.0.1:42684 - - [22/Mar/2022:12:11:48] "GET /admin/core/user/" 200 9028
HTTP b'GET' request for ['127.0.0.1', 42684]
HTTP 200 response started for ['127.0.0.1', 42684]
HTTP close for ['127.0.0.1', 42684]
HTTP response complete for ['127.0.0.1', 42684]
127.0.0.1:42684 - - [22/Mar/2022:12:11:48] "GET /admin/jsi18n/" 200 3343

when I run the daphne server using,

daphne project_name.asgi:application

command. Can anyone help me to get the output about what all middlewares are asynchronous and which are not.

I tried making a view and making a request to it through browser, but it didn't seem to print about any middlewares. Although, there is already the admin model which might be using the middlewares, there is no output of such kind like "synchronous middleware ...adapted".


Solution

  • The logger that I have used in the question is quite correct to view which middlewares are asynchronous enabled or not. But the problem was that I was wondering why Django provided middlewares are not showing here which are asynchronous enabled and which are not. So, I tried to make a middleware on my own to check this feature.

    middleware.py

    class SimpleMiddleware(MiddlewareMixin):
        def __init__(self, get_response):
            self.async_capable = False
            self.get_response = get_response
            # One-time configuration and initialization.
    
        def __call__(self, request):
            # Code to be executed for each request before
            # the view (and later middleware) are called.
    
            response = self.get_response(request)
    
            # Code to be executed for each request/response after
            # the view is called.
    
            return response
    

    Now, if you add this middleware in the settings.py file, and run the server, then you will see that the log will say about this middleware as intended.