Skip to content

Commit

Permalink
⚡ Update(handlers) email_handler to get 'USE_TEMPLATE' option
Browse files Browse the repository at this point in the history
with this option users can choose to have template email or not
  • Loading branch information
MEHRSHAD-MIRSHEKARY committed Aug 18, 2024
1 parent 897272a commit 598e3cf
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion django_logging/handlers/email_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@
class EmailHandler(logging.Handler):
def __init__(self, include_html=False, *args, **kwargs):
super().__init__(*args, **kwargs)

self.include_html = include_html

try:
# Attempt to retrieve the logging settings from the Django settings
logging_settings = settings.DJANGO_LOGGING

self.include_html = logging_settings.get("LOG_EMAIL_NOTIFIER", {}).get("USE_TEMPLATE", include_html)

except AttributeError:
logging.warning(
f"DJANGO_LOGGING settings not found. Using default include_html value: {self.include_html}.")
except Exception as e:
logging.error(f"An unexpected error occurred while initializing EmailHandler: {e}")

def emit(self, record):
try:
request = getattr(record, "request", None)
Expand All @@ -20,7 +33,7 @@ def emit(self, record):
if self.include_html:
email_body = self.render_template(log_entry, request)
else:
email_body = log_entry # Use plain text format if HTML is not included
email_body = log_entry

subject = f"New Log Record: {record.levelname}"
send_email_async(subject, email_body, [settings.ADMIN_EMAIL])
Expand Down

0 comments on commit 598e3cf

Please sign in to comment.