diff --git a/depicts/error_mail.py b/depicts/error_mail.py index 3ffa196..1253a6a 100644 --- a/depicts/error_mail.py +++ b/depicts/error_mail.py @@ -1,6 +1,7 @@ import logging from logging.handlers import SMTPHandler from logging import Formatter +from flask import request PROJECT = 'depicts' @@ -10,22 +11,32 @@ class MatcherSMTPHandler(SMTPHandler): if (record.exc_info and record.exc_info[0]) 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): + 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'], 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.setFormatter(formatter) mail_handler.setLevel(logging.ERROR) app.logger.propagate = True diff --git a/depicts/utils.py b/depicts/utils.py index 48c3ae7..7c83f1e 100644 --- a/depicts/utils.py +++ b/depicts/utils.py @@ -84,7 +84,10 @@ def format_time(time_value, precision): # can't be represented as python datetime year = int(time_value[:time_value.find('-', 1)]) else: - t = datetime.strptime(time_value[1:], "%Y-%m-%dT%H:%M:%SZ") + try: + t = datetime.strptime(time_value[1:], "%Y-%m-%dT%H:%M:%SZ") + except ValueError: + return time_value year = t.year if precision == 9: