Skip to content

Configuration#

You can provide settings for django-user-trace by adding DJANGO_USER_TRACE as a dictionary to your Django settings.

The default settings are as follows:

settings.py
DJANGO_USER_TRACE = {
    "USER_ATTRS": {"username": "get_username"},
    "CELERY_TASK_HEADER": "User",
}

USER_ATTRS#

Default: {"username": "get_username"}
Type: dict[str, str | Callable[[AbstractBaseUser | AnonymousUser, HttpRequest], Any]]

This option controls which request.user attributes are made available to the log records.

It is a mapping of log record field names to either:

  1. an attribute on the Django request.user object;
  2. a callable that accepts (AbstractBaseUser | AnonymousUser, HttpRequest) and returns the result;
  3. an import string (prefixed with ext://) to a callable as seen in (2) above.

Tip

To lookup nested attributes, separate them by __ (two underscores), e.g. profile__country__code.

settings.py
def get_custom_attribute(user, request):
    return f"Mr. {user.get_short_name()}"

DJANGO_USER_TRACE = {
    # ...
    "USER_ATTRS": {
        "email": "email",#(1)!
        "username": "get_username",#(2)!
        "custom": get_custom_attribute,#(3)!
        "custom_2": "ext://settings.get_custom_attribute",#(4)!
    },
}
  1. Here, we are configuring the USER_ATTRS option of django-user-trace to map request.user.email to the username field on a log record.
  2. Here, we are mapping the result of invoking the get_username() method on the request.user instance, to the username field on a log record.
  3. Here, we are mapping the result of invoking the get_custom_attribute callable (defined above) to the custom field on a log record.
  4. Here is another way of invoking the same get_custom_attribute callable (defined above) by first importing the callable from settings.get_custom_attribute.

CELERY_TASK_HEADER#

Default: "User"
Type: str

This option determines the name of the Celery task header used to trace user attributes when django_user_trace.contrib.celery integration is installed.


Last update: 2023-07-02
Created: 2023-02-11