{"id":15907977,"url":"https://github.com/sh-cho/django-custom-logging","last_synced_at":"2025-03-21T21:31:52.302Z","repository":{"id":57419618,"uuid":"351816324","full_name":"sh-cho/django-custom-logging","owner":"sh-cho","description":"Django middleware for custom format logging 🪵","archived":false,"fork":false,"pushed_at":"2024-03-02T08:08:44.000Z","size":56,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2025-03-01T10:13:38.148Z","etag":null,"topics":["django","django-rest-framework","logging","middleware","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/sh-cho.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2021-03-26T14:51:39.000Z","updated_at":"2024-03-02T03:44:10.000Z","dependencies_parsed_at":"2024-03-01T10:25:58.302Z","dependency_job_id":"e56e73c8-b9a4-48be-86e1-7246e71fd1f5","html_url":"https://github.com/sh-cho/django-custom-logging","commit_stats":{"total_commits":89,"total_committers":2,"mean_commits":44.5,"dds":"0.011235955056179803","last_synced_commit":"b10d487f6f0eb69be572ef41e06c0336f0174b20"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sh-cho%2Fdjango-custom-logging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sh-cho%2Fdjango-custom-logging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sh-cho%2Fdjango-custom-logging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sh-cho%2Fdjango-custom-logging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sh-cho","download_url":"https://codeload.github.com/sh-cho/django-custom-logging/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244164612,"owners_count":20408962,"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":["django","django-rest-framework","logging","middleware","python"],"created_at":"2024-10-06T14:08:21.650Z","updated_at":"2025-03-21T21:31:51.982Z","avatar_url":"https://github.com/sh-cho.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django-custom-logging \u003cimg src=\"https://github.com/sh-cho/django-custom-logging/assets/11611397/934a41d5-2f95-49a1-9033-294db188cbac\" width=\"250\" align=\"right\" alt=\"django-custom-logging logo\"\u003e\n\n\u003e django middleware for custom format logging\n\n[![PyPI](https://img.shields.io/pypi/v/django-custom-logging)](https://pypi.python.org/pypi/django-custom-logging/)\n[![PyPI - License](https://img.shields.io/pypi/l/django-custom-logging)](https://github.com/sh-cho/django-custom-logging/blob/master/LICENSE)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/django-custom-logging)](https://pypi.python.org/pypi/django-custom-logging/)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/django-custom-logging)](https://pypi.python.org/pypi/django-custom-logging/)\n![PyPI - Wheel](https://img.shields.io/pypi/wheel/django-custom-logging)\n[![Test](https://github.com/sh-cho/django-custom-logging/actions/workflows/test.yml/badge.svg?branch=develop)](https://github.com/sh-cho/django-custom-logging/actions/workflows/test.yml)\n[![Lint](https://github.com/sh-cho/django-custom-logging/actions/workflows/lint.yml/badge.svg?branch=develop)](https://github.com/sh-cho/django-custom-logging/actions/workflows/lint.yml)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n\n## Installation\n1. Install the package\n```sh\npython -m pip install django-custom-logging\n```\n\n2. Add adequate middlewares to `MIDDLEWARE` in setting file. Current version only supports a middleware that captures `request` into local thread(`threading.local()`)\n```python\nMIDDLEWARE = (\n    # other middlewares ...\n    \"custom_logging.middlewares.capture_request\",\n)\n```\n\n3. Add `custom_logging.filters.CustomFilter` to `LOGGING` in setting file and set `capture_list` containing a list of variables to be captured(`capture_in`) and format string to be printed(`capture_out`). Also add filter on handler's filter list.\n```python\nLOGGING = {\n    \"version\": 1,\n    \"disable_existing_loggers\": False,\n    \"formatters\": {\n        \"verbose\": {\n            \"format\": \"{levelname} {asctime} {module} {meta[REMOTE_ADDR]} {meta[CONTENT_LENGTH]} {process:d} {thread:d}\"\n                      \" [USER_ID:{user_id}] {message}\",\n                                #^^^^^^^^^ - (A)\n            \"style\": \"{\",\n        },\n    },\n    \"filters\": {\n        \"custom_filter\": {\n        #^^^^^^^^^^^^^ - (B)\n            \"()\": \"custom_logging.filters.CustomFilter\",\n            \"capture_list\": (\n                # (capture_in, capture_out)\n                (\"request.user.id\", \"user_id\"),\n                                    #^^^^^^^ - (A)\n                (\"request.META\", \"meta\"),\n            ),\n            \"default_values\" : {\n                \"meta\": {\n                    \"REMOTE_ADDR\": \"127.0.0.1\",\n                    \"CONTENT_LENGTH\": 0,\n                }\n            },\n        },\n    },\n    \"handlers\": {\n        \"console\": {\n            \"level\": \"DEBUG\",\n            \"class\": \"logging.StreamHandler\",\n            \"formatter\": \"verbose\",\n            \"filters\": [\"custom_filter\"],\n                        #^^^^^^^^^^^^^ - (B)\n        },\n    },\n    \"root\": {\"level\": \"INFO\", \"handlers\": [\"console\"]},\n}\n```\nNote that you can use any format styles(%, {, $), but should make format arguments with `str` type. For example, if you want to capture `request.user.id` as `user_id`, please follow format below.\n```\n%-style: %(user_id)s\n{-style: {user_id}\n$-style: ${user_id}\n```\n\n### ⚠️ Specifying Default Values\nDefault values should be provided if `capture_out` is object and its attribute can be undefined. (ex. `{meta[REMOTE_ADDR]}`)\n\nFor example, accessing request variable in filter while using scheduling cronjob(ex. `@shared_task`) can raise error because it is not HTTP request so `request` is not defined.\n\nIf `capture_out` is single value(ex. str, int, etc.), default value is not needed. It will be replaced with placeholder `-` if it is not defined.\n\n\n## How to use\nYou can use `logger` just like before. No extra parameter is needed.\n\n```python\nimport logging\n\nfrom rest_framework import status\nfrom rest_framework.permissions import IsAuthenticated\nfrom rest_framework.response import Response\nfrom rest_framework.views import APIView\n\nlogger = logging.getLogger(__name__)\n\n\nclass ExampleView(APIView):\n    permission_classes = (IsAuthenticated,)\n\n    def post(self, request, format=None):\n        logger.info(\"example log\")\n        return Response({\"hello\": \"world!\"}, status=status.HTTP_200_OK)\n```\n\n```\nINFO 2024-03-01 11:33:25,170 credentials 127.0.0.1 0 35052 4748750336 [USER_ID:-] Found credentials in shared credentials file: ~/.aws/credentials\nINFO 2024-03-01 11:33:25,505 views 123.123.123.123 1000 35052 4748750336 [USER_ID:33] example log\n```\n\n\n## Supported versions\n- Python: \u003e=3.5\n- Django: \u003e=3\n\n## License\nSee [LICENSE](./LICENSE)\n\n\n## Contribution\nFeel free to open issue or pull request.\n\n### Contributors\n\n\u003ca href=\"https://github.com/sh-cho/django-custom-logging/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=sh-cho/django-custom-logging\" /\u003e\n\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsh-cho%2Fdjango-custom-logging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsh-cho%2Fdjango-custom-logging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsh-cho%2Fdjango-custom-logging/lists"}