Helper for starlette to add request id in logger
Project description
starlette-request-id
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
import uvicorn
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette_request_id import RequestIdMiddleware, init_logger
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):
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
[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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for starlette-request-id-0.3.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9e924b0669aedb88450c6c6f09087089551501a4b2a1e93dff4d14ad0d1d4f5 |
|
MD5 | aba4d3c606901b55771d66eb86018b01 |
|
BLAKE2b-256 | 6f4162f7cd9558cf76b07b53c4d595bb357c1a2080383f4834821d9491f1652b |
Close
Hashes for starlette_request_id-0.3.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2320e229932eccd9b28c598678bd181ee9e621822ae2a79a259d05b400078160 |
|
MD5 | 111944c5b86405d8bf2997cabc33cd96 |
|
BLAKE2b-256 | 04d0a8a6c7b7917005bf879c66235ff67c54ab379fbcf0dc86c942f2fc95c800 |