Logging
4 min read
Use logging instead of print() for application messages. Create a logger with logging.getLogger(__name__). Use levels DEBUG, INFO, WARNING, ERROR, and CRITICAL. Configure handlers to write to files or console.
Python Logging
import logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.debug("Debug info")
logging.info("App started")
logging.warning("Low disk space")
logging.error("Connection failed")
logging.critical("System down!")