{"id":21031282,"url":"https://github.com/workable/flask-log-request-id","last_synced_at":"2025-04-05T07:04:30.051Z","repository":{"id":39117097,"uuid":"98310599","full_name":"Workable/flask-log-request-id","owner":"Workable","description":"Flask extension to track and log Request-ID headers produced by PaaS like Heroku and load balancers like Amazon ELB","archived":false,"fork":false,"pushed_at":"2024-07-02T18:47:24.000Z","size":77,"stargazers_count":116,"open_issues_count":17,"forks_count":24,"subscribers_count":12,"default_branch":"develop","last_synced_at":"2025-03-29T06:09:17.501Z","etag":null,"topics":["celery","flask","python","request-id","web"],"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/Workable.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2017-07-25T13:42:39.000Z","updated_at":"2025-02-03T14:17:53.000Z","dependencies_parsed_at":"2024-12-14T21:04:34.271Z","dependency_job_id":"b14d86b8-69b5-4be4-bcd0-d22272ef54b3","html_url":"https://github.com/Workable/flask-log-request-id","commit_stats":{"total_commits":55,"total_committers":5,"mean_commits":11.0,"dds":0.1636363636363637,"last_synced_commit":"b21fe7bcff371b082a257ccf1d077d8b8279f169"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workable%2Fflask-log-request-id","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workable%2Fflask-log-request-id/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workable%2Fflask-log-request-id/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workable%2Fflask-log-request-id/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Workable","download_url":"https://codeload.github.com/Workable/flask-log-request-id/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247299831,"owners_count":20916190,"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":["celery","flask","python","request-id","web"],"created_at":"2024-11-19T12:27:04.131Z","updated_at":"2025-04-05T07:04:30.022Z","avatar_url":"https://github.com/Workable.png","language":"Python","readme":"\n# Flask-Log-Request-Id\n\n[![CircleCI](https://img.shields.io/circleci/project/github/Workable/flask-log-request-id.svg)](https://circleci.com/gh/Workable/flask-log-request-id)\n\n**Flask-Log-Request-Id** is an extension for [Flask](http://flask.pocoo.org/) that can parse and handle the\nrequest-id sent by request processors like [Amazon ELB](http://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-request-tracing.html),\n[Heroku Request-ID](https://devcenter.heroku.com/articles/http-request-id) or any multi-tier infrastructure as the one used\nat microservices. A common usage scenario is to inject the request_id in the logging system so that all log records,\neven those emitted by third party libraries, have attached the request_id that initiated their call. This can\ngreatly improve tracing and debugging of problems.\n\n## Installation\n\nThe easiest way to install it is using ``pip`` from PyPI\n\n```bash\npip install flask-log-request-id\n```\n\n## Usage\n\nFlask-Log-Request-Id provides the `current_request_id()` function which can be used at any time to get the request\nid of the initiated execution chain.\n\n\n### Example 1: Parse request id and print to stdout\n```python\nfrom flask_log_request_id import RequestID, current_request_id\n\n[...]\n\nRequestID(app)\n\n\n@app.route('/')\ndef hello():\n    print('Current request id: {}'.format(current_request_id()))\n```\n\n\n### Example 2: Parse request id and send it to to logging\n\nIn the following example, we will use the `RequestIDLogFilter` to inject the request id on all log events, and\na custom formatter to print this information. If all these sounds unfamiliar please take a look at [python's logging\nsystem](https://docs.python.org/3/library/logging.html)\n\n\n```python\nimport logging\nimport logging.config\nfrom random import randint\nfrom flask import Flask\nfrom flask_log_request_id import RequestID, RequestIDLogFilter\n\ndef generic_add(a, b):\n    \"\"\"Simple function to add two numbers that is not aware of the request id\"\"\"\n    logging.debug('Called generic_add({}, {})'.format(a, b))\n    return a + b\n\napp = Flask(__name__)\nRequestID(app)\n\n# Setup logging\nhandler = logging.StreamHandler()\nhandler.setFormatter(logging.Formatter(\"%(asctime)s - %(name)s - level=%(levelname)s - request_id=%(request_id)s - %(message)s\"))\nhandler.addFilter(RequestIDLogFilter())  # \u003c\u003c Add request id contextual filter\nlogging.getLogger().addHandler(handler)\n\n\n@app.route('/')\ndef index():\n    a, b = randint(1, 15), randint(1, 15)\n    logging.info('Adding two random numbers {} {}'.format(a, b))\n    return str(generic_add(a, b))\n```\n\nThe above will output the following log entries:\n\n```\n2017-07-25 16:15:25,912 - __main__ - level=INFO - request_id=7ff2946c-efe0-4c51-b337-fcdcdfe8397b - Adding two random numbers 11 14\n2017-07-25 16:15:25,913 - __main__ - level=DEBUG - request_id=7ff2946c-efe0-4c51-b337-fcdcdfe8397b - Called generic_add(11, 14)\n2017-07-25 16:15:25,913 - werkzeug - level=INFO - request_id=None - 127.0.0.1 - - [25/Jul/2017 16:15:25] \"GET / HTTP/1.1\" 200 -\n```\n\n### Example 3: Forward request_id to celery tasks\n\nFlask-Log-Request-Id comes with extras to forward the context of current request_id to the workers of celery tasks.\nIn order to use this feature you need to enable the celery plugin and configure the `Celery` instance. Then you can\nuse `current_request_id()` from inside your worker\n\n```python\nfrom flask_log_request_id.extras.celery import enable_request_id_propagation\nfrom flask_log_request_id import current_request_id\nfrom celery.app import Celery\nimport logging\n\ncelery = Celery()\nenable_request_id_propagation(celery)  # \u003c\u003c This step here is critical to propagate request-id to workers\n\napp = Flask()\n\n@celery.task()\ndef generic_add(a, b):\n    \"\"\"Simple function to add two numbers that is not aware of the request id\"\"\"\n\n    logging.debug('Called generic_add({}, {}) from request_id: {}'.format(a, b, current_request_id()))\n    return a + b\n\n@app.route('/')\ndef index():\n    a, b = randint(1, 15), randint(1, 15)\n    logging.info('Adding two random numbers {} {}'.format(a, b))\n    return str(generic_add.delay(a, b))  # Calling the task here, will forward the request id to the workers\n```\n\nYou can follow the same logging strategy for both web application and workers using the `RequestIDLogFilter` as shown in\nexample 1 and 2.\n\n### Example 4: If you want to return request id in response\n\nThis will be useful while integrating with frontend where in you can get the request id from the response (be it 400 or 500) and then trace the request in logs.\n\n```python\nfrom flask_log_request_id import current_request_id\n\n@app.after_request\ndef append_request_id(response):\n    response.headers.add('X-REQUEST-ID', current_request_id())\n    return response\n```\n\n## Configuration\n\nThe following parameters can be configured through Flask's configuration system:\n\n| Configuration Name | Description |\n| ------------------ | ----------- |\n| **LOG_REQUEST_ID_GENERATE_IF_NOT_FOUND**| In case the request does not hold any request id, the extension will generate one. Otherwise `current_request_id` will return None. |\n| **LOG_REQUEST_ID_LOG_ALL_REQUESTS** | If True, it will emit a log event at the request containing all the details as `werkzeug` would done along with the `request_id` . |\n| **LOG_REQUEST_ID_G_OBJECT_ATTRIBUTE** | This is the attribute of `Flask.g` object to store the current request id. Should be changed only if there is a problem. Use `current_request_id()` to fetch the current id. |\n\n\n## License\n\nSee the [LICENSE](LICENSE.md) file for license rights and limitations (MIT).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworkable%2Fflask-log-request-id","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fworkable%2Fflask-log-request-id","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworkable%2Fflask-log-request-id/lists"}