Jiushu logger for Starlette/FastAPI routes.
Project description
jiushu-logger-starlette【九书 Starlette / FastAPI 路由专用】
简介
JF 专用格式化 logger 的 Starlette / FastAPI 路由特供版,专门输出请求日志。
使用方法
from asyncio import sleep
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import PlainTextResponse, JSONResponse
from starlette.routing import Route, Mount
from jiushu_logger_starlette.route_logging_middleware import RouterLoggingMiddleware
async def _test(request: Request):
# You can get trace id of *this* request.
# If apache-skywalking is used, this trace_id will be the ID tracing by skywalking.
return PlainTextResponse(request.state.trace_id.encode('utf_8'))
async def _test2(request: Request):
await sleep(1.23456)
return JSONResponse({'result': True}, headers={'Content-Type': 'application/json'})
async def _should_be_skip(request: Request):
return PlainTextResponse(b'Hello, world!')
async def _health(request: Request):
return JSONResponse({'status': 'UP'}, headers={'Content-Type': 'application/json'})
routes = [
Mount('/api', routes=[
Route('/test', _test),
Route('/test2', _test2, methods=['GET', 'POST']),
Route('/should-be-skip', _should_be_skip),
Route('/health', _health),
])
]
middleware = [
# Logging for routes.
# You can set which route should be skipped,
# or which pattern the route matches should be skipped.
Middleware(RouterLoggingMiddleware,
skip_routes=['/api/health'],
skip_regexes=[r'''^.*skip.*$'''])
]
app = Starlette(routes=routes, middleware=middleware)
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
File details
Details for the file jiushu_logger_starlette-1.0.1.tar.gz
.
File metadata
- Download URL: jiushu_logger_starlette-1.0.1.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a125b508629873ae703ec8cdd48453e563eabc5bc874a1e9394135ab14e138d3 |
|
MD5 | be2701e6fc71f10b3c329aa368aae853 |
|
BLAKE2b-256 | 29d03eb0088137c3d713286507da4bbba77a217d87534f90e43fb5ad852e5cc4 |
File details
Details for the file jiushu_logger_starlette-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: jiushu_logger_starlette-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a314d3eeb21bccffa3f08c941847f3616a173b4e1740ef0bb12f4a36198e4b03 |
|
MD5 | 9d75e69375c0d2d255e92bb29bbacfb4 |
|
BLAKE2b-256 | 0022540f992b15ac0a4b81b490350cb214cf44ca0ed0ad1298c0f3883989b14a |