it-alien
it-alien

Reputation: 480

How to log request start in Sanic?

Sanic has access logging but in fact it logs the response, that is, it fires a message when the request processing has been already completed.

But is it possible to log the request's start, before the user-defined middleware/handler is called?

Upvotes: 0

Views: 114

Answers (1)

Adam Hopkins
Adam Hopkins

Reputation: 7092

Yes, to log something as early as possible, you can do this:

from sanic.log import logger


@app.signal("http.lifecycle.request")
async def log(request):
    logger.info(f"{request.method} {request.path}")

See docs on signals

Upvotes: 1

Related Questions