Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FastAPI - should I log asynchronously?

I have a python web application written using FastAPI (running via uvicorn). In my app I'm using the standard logging module which uses TimedRotatingFileHandler. Since I'm logging into a file I'm concerned about performance. Lets say I have 3 log messages in a function which is called with every call to a /test endpoint. Now imagine 1000 clients requesting /test at the same time - that is 3000 writes to the log file.

I'm concerned this will hinder the app's performance since IO tasks are time consuming. Should I use some form of asynchronous logging? Maybe open a new thread which will write into the file? I tried googling for answers but I found no standardized approach to it. So is it even needed? If yes, how should I approach this? Thanks!

like image 255
muliku Avatar asked Mar 17 '26 19:03

muliku


1 Answers

In my company, the server(around 2k req/s) that we work by default logs the request and response payload, and about 2-3 more logs depending on the type of request. Of course it is somewhat of a hinderance, but unless you really need to squeeze every bit of performance, chances are there are other areas that will bring much more improvement.

Aiologger can be a solution, but if you're writing to a file it blocks anyway so look out for that.

like image 54
Kungyu Lee Avatar answered Mar 20 '26 07:03

Kungyu Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!