From 918a4a88c8b277a0ee2934b4681a90d2344b10bc Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 29 Sep 2019 21:14:41 +0100 Subject: [PATCH] add error mail --- app.py | 2 ++ depicts/error_mail.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 depicts/error_mail.py diff --git a/app.py b/app.py index 67be47b..50e22c6 100755 --- a/app.py +++ b/app.py @@ -6,6 +6,7 @@ from depicts import (utils, wdqs, commons, mediawiki, painting, saam, database, wd_catalog) from depicts.pager import Pagination, init_pager from depicts.model import DepictsItem, DepictsItemAltLabel, Edit, PaintingItem +from depicts.error_mail import setup_error_mail from requests_oauthlib import OAuth1Session from urllib.parse import urlencode from werkzeug.exceptions import InternalServerError @@ -26,6 +27,7 @@ app = Flask(__name__) app.config.from_object('config.default') database.init_db(app.config['DB_URL']) init_pager(app) +setup_error_mail(app) find_more_props = { 'P135': 'movement', diff --git a/depicts/error_mail.py b/depicts/error_mail.py new file mode 100644 index 0000000..3ffa196 --- /dev/null +++ b/depicts/error_mail.py @@ -0,0 +1,32 @@ +import logging +from logging.handlers import SMTPHandler +from logging import Formatter + +PROJECT = 'depicts' + +class MatcherSMTPHandler(SMTPHandler): + def getSubject(self, record): # noqa: N802 + return (f'{PROJECT} error: {record.exc_info[0].__name__}' + if (record.exc_info and record.exc_info[0]) + else f'{PROJECT} error: {record.pathname}:{record.lineno:d}') + +def setup_error_mail(app): + mail_handler = MatcherSMTPHandler(app.config['SMTP_HOST'], + app.config['MAIL_FROM'], + app.config['ADMINS'], + app.name + ' error') + mail_handler.setFormatter(Formatter(''' + Message type: %(levelname)s + Location: %(pathname)s:%(lineno)d + Module: %(module)s + Function: %(funcName)s + Time: %(asctime)s + + Message: + + %(message)s + ''')) + + mail_handler.setLevel(logging.ERROR) + app.logger.propagate = True + app.logger.addHandler(mail_handler)