# starlette-request-id
[![CI](https://github.com/bigbag/starlette-request-id/workflows/CI/badge.svg)](https://github.com/bigbag/starlette-request-id/actions?query=workflow%3ACI)
[![codecov](https://codecov.io/gh/bigbag/starlette-request-id/branch/main/graph/badge.svg?token=ZRUN7SUKB2)](https://codecov.io/gh/bigbag/starlette-request-id)
[![pypi](https://img.shields.io/pypi/v/starlette-request-id.svg)](https://pypi.python.org/pypi/starlette-request-id)
[![downloads](https://img.shields.io/pypi/dm/starlette-request-id.svg)](https://pypistats.org/packages/starlette-request-id)
[![versions](https://img.shields.io/pypi/pyversions/starlette-request-id.svg)](https://github.com/bigbag/starlette-request-id)
[![license](https://img.shields.io/github/license/bigbag/starlette-request-id.svg)](https://github.com/bigbag/starlette-request-id/blob/master/LICENSE)
**starlette-request-id** is a helper for starlette to add request id in logger.
## Installation
starlette-request-id is available on PyPI.
Use pip to install:
$ pip install starlette-request-id
## Basic Usage
```py
import httpx
import uvicorn
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette_request_id import REQUEST_ID_HEADER, RequestIdMiddleware, init_logger, request_id_ctx
LOGGING = {
"version": 1,
"disable_existing_loggers": 0,
"formatters": {
"default": {
"format": "[%(asctime)s] %(levelname)s [%(request_id)s] %(name)s | %(message)s",
"datefmt": "%d/%b/%Y %H:%M:%S",
}
},
"handlers": {
"stdout": {
"level": "INFO",
"class": "logging.StreamHandler",
"formatter": "default",
},
},
"loggers": {
"": {
"handlers": ["stdout"],
"propagate": True,
"level": "INFO",
},
},
}
def init_app():
init_logger(LOGGING)
app_ = Starlette()
app_.add_middleware(RequestIdMiddleware)
@app_.route("/")
def success(request):
httpx.post("https://www.example.org/", headers={REQUEST_ID_HEADER: request_id_ctx.get()})
return PlainTextResponse("OK", status_code=200)
return app_
app = init_app()
if __name__ == "__main__":
uvicorn.run(
app=app,
log_config=LOGGING,
)
```
curl 127.0.0.1:8000
```bash
[17/Jan/2021 18:31:19] INFO [N/A] uvicorn.error | Started server process [576540]
[17/Jan/2021 18:31:19] INFO [N/A] uvicorn.error | Waiting for application startup.
[17/Jan/2021 18:31:19] INFO [N/A] uvicorn.error | Application startup complete.
[17/Jan/2021 18:31:19] INFO [N/A] uvicorn.error | Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
[17/Jan/2021 18:31:22] INFO [22395fa2-e296-420e-93a1-5537e1ba0a62] uvicorn.access | 127.0.0.1:50372 - "GET / HTTP/1.1" 200
[17/Jan/2021 18:31:25] INFO [9ac6fa25-5048-4222-ac54-dd2c70e3e042] uvicorn.access | 127.0.0.1:50374 - "GET / HTTP/1.1" 200
```
## License
starlette-request-id is developed and distributed under the Apache 2.0 license.
## Reporting a Security Vulnerability
See our [security policy](https://github.com/bigbag/starlette-request-id/security/policy).
Raw data
{
"_id": null,
"home_page": "https://github.com/bigbag/starlette-request-id",
"name": "starlette-request-id",
"maintainer": "Pavel Liashkov",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "pavel.liashkov@protonmail.com",
"keywords": "",
"author": "Pavel Liashkov",
"author_email": "pavel.liashkov@protonmail.com",
"download_url": "https://files.pythonhosted.org/packages/02/59/d61e9e2d77aac7d03afb15f802cdb85e0c319f6c9c4294788ff1c08aaa11/starlette-request-id-0.7.0.tar.gz",
"platform": "POSIX",
"description": "# starlette-request-id\n\n[![CI](https://github.com/bigbag/starlette-request-id/workflows/CI/badge.svg)](https://github.com/bigbag/starlette-request-id/actions?query=workflow%3ACI)\n[![codecov](https://codecov.io/gh/bigbag/starlette-request-id/branch/main/graph/badge.svg?token=ZRUN7SUKB2)](https://codecov.io/gh/bigbag/starlette-request-id)\n[![pypi](https://img.shields.io/pypi/v/starlette-request-id.svg)](https://pypi.python.org/pypi/starlette-request-id)\n[![downloads](https://img.shields.io/pypi/dm/starlette-request-id.svg)](https://pypistats.org/packages/starlette-request-id)\n[![versions](https://img.shields.io/pypi/pyversions/starlette-request-id.svg)](https://github.com/bigbag/starlette-request-id)\n[![license](https://img.shields.io/github/license/bigbag/starlette-request-id.svg)](https://github.com/bigbag/starlette-request-id/blob/master/LICENSE)\n\n\n**starlette-request-id** is a helper for starlette to add request id in logger.\n\n## Installation\n\nstarlette-request-id is available on PyPI.\nUse pip to install:\n\n $ pip install starlette-request-id\n\n## Basic Usage\n\n```py\nimport httpx\nimport uvicorn\nfrom starlette.applications import Starlette\nfrom starlette.responses import PlainTextResponse\nfrom starlette_request_id import REQUEST_ID_HEADER, RequestIdMiddleware, init_logger, request_id_ctx\n\nLOGGING = {\n \"version\": 1,\n \"disable_existing_loggers\": 0,\n \"formatters\": {\n \"default\": {\n \"format\": \"[%(asctime)s] %(levelname)s [%(request_id)s] %(name)s | %(message)s\",\n \"datefmt\": \"%d/%b/%Y %H:%M:%S\",\n }\n },\n \"handlers\": {\n \"stdout\": {\n \"level\": \"INFO\",\n \"class\": \"logging.StreamHandler\",\n \"formatter\": \"default\",\n },\n },\n \"loggers\": {\n \"\": {\n \"handlers\": [\"stdout\"],\n \"propagate\": True,\n \"level\": \"INFO\",\n },\n },\n}\n\n\ndef init_app():\n init_logger(LOGGING)\n\n app_ = Starlette()\n app_.add_middleware(RequestIdMiddleware)\n\n @app_.route(\"/\")\n def success(request):\n httpx.post(\"https://www.example.org/\", headers={REQUEST_ID_HEADER: request_id_ctx.get()})\n return PlainTextResponse(\"OK\", status_code=200)\n\n return app_\n\n\napp = init_app()\n\nif __name__ == \"__main__\":\n uvicorn.run(\n app=app,\n log_config=LOGGING,\n )\n\n```\n\ncurl 127.0.0.1:8000\n\n```bash\n [17/Jan/2021 18:31:19] INFO [N/A] uvicorn.error | Started server process [576540]\n [17/Jan/2021 18:31:19] INFO [N/A] uvicorn.error | Waiting for application startup.\n [17/Jan/2021 18:31:19] INFO [N/A] uvicorn.error | Application startup complete.\n [17/Jan/2021 18:31:19] INFO [N/A] uvicorn.error | Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n [17/Jan/2021 18:31:22] INFO [22395fa2-e296-420e-93a1-5537e1ba0a62] uvicorn.access | 127.0.0.1:50372 - \"GET / HTTP/1.1\" 200\n [17/Jan/2021 18:31:25] INFO [9ac6fa25-5048-4222-ac54-dd2c70e3e042] uvicorn.access | 127.0.0.1:50374 - \"GET / HTTP/1.1\" 200\n```\n## License\n\nstarlette-request-id is developed and distributed under the Apache 2.0 license.\n\n## Reporting a Security Vulnerability\n\nSee our [security policy](https://github.com/bigbag/starlette-request-id/security/policy).\n",
"bugtrack_url": null,
"license": "Apache License, Version 2.0",
"summary": "Helper for starlette to add request id in logger",
"version": "0.7.0",
"project_urls": {
"Download": "https://pypi.python.org/pypi/starlette-request-id",
"Homepage": "https://github.com/bigbag/starlette-request-id"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b45d8a33de0e22e63622823874cef5cd257625263dba693b3d34b0004122608a",
"md5": "2d97b605be09ff823e919d7e132c4193",
"sha256": "a87f095922dba79810734fb8065bf3303448534f2c636547d5693b173bd59ad5"
},
"downloads": -1,
"filename": "starlette_request_id-0.7.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2d97b605be09ff823e919d7e132c4193",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 11295,
"upload_time": "2023-08-03T12:10:57",
"upload_time_iso_8601": "2023-08-03T12:10:57.357022Z",
"url": "https://files.pythonhosted.org/packages/b4/5d/8a33de0e22e63622823874cef5cd257625263dba693b3d34b0004122608a/starlette_request_id-0.7.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0259d61e9e2d77aac7d03afb15f802cdb85e0c319f6c9c4294788ff1c08aaa11",
"md5": "83c61ae5f4208adddfed9834594536cc",
"sha256": "9ae3ef706ee998085f971ed0c238df537b9d631681d4c936bc27258a7b11fa6e"
},
"downloads": -1,
"filename": "starlette-request-id-0.7.0.tar.gz",
"has_sig": false,
"md5_digest": "83c61ae5f4208adddfed9834594536cc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 9709,
"upload_time": "2023-08-03T12:11:01",
"upload_time_iso_8601": "2023-08-03T12:11:01.699464Z",
"url": "https://files.pythonhosted.org/packages/02/59/d61e9e2d77aac7d03afb15f802cdb85e0c319f6c9c4294788ff1c08aaa11/starlette-request-id-0.7.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-03 12:11:01",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bigbag",
"github_project": "starlette-request-id",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "starlette-request-id"
}