{"id":20086901,"url":"https://github.com/neocrym/log-with-context","last_synced_at":"2025-11-08T05:03:08.651Z","repository":{"id":43152112,"uuid":"353189307","full_name":"neocrym/log-with-context","owner":"neocrym","description":"A thread-local, context-preserving, minimalist Python logger.","archived":false,"fork":false,"pushed_at":"2023-10-23T23:27:34.000Z","size":49,"stargazers_count":16,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-16T13:06:07.773Z","etag":null,"topics":["logging","minimalist","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/neocrym.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-31T01:31:47.000Z","updated_at":"2024-11-21T12:48:31.000Z","dependencies_parsed_at":"2022-08-30T07:31:20.093Z","dependency_job_id":null,"html_url":"https://github.com/neocrym/log-with-context","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neocrym%2Flog-with-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neocrym%2Flog-with-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neocrym%2Flog-with-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neocrym%2Flog-with-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/neocrym","download_url":"https://codeload.github.com/neocrym/log-with-context/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252609562,"owners_count":21775841,"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":["logging","minimalist","python"],"created_at":"2024-11-13T16:02:59.237Z","updated_at":"2025-11-08T05:03:08.646Z","avatar_url":"https://github.com/neocrym.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"log-with-context--a thread-local, context-preserving Python logger\n==================================================================\n\n``log-with-context`` is a Python logger that saves variables in a\nthread-local context to be passed as `extra` to Python\n`logging \u003chttps://docs.python.org/3/library/logging.html\u003e`_ methods.\n\nNote that ``log-with-context`` assumes that you are passing context\nin a multithreading sense--not in an asyncio sense.\n\nInstallation\n------------\n\nThis library is available on PyPI and can be installed with\n\n.. code:: bash\n\n    python3 -m pip install log-with-context\n\nUsage\n-----\n\nThis library provides a wrapped Python logging.Logger that\nadds a shared context to each logging message, passed as\nthe `extra` parameter.\n\n**You will need an additional library** (like\n`JSON-log-formatter \u003chttps://pypi.org/project/JSON-log-formatter/\u003e`_)\n**to actually output the logging messages**. We avoided putting this\nfunctionality in this library to keep it lightweight and flexible.\nWe assumed that you already have a preferred way to format your\nlogging messages.\n\n.. code:: python\n\n    import logging\n    import logging.config\n\n    from log_with_context import add_logging_context, Logger\n\n    logging.config.dictConfig({\n        \"version\": 1,\n        \"disable_existing_loggers\": True,\n        \"formatters\": {\n            \"json\": {\"()\": \"json_log_formatter.JSONFormatter\"},\n        },\n        \"handlers\": {\n            \"console\": {\n                \"formatter\": \"json\",\n                \"class\": \"logging.StreamHandler\",\n            }\n        },\n        \"loggers\": {\n            \"\": {\"handlers\": [\"console\"], \"level\": \"INFO\"},\n        },\n    })\n\n    LOGGER = Logger(__name__)\n\n    LOGGER.info(\"First message. No context\")\n\n    with add_logging_context(current_request=\"hi\"):\n        LOGGER.info(\"Level 1\")\n\n        with add_logging_context(more_info=\"this\"):\n            LOGGER.warning(\"Level 2\")\n\n        LOGGER.info(\"Back to level 1\")\n\n    LOGGER.error(\"No context at all...\")\n\n\nThe above program logs the following messages to standard error:\n\n.. code:: json\n\n    {\"message\": \"First message. No context\", \"time\": \"2021-04-08T16:37:23.126099\"}\n    {\"current_request\": \"hi\", \"message\": \"Level 1\", \"time\": \"2021-04-08T16:37:23.126336\"}\n    {\"current_request\": \"hi\", \"more_info\": \"this\", \"message\": \"Level 2\", \"time\": \"2021-04-08T16:37:23.126389\"}\n    {\"current_request\": \"hi\", \"message\": \"Back to level 1\", \"time\": \"2021-04-08T16:37:23.126457\"}\n    {\"message\": \"No context at all...\", \"time\": \"2021-04-08T16:37:23.126514\"}\n\n\nThis example may look trivial, but it is very handy to maintain a\nlogging context up and down a Python call stack without having\nto pass additional variables to the functions and methods\nthat you call.\n\nImplementation details\n----------------------\nLogging contexts are stored as thread-local variables. If you want\nto share information between threads, you must create a Logging\ncontext in each thread with the same information.\n\nSimilarly, logging contexts are *deliberately not copied* when\ncreating subprocesses. This is done to minimize bugs and make sure\nthat log-with-context behaves in the exact same manner across\noperating systems.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneocrym%2Flog-with-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneocrym%2Flog-with-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneocrym%2Flog-with-context/lists"}