The logging module helps you print structured runtime messages (debug, info, warnings, and errors).
import logging
logging.basicConfig(level=logging.INFO)
logging.info("App started")
logging.warning("Low disk space")import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("Debug details")
logging.info("General information")
logging.warning("Something unexpected happened")
logging.error("An error occurred")
logging.critical("Serious failure")import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(message)s",
)
logging.info("User logged in")import logging
logging.basicConfig(
filename="app.log",
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(message)s",
)
logging.info("Saved order #123")import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("Running module code")import logging
logging.basicConfig(level=logging.INFO)
try:
result = 10 / 0
except ZeroDivisionError:
logging.exception("Calculation failed")import logging
import sys
logger = logging.getLogger("my_app")
logger.setLevel(logging.DEBUG) # Collect all levels; handlers decide what to output
logger.propagate = False
formatter = logging.Formatter("%(asctime)s | %(name)s | %(levelname)s | %(message)s")
# Handler 1: stdout (INFO and above)
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.INFO)
stdout_handler.setFormatter(formatter)
# Handler 2: file (DEBUG and above)
file_handler = logging.FileHandler("my_app.log", encoding="utf-8")
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
logger.debug("Loaded configuration file") # File only
logger.info("Service started successfully") # Stdout + file
logger.warning("Cache miss ratio is high") # Stdout + fileNote
Prefer logging over print() for real applications: it supports levels, formatting, and writing logs to files or external systems.