Logging

Using Uvicorn's default logging.
 
# server.py
import uvicorn

if __name__ == "__main__":
    # This uses Uvicorn's built-in logging configuration
    uvicorn.run(
        "app:app",           # module:variable
        host="127.0.0.1",
        port=8000,
        log_level="info",    # info/debug/warning/error/critical
    )
app.py
 

from fastapi import FastAPI
import logging

app = FastAPI()

# Use a module-level logger
logger = logging.getLogger(__name__)

@app.get("/hello")
def hello():
    logger.info("Handling /hello")   # This will appear in the server logs
Run server
 
python server.py
For tests (pytest), logs are often captured. To show them on the test terminal, add a pytest.ini:
 

[pytest]
testpaths = tests
pythonpath = .
log_cli = false
log_cli_level = INFO
log_cli_format = %(asctime)s - %(levelname)s - %(message)s
log_cli_date_format = %Y-%m-%d %H:%M:
markers = 
    integration: tests that require real network/services (slower)
    unit: testclient (fast)