[Django]-Django logging custom attributes in formatter

33👍

✅

You can use a filter to add your custom attribute. For example :

def add_my_custom_attribute(record):
    record.myAttribute = 'myValue'
    record.username = record.request.user.username 
    return True

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        ...
        'add_my_custom_attribute': {
            '()': 'django.utils.log.CallbackFilter',
            'callback': add_my_custom_attribute,
        }
    },
    'handlers': {
        ...
        'django.server': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'filters': ['add_my_custom_attribute'],
            'formatter': 'django.server',
        },            
    },
    ...
}

By installing a filter, you can process each log record and decide whether it should be passed from logger to handler.

The filter get the log record which contains all the details of log (i.e : time, severity, request, status code).

The attributes of the record are used by the formatter to format it to string message. If you add your custom attributes to that record – they will also be available to the formatter.

4👍

The extra keyword is not a workaround. That’s the most eloquent way of writing customized formatters, unless you are writing a custom logging altogether.

format: '%(asctime).19s %(levelname)s - %(username)s: %(message)s'
logging.basicConfig(format=format)
logger.info(message, extra={'username' : request.user.username})

Some note from the documentation (**kwars for Django logger):

The keys in the dictionary passed in extra should not clash with the keys used by the logging system.

If the strings expected by the Formatter are missing, the message will not be logged.

This feature is intended for use in specialized circumstances, and not always.

2👍

I will provide one of many possible complete answers to this question:

How can Django use logging to log using custom attributes in the formatter? I’m thinking of logging the logged in username for example.

Other answers touched on the way to add extra contextual info through python’s logging utilities. The method of using filters to attach additional information to the log record is ideal, and best described in the docs:

https://docs.python.org/3/howto/logging-cookbook.html#using-filters-to-impart-contextual-information

This still does not tell us how to get the user from the request in a universal way. The following library does this:

https://github.com/ninemoreminutes/django-crum

Thus, combine the two, and you will have a complete answer to the question that has been asked.

import logging
from crum import get_current_user

class ContextFilter(logging.Filter):
    """
    This is a filter injects the Django user
    """

    def filter(self, record):

        record.user = get_current_user()
        return True

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG,
                        format='User: %(user)-8s %(message)s')
    a1 = logging.getLogger('a.b.c')

    f = ContextFilter()
    a1.addFilter(f)
    a1.debug('A debug message')

This will need to happen within a Django request-response cycle with the CRUM library properly installed.

0👍

I came accross this old question while looking for a solution to display username in Django request logs.

Response of napuzba helped a lot, but it didn’t work right away, because the user doesn’t always exist in the request object.
It resulted in this piece of code in Django settings.py:

def add_username(record):
    """
    Adds request user username to the log
    :param record:
    :return:
    """
    try:
        record.username = record.request.user.username
    except AttributeError:
        record.username = ""
    return True
 

LOGGING = {
    ...
    "formatters": {
        "verbose": {
            "format": "[%(asctime)s] %(levelname)s [%(name)s:%(module)s] [%(username)s] %(message)s",
            "datefmt": "%d.%m.%Y %H:%M:%S",
        },
    },
    "filters": {
        "add_username": {
            "()": "django.utils.log.CallbackFilter",
            "callback": add_username,
        }
    },
    "handlers": {        
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
            "filters": ["add_username"],
        },
    }
}

Leave a comment