Skip to main content

Jiushu logger for Starlette/FastAPI routes.

Project description

logo.png

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jiushu_logger_starlette-1.0.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

jiushu_logger_starlette-1.0.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file jiushu_logger_starlette-1.0.0.tar.gz.

File metadata

File hashes

Hashes for jiushu_logger_starlette-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b4dbcd26a52c37854430e6ef3f9392561949ce3043007dc99be7f88d6ef45b1e
MD5 69a3e035b120f27ccabe2571ece1661e
BLAKE2b-256 675ae33e17a2c0aab73bc09576e10ad33a9c000d02fff999288b1e4b57addb62

See more details on using hashes here.

File details

Details for the file jiushu_logger_starlette-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jiushu_logger_starlette-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a1cccd9dd2ca2c18050f3c6adbec506b7f9e6f81698c301c3af016d22b62f8c5
MD5 f482dfa5c4b8cbad72808dc4bb1c5cdf
BLAKE2b-256 0f78b7566f88cc16e938cb08bfb73ae90a0990ed0636f7a73acd8d21f78e94ef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page