Search code examples
loggingsanic

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?


Solution

  • 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