Output user information etc. to Django log

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.

Overview

-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']
        },
    },
     ...
}

views (for operation check)

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')

log output result

--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')

Source

https://github.com/ping2shi2/DRF-sample

References

Recommended Posts

Output user information etc. to Django log
Pass login user information to view in Django
How to update user information when logging in to Django RemoteUserMiddleware
[Django] Command to output QuerySet to csv
Output Python log to console with GAE
Setting to output the log of cron execution
[Django memo] I want to set the login user information in the form in advance
Output python log to both console and file
Add functionality to modify Django day shift information
[CentOS8] How to output Python standard output to systemd log
Apache settings, log confirmation, etc. (* Updated from time to time)
Output log to console with Flask + Nginx on Docker
Implement the Django user extension and register the attached information
[Python-pptx] Output PowerPoint font information to csv with python
Allows you to select by name from Django User Name
Output PDF with Django
Investigate django user management
Markdown output with Django
unable to import django
update django version 1.11.1 to 2.2
[Django] Pass the user instance authenticated by API to ModelSerializer
[Django] I want to log in automatically after new registration
Output product information to csv using Rakuten product search API [Python]
How to output additional information when logging with python's logging module
I tried to output the access log to the server using Node.js