Skip to content

django-user-trace

A Python logging filter for Django user attributes

Build Coverage Python PyPI


django-user-trace provides a Python logging filter that injects attributes from the currently logged in Django user.

INFO ... john_doe project.views This is a view log, and should have a user.
INFO ... mike_scott project.views This is a view log, and should have a user.
WARNING ... john_doe project.services.file Some warning in a function!

[//]: # (exclude 'Installation' section)
It uses a [`ContextVar`][python:contextvars] to store user attributes for use on
the current thread. These are then injected into all log records via a logging
filter.

_[Visit the documentation][docs]._

```mermaid
sequenceDiagram
    actor User
    User ->>+ django: make request
    django ->>+ django.contrib.auth: AuthenticationMiddleware
    django.contrib.auth ->>+ django: set `request.user`
    django ->>+ django_user_trace: django_user_trace_middleware
    note over django, django_user_trace: Capture relevant user attributes into a `ContextVar`
    django_user_trace ->>+ django: continue
    django --> logging: log message
    logging ->>+ django_user_trace: get user attrs
    django_user_trace -->> logging: `ContextVar` for user attrs
    django ->>+ django_user_trace: signal `request_finished`
    note over django, django_user_trace: Clear user attributes from the `ContextVar`
    django_user_trace ->>+ django: continue
    django -->> User: send response

Supports#

Resources#

Or, via poetry:

poetry add django-user-trace

Contributing#

Thank you for considering contributing to django-user-trace! Please see the Contribution Guidelines.

Security Vulnerabilities#

Please review the Security Policy on how to report security vulnerabilities.

Licence#

django-user-trace is open-sourced software licenced under the MIT licence.