How to log request uuid in django?

I found a way to log request uuid is that rename thread name in my middleware,but I’m not sure that if have problem in high qps.
Besides, it has any other method to log request uuid?

here is my middleware

class RequestIDMiddleware():
    # pass

    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        request.META['request_id'] = str(uuid.uuid4())
        logger.info(f"start request id: {request.META['request_id']}")
        response = self.get_response(request)
        if request.META.get('request_id') is None:
            response.headers['X-REQUEST-ID'] = request.META['request_id']
            logger.info(f"finish request id: {response.headers['X-REQUEST-ID']}")
        return response

structlog works well a concurrent envs and integrates with standard logging libraries while providing more structured and flexible options.

mode details at: Bound Loggers - structlog 24.1.0 documentation

your code will look like:

import structlog
import uuid

logger = structlog.get_logger()

class RequestIDMiddleware():
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        request_id = str(uuid.uuid4())
        request.META['request_id'] = request_id
        bound_logger = logger.bind(request_id=request_id)
        bound_logger.info("Start request")

        response = self.get_response(request)

        bound_logger.info("Finish request")
        response.headers['X-REQUEST-ID'] = request_id

        return response