From c9b8c9d96c6f6658e6345b07068edefb348f02f2 Mon Sep 17 00:00:00 2001 From: Wiktork Date: Tue, 3 Jan 2023 14:45:46 +0100 Subject: [PATCH] feat: add email error handler --- CONFIGURATION.md | 12 ++++++++++++ document_merge_service/settings.py | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 03c553b3..e5b809fa 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -76,3 +76,15 @@ Users of nginx/apache must ensure to have matching CORS configurations. * `PAGINATION_ENABLED`: whether the pagination is enabled (default: `True`) * `PAGINATION_DEFAULT_PAGE_SIZE`: the default page size if no query param (`page_size`) is given (default: `100`) * `PAGINATION_MAX_PAGE_SIZE`: the max value of the page size query param (`page_size`) (default: `1000`) + +## Email error handler +* `ENABLE_ADMIN_EMAIL_LOGGING`: enable Django to send email to admins on errors (default: `False`) +* `SERVER_MAIL`: the email address that error messages come from +* `EMAIL_HOST`: the host to use for sending email (default: `localhost`) +* `EMAIL_PORT`: port to use for the SMTP server (default: `25`) +* `EMAIL_HOST_USER`: username for the SMTP server(default: "") +* `EMAIL_HOST_PASSWORD`: password for the SMTP server user (default: "") +* `EMAIL_USE_TLS`: whether to use an implicit TLS (secure) connection when talking to the SMTP server (default: `False`) +* `ADMINS`: list of people who will get code error notifications. Items in the list should follow this example: `Test Example ,Test2 ` + +If either `EMAIL_HOST_USER` or `EMAIL_HOST_PASSWORD` is empty, Django won't attempt authentication. diff --git a/document_merge_service/settings.py b/document_merge_service/settings.py index 1751965f..0515fe17 100644 --- a/document_merge_service/settings.py +++ b/document_merge_service/settings.py @@ -227,6 +227,8 @@ def parse_admins(admins): ) # Logging +ENABLE_ADMIN_EMAIL_LOGGING = env.bool("ENABLE_ADMIN_EMAIL_LOGGING", False) + LOGGING = { "version": 1, "disable_existing_loggers": False, @@ -235,9 +237,24 @@ def parse_admins(admins): "level": "WARNING", "filters": None, "class": "logging.StreamHandler", - } + }, + "mail_admins": { + "level": "ERROR", + "filters": None, + "class": "django.utils.log.AdminEmailHandler", + }, }, "loggers": {"django": {"handlers": ["console"], "level": "WARNING"}}, } URL_PREFIX = env.str("URL_PREFIX", default="") + +# Email settings +if ENABLE_ADMIN_EMAIL_LOGGING: # pragma: no cover + LOGGING["loggers"]["django"]["handlers"].append("mail_admins") # type: ignore + SERVER_MAIL = env.str("SERVER_MAIL", default="root@localhost") + EMAIL_HOST = env.str("EMAIL_HOST", default="localhost") + EMAIL_PORT = env.int("EMAIL_PORT", default=25) + EMAIL_HOST_USER = env.str("EMAIL_HOST_USER", default="") + EMAIL_HOST_PASSWORD = env.str("EMAIL_HOST_PASSWORD", default="") + EMAIL_USE_TLS = env.bool("EMAIL_USE_TLS", default=False)