<div align="center">
# `django-user-trace`
A Python [logging][python:logging] filter for [Django][django] user attributes
[![Build](https://img.shields.io/github/actions/workflow/status/axieum/django-user-trace/release.yml?branch=main&style=flat-square)][ci:release]
[![Coverage](https://img.shields.io/codecov/c/github/axieum/django-user-trace?style=flat-square)][codecov]
[![Python](https://img.shields.io/pypi/pyversions/django-user-trace?style=flat-square)][python]
[![PyPI](https://img.shields.io/pypi/v/django-user-trace?style=flat-square&include_prereleases&sort=semver)][pypi]
</div>
`django-user-trace` provides a Python [logging][python:logging] filter that
injects attributes from the currently logged in [Django][django] user.
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
* [Celery task logging][celery]
#### Resources
* [Django — How to configure and use logging][django:logging]
#### Related Projects
* [madzak/python-json-logger][python-json-logger]
* [snok/django-guid][django-guid]
## Installation
Install via `pip`:
```shell
pip install django-user-trace
```
Or, via [`poetry`][poetry]:
```shell
poetry add django-user-trace
```
## Contributing
Thank you for considering contributing to `django-user-trace`! Please see the
[Contribution Guidelines][contributing].
## Security Vulnerabilities
Please review the [Security Policy][security] on how to report security
vulnerabilities.
## Licence
`django-user-trace` is open-sourced software licenced under the
[MIT licence][licence].
[celery]: https://docs.celeryq.dev/
[ci:release]: https://github.com/axieum/django-user-trace/actions/workflows/release.yml
[ci:test]: https://github.com/axieum/django-user-trace/actions/workflows/test.yml
[codecov]: https://app.codecov.io/gh/axieum/django-user-trace
[contributing]: CONTRIBUTING.md
[django]: https://djangoproject.com/
[django:logging]: https://docs.djangoproject.com/en/stable/howto/logging/
[django-guid]: https://github.com/snok/django-guid
[docs]: https://axieum.github.io/django-user-trace
[licence]: https://opensource.org/licenses/MIT
[poetry]: https://python-poetry.org/
[pypi]: https://pypi.org/project/django-user-trace
[python]: https://python.org/
[python:contextvars]: https://docs.python.org/3/library/contextvars.html
[python:logging]: https://docs.python.org/3/library/logging.html
[python-json-logger]: https://github.com/madzak/python-json-logger
[security]: SECURITY.md
Raw data
{
"_id": null,
"home_page": "https://github.com/axieum/django-user-trace",
"name": "django-user-trace",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10,<4.0",
"maintainer_email": "",
"keywords": "async,django,logs,logging,middleware,sync,trace,tracing,user,web",
"author": "Jonathan Hiles",
"author_email": "jonathan@hil.es",
"download_url": "https://files.pythonhosted.org/packages/c2/6e/872047030ac4a22172ce6135c13c068f46b73376d0e936f033753039de4d/django_user_trace-2.0.0.tar.gz",
"platform": null,
"description": "<div align=\"center\">\n\n# `django-user-trace`\n\nA Python [logging][python:logging] filter for [Django][django] user attributes\n\n[![Build](https://img.shields.io/github/actions/workflow/status/axieum/django-user-trace/release.yml?branch=main&style=flat-square)][ci:release]\n[![Coverage](https://img.shields.io/codecov/c/github/axieum/django-user-trace?style=flat-square)][codecov]\n[![Python](https://img.shields.io/pypi/pyversions/django-user-trace?style=flat-square)][python]\n[![PyPI](https://img.shields.io/pypi/v/django-user-trace?style=flat-square&include_prereleases&sort=semver)][pypi]\n\n</div>\n\n`django-user-trace` provides a Python [logging][python:logging] filter that\ninjects attributes from the currently logged in [Django][django] user.\n\nIt uses a [`ContextVar`][python:contextvars] to store user attributes for use on\nthe current thread. These are then injected into all log records via a logging\nfilter.\n\n_[Visit the documentation][docs]._\n\n```mermaid\nsequenceDiagram\n actor User\n User ->>+ django: make request\n django ->>+ django.contrib.auth: AuthenticationMiddleware\n django.contrib.auth ->>+ django: set `request.user`\n django ->>+ django_user_trace: django_user_trace_middleware\n note over django, django_user_trace: Capture relevant user attributes into a `ContextVar`\n django_user_trace ->>+ django: continue\n django --> logging: log message\n logging ->>+ django_user_trace: get user attrs\n django_user_trace -->> logging: `ContextVar` for user attrs\n django ->>+ django_user_trace: signal `request_finished`\n note over django, django_user_trace: Clear user attributes from the `ContextVar`\n django_user_trace ->>+ django: continue\n django -->> User: send response\n```\n\n#### Supports\n\n* [Celery task logging][celery]\n\n#### Resources\n\n* [Django — How to configure and use logging][django:logging]\n\n#### Related Projects\n\n* [madzak/python-json-logger][python-json-logger]\n* [snok/django-guid][django-guid]\n\n## Installation\n\nInstall via `pip`:\n\n```shell\npip install django-user-trace\n```\n\nOr, via [`poetry`][poetry]:\n\n```shell\npoetry add django-user-trace\n```\n\n## Contributing\n\nThank you for considering contributing to `django-user-trace`! Please see the\n[Contribution Guidelines][contributing].\n\n## Security Vulnerabilities\n\nPlease review the [Security Policy][security] on how to report security\nvulnerabilities.\n\n## Licence\n\n`django-user-trace` is open-sourced software licenced under the\n[MIT licence][licence].\n\n[celery]: https://docs.celeryq.dev/\n[ci:release]: https://github.com/axieum/django-user-trace/actions/workflows/release.yml\n[ci:test]: https://github.com/axieum/django-user-trace/actions/workflows/test.yml\n[codecov]: https://app.codecov.io/gh/axieum/django-user-trace\n[contributing]: CONTRIBUTING.md\n[django]: https://djangoproject.com/\n[django:logging]: https://docs.djangoproject.com/en/stable/howto/logging/\n[django-guid]: https://github.com/snok/django-guid\n[docs]: https://axieum.github.io/django-user-trace\n[licence]: https://opensource.org/licenses/MIT\n[poetry]: https://python-poetry.org/\n[pypi]: https://pypi.org/project/django-user-trace\n[python]: https://python.org/\n[python:contextvars]: https://docs.python.org/3/library/contextvars.html\n[python:logging]: https://docs.python.org/3/library/logging.html\n[python-json-logger]: https://github.com/madzak/python-json-logger\n[security]: SECURITY.md\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Python logging filter for Django user attributes.",
"version": "2.0.0",
"project_urls": {
"Documentation": "https://axieum.github.io/django-user-trace/latest/",
"Funding": "https://github.com/sponsors/axieum",
"Homepage": "https://github.com/axieum/django-user-trace",
"Issue tracker": "https://github.com/axieum/django-user-trace/issues",
"Release notes": "https://axieum.github.io/django-user-trace/latest/changelog/",
"Repository": "https://github.com/axieum/django-user-trace"
},
"split_keywords": [
"async",
"django",
"logs",
"logging",
"middleware",
"sync",
"trace",
"tracing",
"user",
"web"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "71f7a4dd00023cf9b1979c8832bbb153d015cd8016d9e04337688046b2b13ada",
"md5": "b262abf674ecd79d12020edeba7ba1c0",
"sha256": "844663cde15f2bdfc92b8af198ddee384a59d2d23787dec64dc08110d184b78b"
},
"downloads": -1,
"filename": "django_user_trace-2.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b262abf674ecd79d12020edeba7ba1c0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10,<4.0",
"size": 10052,
"upload_time": "2023-12-07T10:32:56",
"upload_time_iso_8601": "2023-12-07T10:32:56.019762Z",
"url": "https://files.pythonhosted.org/packages/71/f7/a4dd00023cf9b1979c8832bbb153d015cd8016d9e04337688046b2b13ada/django_user_trace-2.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c26e872047030ac4a22172ce6135c13c068f46b73376d0e936f033753039de4d",
"md5": "879d9bd552f6d69e2b09ec7bd8365de9",
"sha256": "750baad616d80e42003d5d2037b612c28c1f0b99233ea46d36b29ac68b395414"
},
"downloads": -1,
"filename": "django_user_trace-2.0.0.tar.gz",
"has_sig": false,
"md5_digest": "879d9bd552f6d69e2b09ec7bd8365de9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10,<4.0",
"size": 13924,
"upload_time": "2023-12-07T10:32:57",
"upload_time_iso_8601": "2023-12-07T10:32:57.004343Z",
"url": "https://files.pythonhosted.org/packages/c2/6e/872047030ac4a22172ce6135c13c068f46b73376d0e936f033753039de4d/django_user_trace-2.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-07 10:32:57",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "axieum",
"github_project": "django-user-trace",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "django-user-trace"
}