Chapter 26: Logging and Troubleshooting
ASP.NET Core has structured logging abstractions. Python has standard logging, plus libraries such as structlog and loguru. For production services, prefer standard logging plus structured output.
import logging
logger = logging.getLogger(__name__)
def publish_recipe(recipe_id: int, user_id: int) -> None:
logger.info(
"Publishing recipe",
extra={"recipe_id": recipe_id, "user_id": user_id},
)
Production logging practices:
- Log structured fields, not only prose.
- Include request ID or trace ID.
- Avoid logging secrets, tokens, passwords, and personal data.
- Log at boundaries: request, command, external call, background job, exception.
- Use exception logging with stack traces for unexpected failures.
- Add health endpoints and readiness checks.
Troubleshooting Python apps also means understanding import paths, virtual environments, process managers, worker counts, and environment variables. Many "framework bugs" are actually deployment shape bugs.