- GOODIES
- Generator
- Named Tuple
- Modules
- Type Checking
- APPLICATIONS
- Palindrome
- Word Search
- Conway Game
- Coin Flip
- SCHEDULER
- Time
- Multithreading
- Subprocess
- Logging
- PACKAGES
- Clipboard
- Socket
- Image
- Virtualenv
- Jupyter
- Icecream
- Anytree
- Watchdog
- Itertools
- Pywin32
- Fastapi
- FASTAPI
- Project Structure
- Enable Https
- Unit Testing
-
Logging
- Service Wrapper
- Middleware
PYTHON PAGES - LEVEL 2
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)