If you try to output information that is not in the LogRecord attribute such as request user information to the Django log,
logger.info(message, extra={'username' : request.user.username})
You can find something like this, but it's a little different. There was a solution when I thought that there were development members who did not follow the rules and that it only worked for the logs I wrote.
-Use Middleware to set user information to threading.local () when requesting -Add user information of threading.local () to LogRecord with logging.filter --LOGGING settings in settings.py
Middleware --Get username from request. (If it's an API, you can pharse JWT to get user information ...) --Set the username obtained above to threading.local (). (For reference from the filter below) --Set None to clear after self.get_response (request)
python
import logging
import threading
local = threading.local()
class CustomAttrMiddleware:
    """
Middleware to get the custom item to output to log
    """
    def __init__(self, get_response):
        self.get_response = get_response
    def __call__(self, request):
        """
Get the username of the request when requesting from the client
        threading.local()Temporarily save to
        """
        if request.user:
            setattr(local, 'user', request.user.username)
        else:
            setattr(local, 'user', None)
        response = self.get_response(request)
        #Clear when responding
        setattr(local, 'user', None)
        return response
filter --Set the username set in threading.local () in Middleware to LogRecord. --Be sure to return True.
python
class CustomAttrFilter(logging.Filter):
    """
filter for outputting custom items to log
    """
    def filter(self, record):
        record.user = getattr(local, 'user', None)
        return True
settings.py --Add the Middleware created above. --Added user output format (user =% (user) s) to LOGGING formatters --Add the filter created above to the filters of LOGGING --Set the added filter to be used by LOGGING handlers
python
MIDDLEWARE = [
     ...
    'middleware.custom_logging.CustomAttrMiddleware',
]
LOGGING = {
     ...
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d user=%(user)s %(message)s'
        },
    },
    'filters' : {
        'custom': {
            '()': 'middleware.custom_logging.CustomAttrFilter'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
            'filters': ['custom']
        },
    },
     ...
}
python
class LoggingExceptListView(generics.ListAPIView):
    """
ListView to see exception output in the log
    """
    def list(self, request, *args, **kwargs):
        """
Just raise an Exception
        """
        raise Exception('For checking logs')
--Execute the above operation check views --The "user = CustomAttrUser" part of the log below
python
ERROR 2020-05-06 17:12:02,703 log 13516 10376 user=CustomAttrUser Internal Server Error: /sample_api/logging-exp/
Traceback (most recent call last):
  File "C:\ProgramData ...
 ...  raise Exception('For checking logs')
https://github.com/ping2shi2/DRF-sample
Recommended Posts