✨ Support `--log-config` option by 07pepa · Pull Request #36 · fastapi/fastapi-cli
Following up on my previous comment saying that after some studies I will suggest for production service to avoid fastapi-cli installing fastapi and uvicorn without fastapi[standard] or other versions.
You can create the main.py file with explicit uvicorn configuration:
import uvicorn uvicorn.run( "app_folder.app_creator:app", host=<host>, # example: "0.0.0.0" port=<port>, # example: 8080 workers=<workers>, # example: 3 reload=<reload>, # false in production log_config=<logging_config dict> # Optional )
- I suggest to use
pydantic-settingsto create a configuration object containing all the parameters in order to be flexible with different configuration sources having a type validation logging_config dictfollow the python logging standard configuration that can be found here. In this way you can have a clean cli that is much readable server-side and you can set up other handlers like jsonl file or more fancy handlers. In my case I set up a clean and minimal cli log without fancy lines and similar stuff, plus a jsonl like handler that split the file by level and rotate them, all the handlers managed by a queue handler that process them in a separated thread in order to avoid api performance issues.
In app_creator.py create your FastAPI app with all necessary components like lifespan, routers, middleware etc.
Something similar to:
from fastapi import FastAPI app = FastAPI( title="Your API", # all needed parameters and configurations ) # Add routers, middleware, exception handlers, etc. app.include_router(your_router)