Welcome to the “Logging in Python” tutorial!
Overview
Use the logging
module to record events instead of print()
.
Basic Configuration
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s:%(message)s'
)
logging.debug("Debug message")
logging.info("Info message")
logging.warning("Warning message")
logging.error("Error message")
logging.critical("Critical message")
Writing to a File
logging.basicConfig(
filename='app.log',
level=logging.DEBUG,
format='%(asctime)s %(levelname)s:%(message)s'
)
Log Levels
- DEBUG
- INFO
- WARNING
- ERROR
- CRITICAL
Key Takeaways
- Prefer
logging
overprint
for production. - Configure level and format once at startup.
- Log to files for persistent records.
Happy coding!
Comments
Post a Comment