Fastapi: async logging

Created on 4 Oct 2020  路  16Comments  路  Source: tiangolo/fastapi

how can I use fasyapi with async logging? It鈥檚 sync logging by python standard lib logging.

First check

  • [ ] I added a very descriptive title to this issue.
  • [ ] I used the GitHub search to find a similar issue and didn't find it.
  • [ ] I searched the FastAPI documentation, with the integrated search.
  • [ ] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [ ] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [ ] I already checked if it is not related to FastAPI but to Pydantic.
  • [ ] I already checked if it is not related to FastAPI but to Swagger UI.
  • [ ] I already checked if it is not related to FastAPI but to ReDoc.
  • [ ] After submitting this, I commit to:

    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

    • Or, I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

    • Implement a Pull Request for a confirmed bug.

Example

Here's a self-contained minimal, reproducible, example with my use case:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}

Description

  • Open the browser and call the endpoint /.
  • It returns a JSON with {"Hello": "World"}.
  • I would like it to have an extra parameter to teleport me to the moon and back.

The solution you would like

I would like it to have a teleport_to_moon parameter that defaults to False, and can be set to True to teleport me:

from fastapi import FastAPI

app = FastAPI()


@app.get("/", teleport_to_moon=True)
def read_root():
    return {"Hello": "World"}

Describe alternatives you've considered

To wait for Space X moon travel plans to drop down long after they release them. But I would rather teleport.

Environment

  • OS: [e.g. Linux / Windows / macOS]:
  • FastAPI Version [e.g. 0.3.0]:

To know the FastAPI version use:

python -c "import fastapi; print(fastapi.__version__)"
  • Python version:

To know the Python version use:

python --version

Additional context

enhancement

Most helpful comment

How is this related to fastapi then?

All 16 comments

Your question is a bit unclear. Are you looking to use a particular async logging library?

Yes, I've a look at demo project and find it's sync logging.
Could you please show some practice with async logging?

https://github.com/tiangolo/full-stack-fastapi-postgresql

Async logging is very rarely required or desired. Can you explain why you want it?

I agree with @Mause here, it sounds like an arbitrary need.

Let me explain

@app.get("/dummypath")
async def get_dummy_path():
    logging.debug("say smth")
    return {"something":"here"}

This code piece will run in the event loop. Since it's running inside an event loop this will not block anything or it will not increase the performance.

assume that logging module is as fast as no matter to performance. In fact, it's not the bottleneck when the request per second below 3000.

If it's not the bottleneck why do you care about it?

Today, I've test a fastest web framework -- Japronto, python based.
It shows that Requests per second: 27015.82 [#/sec] (mean).
In Japronto application, if I put logging function, the performance drops obviously, around to 1/2 to 1/3.
Above all, sync logging function is not an issue if rps not to be aimed.

@littleforce163 Japronto is not an async framework and it is C Based.

How is this related to fastapi then?

@littleforce163 Japronto is not an async framework and it is C Based.

Oh锛宻orry for the mistake. For now锛孖 do not understand why IO operating锛坙ogging锛塪oesn鈥榯 effect the performance in fastapi. As I know锛宲ut IO/network operation to async would save CPU and would get better perfomace than sync. Please correct me if any mistakes.
It is async and swagger integrated that fastapi mostly attraced me. If it equals to Go performance top rank锛宨t would play an more important role in python锛宔ven in all web frameworks.
Above all锛宨f async logging would increase performance锛宲lease take it as consider.
Thank you for all your advice.

@littleforce163 if you need async logging maybe you can use this in your code!

I don't understand your request, are you asking:
1) should you use async logging in your code so your application is faster? or
2) fastapi change its internal logging to async logging?

If you are asking for 1) then by all means you can do so with the library suggested by @victoraugustolls or some other library. If you are suggesting 2) I think there is only minimal logging fastapi does, the logging you see is most likely from uvicorn which is a different project.

@littleforce163 if you need async logging maybe you can use this in your code!

It is not fully async when writing to file on disk. Others recommended?

Tldr; aiologger is only fully async when logging to stdout/stderr. If you log into files on disk you are not being fully async and will be using Threads.

Sorry, but that's the only async logger that I'm aware of

Sorry, but that's the only async logger that I'm aware of

So fast reply! Thank you all the same.

File IO generally cannot be async, so is usually simply done with thread's.

See more details here: https://github.com/python/asyncio/wiki/ThirdParty#filesystem

Was this page helpful?
0 / 5 - 0 ratings