I want to send an email and send the error to Amazon Cloudwatch with every exception. Right now I am doing like this:
import watchtower
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.addHandler(watchtower.CloudWatchLogHandler(
log_group_name="example",
log_stream_name="example")
)
def send_email(exception):
blabla
try:
something
except Exception as e:
logger.exception(f"ERROR {e}")
send_email(e)
raise
I was wondering if somehow I can tell python to use my custom logger by default and also send the email function in every exception, so that I don't have to code a lot of try except with the same structure.