This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description
.. 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="">`_
Release History

Release History

1.2.1

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.1.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django-context-logging-1.2.1.tar.gz (5.2 kB) Copy SHA256 Checksum SHA256 Source Jun 2, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting