Skip to main content
.. image:: https://travis-ci.org/lorehov/django_context_logging.svg
:target: https://travis-ci.org/lorehov/django_context_logging
:alt: Build Status

########################
Django Context Logging
########################

``django_context_logging`` is a library for enriching logs records with context
fields. Typical usage is for adding some request_id to all logs in order to
make troubleshooting more comfortable. This context is shared by all app using
``logging``, transparently.


=======
Usage
=======

You have two option to send the context to the log system: inject context as
extra fields in records or append context to the each message.

Using filter
============

This method allow to pass fields to JSON formatters, log servers, etc. or use
the extra fields in format string.

.. code-block::

LOGGING = {
'version': 1,
'formatters': {
'extra': {
'format': '%(levelname)s %(rid)s %(name)s %(message)s',
},
},
'filters': {
'context_filter': {
'()': 'django_context_logging.AddContextFilter',
'default': {'rid': None},
}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'filters': ['context_filter'],
},
},
'root': {
'handlers': ['console'],
'level': 'DEBUG',
},
}

Note three things:

* ``%(rid)`` in format string is for logging rid (for request_id) from our
context;
* ``django_context_logging.AddContextFilter`` - filter which converts keys from
context dict to attributes of LogRecord;
* ``'default': {'rid': None}`` - some of our log events could be without
context for example logs emitted on worker start. All these logs will not be
recorded due to the lack of 'rid' attribute (in our example) on LogRecord
instance. To fix this we provide default value for 'rid'.


Using formatter
===============

If you do not want to bother with custom log format and default context values
for a filter - you can use ``django_context_logging.AddContextFormatter``.

.. code-block::

LOGGING = {
'version': 1,
'formatters': {
'append': {
'()': 'django_context_logging.AddContextFormatter'
'format': '%(levelname)s %(name)s %(message)s'
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'append',
},
},
'root': {
'handlers': ['console'],
'level': 'DEBUG',
},
}

As you can see in this case we doesn't add any context related fields to a log
format string. This is because ``django_context_logging.AddContextFormatter``
will append all context information to every log.


Feeding the context
===================

The context class is just a thread local dict. It is used as local registry to
inject shared fields in log records. Here is a full example:

.. code-block::

from django_context_logging.log import context as log_context


log_context.push(userId=user.pk)
# code, log, etc.
for article in blog.articles:
with log_context(articleId=article.pk):
# code, log, ...
# code, log, etc.
log_context.pop('userId')


Automatic feeding with middleware
---------------------------------

A middleware is provided to inject extra fields in context. It will also clear
the context after each requests.

.. code-block::

MIDDLEWARE_CLASSES = [
'django_context_logging.ExtractRequestContextMiddleware',
# rest middlewares
]

DJANGO_CONTEXT_LOGGING_EXTRACTOR = lambda request: {'rid': request.GET.getlist('rid')}


Here DJANGO_CONTEXT_LOGGING_EXTRACTOR is a callable which takes
django.http.request.HttpRequest and returns dictionary with extracted context.

**Note:** ExtractRequestContextMiddleware will fail with exception if no
DJANGO_CONTEXT_LOGGING_EXTRACTOR specified.

Contributors
------------

* Lev Orekhov `@lorehov <https://github.com/lorehov>`_
* Étienne BERSAC `@bersace <https://github.com/bersace>`_

Metadata

Release files for django-context-logging 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-context-logging 1.2.1
File Size Uploaded Provenance Actions
django-context-logging-1.2.1.tar.gz 5.2 kB Without provenance Details for django-context-logging-1.2.1.tar.gz

Release files / django-context-logging-1.2.1.tar.gz

Download URL django-context-logging-1.2.1.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
322121a53eff22acecf1a754c0aba40a905b272000fad3be177775cad3b2a263
BLAKE2b-256 checksum
How to use checksums
45d1b23d9b117758a93d3c56e1839233d9a77b47c6380454b004909d5912b719
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-context-logging 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page