From 0db048a7557d94c1cf0a2d22dd3d9aa181198d68 Mon Sep 17 00:00:00 2001 From: Jaimyn Mayer Date: Fri, 22 Sep 2023 10:13:14 +1000 Subject: [PATCH] improved logging --- memberportal/membermatters/settings.py | 8 +++-- memberportal/services/emails.py | 41 +++++++++++++++++++++----- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/memberportal/membermatters/settings.py b/memberportal/membermatters/settings.py index 8566ee52..b540d006 100644 --- a/memberportal/membermatters/settings.py +++ b/memberportal/membermatters/settings.py @@ -199,8 +199,12 @@ "version": 1, "disable_existing_loggers": True, "formatters": { - "console": {"format": "%(asctime)s %(name)s %(levelname)-8s %(message)s"}, - "file": {"format": "%(asctime)s %(name)s %(levelname)-8s %(message)s"}, + "console": { + "format": "%(asctime)s %(name)s %(filename)s:%(lineno)s %(levelname)-8s %(message)s" + }, + "file": { + "format": "%(asctime)s %(name)s %(filename)s:%(lineno)s %(levelname)-8s %(message)s" + }, }, "handlers": { "console": { diff --git a/memberportal/services/emails.py b/memberportal/services/emails.py index dfd60ea4..86037499 100644 --- a/memberportal/services/emails.py +++ b/memberportal/services/emails.py @@ -1,7 +1,11 @@ from django.template.loader import render_to_string from django.utils.html import escape from constance import config -from postmarker.core import PostmarkClient +from postmarker.core import PostmarkClient, ClientError +import logging +import json + +logger = logging.getLogger("app") def send_single_email( @@ -15,6 +19,8 @@ def send_single_email( # TODO: move to celery template_to_use = template_name if template_name else "email_without_button.html" + logger.debug("Using email template: " + template_to_use) + logger.debug("Using template vars: " + json.stringify(template_vars)) if template_vars.get("message"): template_vars["message"] = escape(template_vars["message"]).replace( @@ -27,21 +33,40 @@ def send_single_email( if config.POSTMARK_API_KEY: postmark = PostmarkClient(server_token=config.POSTMARK_API_KEY) - postmark.emails.send( - From=config.EMAIL_DEFAULT_FROM, - To=to_email, - Subject=subject, - HtmlBody=email_string, - ReplyTo=reply_to or config.EMAIL_DEFAULT_FROM, - ) + try: + postmark.emails.send( + From=config.EMAIL_DEFAULT_FROM, + To=to_email, + Subject=subject, + HtmlBody=email_string, + ReplyTo=reply_to or config.EMAIL_DEFAULT_FROM, + ) + except ClientError as e: + code = e.error_code + + if code == 406: + if user: + logger.warning( + f"Email NOT sent because recipient is INACTIVE in postmark" + ) + user.log_event( + "Email NOT sent because recipient is INACTIVE in postmark: ", + "email", + "Email content: " + template_vars, + ) + else: + logger.error("Error sending email: " + str(e)) + raise e if user: + logger.info("Email sent to " + to_email + " with subject: " + subject) user.log_event( "Sent email with subject: " + subject, "email", "Email content: " + template_vars.get("message"), ) else: + logger.warning("No postmark API key set, not sending email") if user: user.log_event( "Email NOT sent due to configuration issue: " + subject,