diff --git a/django_logging/handlers/email_handler.py b/django_logging/handlers/email_handler.py index 25399fa..53ea22a 100644 --- a/django_logging/handlers/email_handler.py +++ b/django_logging/handlers/email_handler.py @@ -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) @@ -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])