Python Logging Handlers — ELI5
Imagine you write a letter. Before you send it, you choose: hand it to the person, put it in a mailbox, or email it. Each choice is a different handler — a delivery route for your message.
Python logging works the same way. Your code creates messages about what’s happening (errors, warnings, progress notes). Handlers decide where those messages go.
StreamHandler prints to your screen — like reading the letter out loud.
FileHandler saves to a file — like tucking the letter in a filing cabinet.
RotatingFileHandler saves to a file but starts a new one when it gets too big — like flipping to a new notebook page so the old one doesn’t overflow.
SMTPHandler emails the message to someone — like mailing the letter across the country, useful when something critical breaks at 3 AM.
You can attach several handlers to one logger. That means a single “Something went wrong!” message can show up on your screen and get saved to a file and trigger an email — all at once.
One thing to remember: A handler is just a delivery address. Pick the right ones and your messages always reach the people (or files) that need them.
See Also
- Python Alerting Patterns Alerting is a smoke detector for your code — it wakes you up when something is burning, not when someone is cooking.
- Python Correlation Ids Correlation IDs are name tags for requests — they let you follow one visitor's journey through a crowded theme park of services.
- Python Grafana Dashboards Python Grafana turns boring numbers from your Python app into colorful, real-time dashboards — like a car's dashboard but for your code.
- Python Log Aggregation Elk ELK collects scattered log files from all your services into one searchable place — like gathering every sticky note in the office into a single filing cabinet.
- Python Logging Best Practices Treat logs like a flight recorder so you can understand failures after they happen, not just during development.