Skip to content

Commit

Permalink
improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jabelone committed Sep 22, 2023
1 parent bb45af6 commit 0db048a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
8 changes: 6 additions & 2 deletions memberportal/membermatters/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
41 changes: 33 additions & 8 deletions memberportal/services/emails.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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(
Expand All @@ -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,
Expand Down

0 comments on commit 0db048a

Please sign in to comment.