Python logging is very useful.

Using python logging, we can write errors to a file. But doing so requires us to come back to check the file from time to time to see if any errors occurred.

But this solution is not the best.

Here is how we can send the logs in the email. First, if you want to send from Gmail you have to generate the App password

import logging
import logging.handlers

## Enable logging
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO, filename='log_application.log')
logger = logging.getLogger(__name__)
smtp_handler = logging.handlers.SMTPHandler(mailhost=('smtp.gmail.com', 587), fromaddr='email', toaddrs=['email'], subject='Error', credentials=('email','password_App'), secure=())
logger.addHandler(smtp_handler)

If you want you can send to multiple address:

toaddrs=[‘email_1’, ‘email_2’],

In this way, you save errors in the log_application.log file, and every time an error is generated, it is sent by email.

#python #gmail #email

Python Send Email with Runtime Error using Gmail
2.65 GEEK