From 9f62c251f21a41649a86db411f6bc2c0b9553713 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Thu, 3 Dec 2020 22:59:13 +0000 Subject: [PATCH] Fix error emails not preserving whitespace --- app/maillogger.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/maillogger.py b/app/maillogger.py index 10a046a..f3cbf87 100644 --- a/app/maillogger.py +++ b/app/maillogger.py @@ -34,14 +34,13 @@ class FlaskMailSubjectFormatter(logging.Formatter): class FlaskMailTextFormatter(logging.Formatter): pass -# TODO: hier nog niet tevreden over (vooral logger.error(..., exc_info, stack_info)) class FlaskMailHTMLFormatter(logging.Formatter): pre_template = "

%s

%s
" def formatException(self, exc_info): formatted_exception = logging.Handler.formatException(self, exc_info) return FlaskMailHTMLFormatter.pre_template % ("Exception information", formatted_exception) def formatStack(self, stack_info): - return FlaskMailHTMLFormatter.pre_template % ("

Stack information

%s
", stack_info) + return "
%s
" % stack_info # see: https://github.com/python/cpython/blob/3.6/Lib/logging/__init__.py (class Handler) @@ -76,13 +75,17 @@ class FlaskMailHandler(logging.Handler): return subject def emit(self, record): + record.stack_info = record.exc_text + record.exc_text = None + record.exc_info = None + text = self.format(record) if self.formatter else None html = self.html_formatter.format(record) if self.html_formatter else None sendEmailRaw.delay(self.send_to, self.getSubject(record), text, html) def register_mail_error_handler(app, mailer): - subject_template = "ContentDB crashed (%(module)s > %(funcName)s)" + subject_template = "ContentDB %(message)s (%(module)s > %(funcName)s)" text_template = """ Message type: %(levelname)s Location: %(pathname)s:%(lineno)d @@ -99,10 +102,8 @@ Message: Function:%(funcName)s Time:%(asctime)s -

Message

-
%(message)s
""" +

%(message)s

""" - import logging mail_handler = FlaskMailHandler(mailer, subject_template) mail_handler.setLevel(logging.ERROR) mail_handler.setFormatter(text_template, html_template)