Python logging - 1
1.Basics of Python Logging
import logging
# Configure basic logging
logging.basicConfig(
filename="app.log", # Logs will be saved in 'app.log'
filemode="w", # Overwrites the file each run
format="%(asctime)s - %(levelname)s - %(message)s",
level=logging.DEBUG # Set log level
)
# Generate log messages
logging.debug("This is a debug message")
logging.info("This is an info message")
logging.warning("This is a warning message")
logging.error("This is an error message")
logging.critical("This is a critical message")
print("Logging completed. Check 'app.log' for output.")2️⃣ Run the Script
3️⃣ Check the Output
2. Configuring Log Output
5️⃣ Logging to Both Console and File
Last updated