Block 133: Logging Best Practices
Use Python's logging module for professional script output.
Concepts
- import logging, basicConfig
- Log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL
- Logging to file and console simultaneously
- Log formatting with timestamps and filenames
Code Examples
See exercise below.
Exercise
Add logging to yesterday's file organizer script. Log every file operation at INFO level and every error at ERROR level. Set up a logger that writes to both console and 'app.log' file.
Homework
Why is print() inadequate for production scripts? List 4 advantages of logging.