Add more detail to error mails.

This commit is contained in:
Edward Betts 2020-01-07 21:29:31 +00:00
parent 50dc706b32
commit 8eecad0f9e
2 changed files with 26 additions and 12 deletions

View file

@ -1,6 +1,7 @@
import logging import logging
from logging.handlers import SMTPHandler from logging.handlers import SMTPHandler
from logging import Formatter from logging import Formatter
from flask import request
PROJECT = 'depicts' PROJECT = 'depicts'
@ -10,22 +11,32 @@ class MatcherSMTPHandler(SMTPHandler):
if (record.exc_info and record.exc_info[0]) if (record.exc_info and record.exc_info[0])
else f'{PROJECT} error: {record.pathname}:{record.lineno:d}') else f'{PROJECT} error: {record.pathname}:{record.lineno:d}')
class RequestFormatter(Formatter):
def format(self, record):
record.request = request
return super().format(record)
def setup_error_mail(app): def setup_error_mail(app):
formatter = RequestFormatter('''
Message type: {levelname}
Location: {pathname:s}:{lineno:d}
Module: {module:s}
Function: {funcName:s}
Time: {asctime:s}
GET args: {request.args!r}
URL: {request.url}
Message:
{message:s}
''', style='{')
mail_handler = MatcherSMTPHandler(app.config['SMTP_HOST'], mail_handler = MatcherSMTPHandler(app.config['SMTP_HOST'],
app.config['MAIL_FROM'], app.config['MAIL_FROM'],
app.config['ADMINS'], app.config['ADMINS'],
app.name + ' error') app.name + ' error')
mail_handler.setFormatter(Formatter(''' 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) mail_handler.setLevel(logging.ERROR)
app.logger.propagate = True app.logger.propagate = True

View file

@ -84,7 +84,10 @@ def format_time(time_value, precision):
# can't be represented as python datetime # can't be represented as python datetime
year = int(time_value[:time_value.find('-', 1)]) year = int(time_value[:time_value.find('-', 1)])
else: else:
try:
t = datetime.strptime(time_value[1:], "%Y-%m-%dT%H:%M:%SZ") t = datetime.strptime(time_value[1:], "%Y-%m-%dT%H:%M:%SZ")
except ValueError:
return time_value
year = t.year year = t.year
if precision == 9: if precision == 9: