how can I use fasyapi with async logging? It鈥檚 sync logging by python standard lib logging.
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"}
/.{"Hello": "World"}.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"}
To wait for Space X moon travel plans to drop down long after they release them. But I would rather teleport.
To know the FastAPI version use:
python -c "import fastapi; print(fastapi.__version__)"
To know the Python version use:
python --version
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?
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
Most helpful comment
How is this related to fastapi then?