Package: django-context-logging
.. 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 contextfields.  Typical usage is for adding some request_id to all logs in order tomake 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 asextra 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 usethe 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 ourcontext;* ``django_context_logging.AddContextFilter`` - filter which converts keys fromcontext dict to attributes of LogRecord;* ``'default': {'rid': None}`` - some of our log events could be withoutcontext for example logs emitted on worker start. All these logs will not berecorded due to the lack of 'rid' attribute (in our example) on LogRecordinstance. 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 valuesfor 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 logformat 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 toinject shared fields in log records. Here is a full example:.. code-block::from django_context_logging.log import context as log_contextlog_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 clearthe 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 takesdjango.http.request.HttpRequest and returns dictionary with extracted context.**Note:** ExtractRequestContextMiddleware will fail with exception if noDJANGO_CONTEXT_LOGGING_EXTRACTOR specified.Contributors------------* Lev Orekhov `@lorehov <https://github.com/lorehov>`_* Étienne BERSAC `@bersace <https://github.com/bersace>`_