{ "info": { "author": "Lev Orekhov", "author_email": "lev.orekhov@gmail.com", "bugtrack_url": null, "classifiers": [ "Intended Audience :: Developers", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3", "Topic :: Software Development :: Libraries", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": ".. image:: https://travis-ci.org/lorehov/django_context_logging.svg\n :target: https://travis-ci.org/lorehov/django_context_logging\n :alt: Build Status\n\n########################\n Django Context Logging\n########################\n\n``django_context_logging`` is a library for enriching logs records with context\nfields. Typical usage is for adding some request_id to all logs in order to\nmake troubleshooting more comfortable. This context is shared by all app using\n``logging``, transparently.\n\n\n=======\n Usage\n=======\n\nYou have two option to send the context to the log system: inject context as\nextra fields in records or append context to the each message.\n\nUsing filter\n============\n\nThis method allow to pass fields to JSON formatters, log servers, etc. or use\nthe extra fields in format string.\n\n.. code-block::\n\n LOGGING = {\n 'version': 1,\n 'formatters': {\n 'extra': {\n 'format': '%(levelname)s %(rid)s %(name)s %(message)s',\n },\n },\n 'filters': {\n 'context_filter': {\n '()': 'django_context_logging.AddContextFilter',\n 'default': {'rid': None},\n }\n },\n 'handlers': {\n 'console': {\n 'class': 'logging.StreamHandler',\n 'filters': ['context_filter'],\n },\n },\n 'root': {\n 'handlers': ['console'],\n 'level': 'DEBUG',\n },\n }\n\nNote three things:\n\n* ``%(rid)`` in format string is for logging rid (for request_id) from our\n context;\n* ``django_context_logging.AddContextFilter`` - filter which converts keys from\n context dict to attributes of LogRecord;\n* ``'default': {'rid': None}`` - some of our log events could be without\n context for example logs emitted on worker start. All these logs will not be\n recorded due to the lack of 'rid' attribute (in our example) on LogRecord\n instance. To fix this we provide default value for 'rid'.\n\n\nUsing formatter\n===============\n\nIf you do not want to bother with custom log format and default context values\nfor a filter - you can use ``django_context_logging.AddContextFormatter``.\n\n.. code-block::\n\n LOGGING = {\n 'version': 1,\n 'formatters': {\n 'append': {\n '()': 'django_context_logging.AddContextFormatter'\n 'format': '%(levelname)s %(name)s %(message)s'\n },\n },\n 'handlers': {\n 'console': {\n 'class': 'logging.StreamHandler',\n 'formatter': 'append',\n },\n },\n 'root': {\n 'handlers': ['console'],\n 'level': 'DEBUG',\n },\n }\n\nAs you can see in this case we doesn't add any context related fields to a log\nformat string. This is because ``django_context_logging.AddContextFormatter``\nwill append all context information to every log.\n\n\nFeeding the context\n===================\n\nThe context class is just a thread local dict. It is used as local registry to\ninject shared fields in log records. Here is a full example:\n\n.. code-block::\n\n from django_context_logging.log import context as log_context\n\n\n log_context.push(userId=user.pk)\n # code, log, etc.\n for article in blog.articles:\n with log_context(articleId=article.pk):\n # code, log, ...\n # code, log, etc.\n log_context.pop('userId')\n\n\nAutomatic feeding with middleware\n---------------------------------\n\nA middleware is provided to inject extra fields in context. It will also clear\nthe context after each requests.\n\n.. code-block::\n\n MIDDLEWARE_CLASSES = [\n 'django_context_logging.ExtractRequestContextMiddleware',\n # rest middlewares\n ]\n\n DJANGO_CONTEXT_LOGGING_EXTRACTOR = lambda request: {'rid': request.GET.getlist('rid')}\n\n\nHere DJANGO_CONTEXT_LOGGING_EXTRACTOR is a callable which takes\ndjango.http.request.HttpRequest and returns dictionary with extracted context.\n\n**Note:** ExtractRequestContextMiddleware will fail with exception if no\n DJANGO_CONTEXT_LOGGING_EXTRACTOR specified.\n\n Contributors\n ------------\n\n * Lev Orekhov `@lorehov `_\n * \u00c9tienne BERSAC `@bersace `_", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/lorehov/django_context_logging", "keywords": "django context logging", "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "django-context-logging", "package_url": "https://pypi.org/project/django-context-logging/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/django-context-logging/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/lorehov/django_context_logging" }, "release_url": "https://pypi.org/project/django-context-logging/1.2.1/", "requires_dist": null, "requires_python": null, "summary": "Library for adding request context to log records", "version": "1.2.1" }, "last_serial": 2146901, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "84c7f0c6034728cc08fa43a06b220080", "sha256": "e7d51cc37dec094924c2a9e98143055c31df6b81f563c99854c7696a0afa7c53" }, "downloads": -1, "filename": "django-context-logging-1.0.0.tar.gz", "has_sig": false, "md5_digest": "84c7f0c6034728cc08fa43a06b220080", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4048, "upload_time": "2016-05-25T15:12:50", "url": "https://files.pythonhosted.org/packages/e0/ee/4d8a833c49560966c113b490f9fefb57f4a3f318f54b3877a4cf52d13789/django-context-logging-1.0.0.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "d7ed419be8c463d1122879949e3ded04", "sha256": "83eac7866b3bd5247ed71b89a6dad4a507dbe73e0b6e5b47c8db609c8a7c0910" }, "downloads": -1, "filename": "django-context-logging-1.1.0.tar.gz", "has_sig": false, "md5_digest": "d7ed419be8c463d1122879949e3ded04", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4115, "upload_time": "2016-05-26T10:31:44", "url": "https://files.pythonhosted.org/packages/bb/d5/078df1ac096489c490d9bcbcb68feb1aae0b6c858bc6788946bf2ebab463/django-context-logging-1.1.0.tar.gz" } ], "1.2.1": [ { "comment_text": "", "digests": { "md5": "bc89d4d3572817e0dadcbaa51a15472b", "sha256": "322121a53eff22acecf1a754c0aba40a905b272000fad3be177775cad3b2a263" }, "downloads": -1, "filename": "django-context-logging-1.2.1.tar.gz", "has_sig": false, "md5_digest": "bc89d4d3572817e0dadcbaa51a15472b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5180, "upload_time": "2016-06-02T10:55:38", "url": "https://files.pythonhosted.org/packages/45/d1/b23d9b117758a93d3c56e1839233d9a77b47c6380454b004909d5912b719/django-context-logging-1.2.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "bc89d4d3572817e0dadcbaa51a15472b", "sha256": "322121a53eff22acecf1a754c0aba40a905b272000fad3be177775cad3b2a263" }, "downloads": -1, "filename": "django-context-logging-1.2.1.tar.gz", "has_sig": false, "md5_digest": "bc89d4d3572817e0dadcbaa51a15472b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5180, "upload_time": "2016-06-02T10:55:38", "url": "https://files.pythonhosted.org/packages/45/d1/b23d9b117758a93d3c56e1839233d9a77b47c6380454b004909d5912b719/django-context-logging-1.2.1.tar.gz" } ] }