Skip to content

Starlette

LoglyMiddleware adds request_id, method, path, client, and user_agent to the Logly context for each request. Logs timing and exceptions automatically.

Installation

This integration requires the starlette package.

bash
uv add logly[starlette]
bash
pip install "logly[starlette]"
bash
uv add starlette
bash
pip install starlette

Missing Dependency

If starlette is not installed, you'll see:

ModuleNotFoundError: No module named 'starlette'

Usage

python
from starlette.applications import Starlette
from logly.integrations.starlette import LoglyMiddleware

app = Starlette()
app.add_middleware(LoglyMiddleware)

Full Example

python
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
from logly import logger
from logly.integrations.starlette import LoglyMiddleware

async def homepage(request):
    logger.info("Homepage accessed")
    return JSONResponse({"message": "Hello World"})

async def item(request):
    item_id = request.path_params["item_id"]
    logger.debug("Fetching item {}", item_id)
    return JSONResponse({"item_id": item_id})

app = Starlette(routes=[
    Route("/", homepage),
    Route("/items/{item_id}", item),
])
app.add_middleware(LoglyMiddleware)

Released under the MIT License.