FastAPI Uvicorn logging in Production
Hello 🙋♂️, Running a ⏩FastAPI ⏩ application in production is very easy and fast, but along the way some Uvicorn logs are lost. In this article I will discuss how to write a custom UvicornWorker and to centralize your logging configuration into a single file. To keep things as simple as possible I’ve put all my code in a single Python file. main.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import uvicorn as uvicorn from fastapi import FastAPI, APIRouter router = APIRouter(prefix="") def create_app(): fast_app = FastAPI() fast_app.include_router(router) return fast_app @router.get("/") def read_root(): return {"Hello": "World"} if __name__ == '__main__': app = create_app() uvicorn.run(app=app) Running the code will return a {"Hello": "World"} json when you visit the root endpoint / at http://127.0.0.1:8000. 😁 ...