{"id":13468732,"url":"https://github.com/peopledoc/pylogctx","last_synced_at":"2025-12-13T17:02:53.310Z","repository":{"id":49255912,"uuid":"60260832","full_name":"peopledoc/pylogctx","owner":"peopledoc","description":":snake: Inject extra fields to each log records","archived":false,"fork":false,"pushed_at":"2024-01-18T13:42:39.000Z","size":94,"stargazers_count":25,"open_issues_count":2,"forks_count":9,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-12-15T06:10:05.535Z","etag":null,"topics":["approved-public","celery","cloud","devops","django","ghec-mig-migrated","json","logging","python","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/peopledoc.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-06-02T12:05:31.000Z","updated_at":"2023-09-27T19:10:03.000Z","dependencies_parsed_at":"2024-06-21T15:20:39.754Z","dependency_job_id":"941c64c2-4cd7-48c3-8cdc-9a86e30f9f7b","html_url":"https://github.com/peopledoc/pylogctx","commit_stats":null,"previous_names":["novafloss/pylogctx"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peopledoc%2Fpylogctx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peopledoc%2Fpylogctx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peopledoc%2Fpylogctx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peopledoc%2Fpylogctx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peopledoc","download_url":"https://codeload.github.com/peopledoc/pylogctx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229787011,"owners_count":18124014,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["approved-public","celery","cloud","devops","django","ghec-mig-migrated","json","logging","python","python3"],"created_at":"2024-07-31T15:01:17.720Z","updated_at":"2025-12-13T17:02:47.911Z","avatar_url":"https://github.com/peopledoc.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"| |GitHub| |BSD| |CI| |PyPI|\n\n########################\n Python Logging Context\n########################\n\n``pylogctx`` is a library for enriching each logs records from a context.\nTypical usage is for adding some ``request_id`` to all logs in order to make\ntroubleshooting more comfortable. This context is shared by all piece of code\nusing ``logging``, transparently.\n\n.. code-block::\n\n    import logging.config\n\n    from pylogctx import context as log_context\n\n\n    logging.config.dictConfig({\n        'formatters': {'json': {\n            '()': 'pythonjsonlogger.jsonlogger.JsonFormatter',\n            'format': \"%(asctime)s %(name)s %(levelname)s %(message)s\",\n        }},\n        'filters': {'context': {\n            '()': 'pylogctx.AddContextFilter',\n        }},\n        'handlers': {'console': {\n            'class': 'logging.StreamHandler',\n            'filters': ['context'],\n            'formatter': 'json',\n        }},\n        'root': {\n            'level': 'INFO',\n            'handlers': ['console'],\n        },\n    })\n\n\n    logger = logging.getLogger(__name__)\n\n\n    def mycode(request, ticket_id):\n        # push new fields\n        log_context.update(requestId=uuid.uuid4())\n        myticket = get_object_or_404(models.Ticket, pk=ticket_id)\n\n        # push objects, they will be adapted to log fields\n        log_context.update(myticket):\n\n        # Log as usual\n        logger.info(\"Working on %r\", myticket)\n\n        for comment in myticket.comments:\n            # A context manager allow to push and pop fields\n            with log_context(comment):\n                logger.info(\"Working on comment %r\", comment)\n                # code, use external libs, etc.\n\n        # Don't forget to clear the context for the next request. Use the\n        # middleware to have it clean.\n        log_context.clear()\n\n\nThe output looks like::\n\n     {'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on \u003cTicket #1\u003e', 'ticketId': 1, 'requestId': 'c5521138-031a-4da6-b9db-c9eda3e090f1'}\n     {'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on comment \u003cComment #4\u003e', 'ticketId': 1, 'ticketCommentId': 4, 'requestId': 'c5521138-031a-4da6-b9db-c9eda3e090f1'}\n     {'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on comment \u003cComment #5\u003e', 'ticketId': 1, 'ticketCommentId': 5, 'requestId': 'c5521138-031a-4da6-b9db-c9eda3e090f1'}\n     {'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on comment \u003cComment #78\u003e', 'ticketId': 1, 'ticketCommentId': 78, 'requestId': 'c5521138-031a-4da6-b9db-c9eda3e090f1'}\n     {'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on comment \u003cComment #9\u003e', 'ticketId': 1, 'ticketCommentId': 9, 'requestId': 'c5521138-031a-4da6-b9db-c9eda3e090f1'}\n     {'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on \u003cTicket #890\u003e', 'ticketId': 890, 'requestId': 'c64aaae7-049b-4a02-929b-2d0ac9141f5c'}\n     {'loggerName': 'package.module', 'levelname': 'INFO', 'message': 'Working on comment \u003cComment #80\u003e', 'ticketId': 890, 'ticketCommentId': 80, 'requestId': 'c64aaae7-049b-4a02-929b-2d0ac9141f5c'}\n\n\nInstall it with your favorite python package installer::\n\n   $ pip install pylogctx\n\n\nThere is a few helpers for Celery_ and Django_ projects. See USAGE_ for details!\n\n\nContributors\n============\n\nJoin us to make log rocking better! Read HACKING_ and ask maintainers:\n\n* David STEINBERGER `@dsteinberger \u003chttps://github.com/dsteinberger\u003e`_\n* Étienne BERSAC `@bersace \u003chttps://github.com/bersace\u003e`_\n* Lev Orekhov `@lorehov \u003chttps://github.com/lorehov\u003e`_\n* Malte Baden Hansen `@Moulde \u003chttps://github.com/Moulde\u003e`_\n* Vincent RIALLAND `@vrialland \u003chttps://github.com/vrialland\u003e`_\n* Hugo SMETT `@hsmett \u003chttps://github.com/hsmett\u003e`_\n\n\n.. |BSD| image:: https://img.shields.io/pypi/l/pylogctx.svg?maxAge=2592000\n   :target: https://github.com/novafloss/pylogctx/blob/master/LICENSE\n   :alt: BSD Licensed\n\n.. |CI| image:: https://travis-ci.org/peopledoc/pylogctx.svg?style=shield\n   :target: https://travis-ci.org/peopledoc/pylogctx\n   :alt: CI Status\n\n.. |GitHub| image:: https://img.shields.io/github/stars/novafloss/pylogctx.svg?label=GitHub%20stars\n   :target: https://github.com/novafloss/pylogctx\n   :alt: GitHub homepage\n\n.. |PyPI| image:: https://img.shields.io/pypi/v/pylogctx.svg\n   :target: https://pypi.python.org/pypi/pylogctx\n   :alt: Version on PyPI\n\n.. _Celery: http://www.celeryproject.org/\n.. _Django: https://www.djangoproject.com/\n.. _HACKING: https://github.com/novafloss/pylogctx/blob/master/HACKING.rst\n.. _USAGE: https://github.com/novafloss/pylogctx/blob/master/USAGE.rst\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeopledoc%2Fpylogctx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeopledoc%2Fpylogctx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeopledoc%2Fpylogctx/lists"}