add error mail

This commit is contained in:
Edward Betts 2019-09-29 21:14:41 +01:00
parent c2727e6fc8
commit 918a4a88c8
2 changed files with 34 additions and 0 deletions

2
app.py
View file

@ -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',

32
depicts/error_mail.py Normal file
View file

@ -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)