Sending emails on the occurrence of errors
It is a good idea to receive notifications when something unexpected happens with the application. Setting this up is pretty easy and adds a lot of convenience to the process of error handling.
Getting ready
We will take the application from the last recipe and add mail_handler
to it to make our application send emails when an error occurs. Also, we will demonstrate the email setup using Gmail as the SMTP server.
How to do it…
First, add the handler to the configuration in my_app/__init__.py
. This is similar to how we added file_handler
in the previous recipe:
RECEPIENTS = ['[email protected]'] if not app.debug: import logging from logging import FileHandler, Formatter from logging.handlers import SMTPHandler file_handler = FileHandler(app.config['LOG_FILE']) app.logger.setLevel...