Add celery task maillogging

This commit is contained in:
rubenwardy 2020-12-07 18:06:34 +00:00
parent 9ead6c1481
commit f7b3f4573d
4 changed files with 51 additions and 51 deletions

View File

@ -57,8 +57,8 @@ sass(app)
if not app.debug and app.config["MAIL_UTILS_ERROR_SEND_TO"]: if not app.debug and app.config["MAIL_UTILS_ERROR_SEND_TO"]:
from .maillogger import register_mail_error_handler from .maillogger import build_handler
register_mail_error_handler(app, mail) app.logger.addHandler(build_handler(app))
from .markdown import init_app from .markdown import init_app
@ -68,7 +68,7 @@ init_app(app)
# def get_locale(): # def get_locale():
# return request.accept_languages.best_match(app.config['LANGUAGES'].keys()) # return request.accept_languages.best_match(app.config['LANGUAGES'].keys())
from . import models, tasks, template_filters from . import models, template_filters
@login_manager.user_loader @login_manager.user_loader
def load_user(user_id): def load_user(user_id):

View File

@ -36,10 +36,9 @@ class FlaskMailTextFormatter(logging.Formatter):
pass pass
class FlaskMailHTMLFormatter(logging.Formatter): class FlaskMailHTMLFormatter(logging.Formatter):
pre_template = "<h1>%s</h1><pre>%s</pre>"
def formatException(self, exc_info): def formatException(self, exc_info):
formatted_exception = logging.Handler.formatException(self, exc_info) formatted_exception = logging.Handler.formatException(self, exc_info)
return FlaskMailHTMLFormatter.pre_template % ("Exception information", formatted_exception) return "<pre>%s</pre>" % formatted_exception
def formatStack(self, stack_info): def formatStack(self, stack_info):
return "<pre>%s</pre>" % stack_info return "<pre>%s</pre>" % stack_info
@ -47,66 +46,46 @@ class FlaskMailHTMLFormatter(logging.Formatter):
# see: https://github.com/python/cpython/blob/3.6/Lib/logging/__init__.py (class Handler) # see: https://github.com/python/cpython/blob/3.6/Lib/logging/__init__.py (class Handler)
class FlaskMailHandler(logging.Handler): class FlaskMailHandler(logging.Handler):
def __init__(self, mailer, subject_template, level=logging.NOTSET): def __init__(self, send_to, subject_template, level=logging.NOTSET):
logging.Handler.__init__(self, level) logging.Handler.__init__(self, level)
self.mailer = mailer self.send_to = send_to
self.send_to = mailer.app.config["MAIL_UTILS_ERROR_SEND_TO"]
self.subject_template = subject_template self.subject_template = subject_template
self.html_formatter = None
def setFormatter(self, text_fmt, html_fmt=None): def setFormatter(self, text_fmt):
""" """
Set the formatters for this handler. Provide at least one formatter. Set the formatters for this handler. Provide at least one formatter.
When no text_fmt is provided, no text-part is created for the email body. When no text_fmt is provided, no text-part is created for the email body.
""" """
assert (text_fmt, html_fmt) != (None, None), "At least one formatter should be provided" assert text_fmt != None, "At least one formatter should be provided"
if type(text_fmt)==str: if type(text_fmt)==str:
text_fmt = FlaskMailTextFormatter(text_fmt) text_fmt = FlaskMailTextFormatter(text_fmt)
self.formatter = text_fmt self.formatter = text_fmt
if type(html_fmt)==str:
html_fmt = FlaskMailHTMLFormatter(html_fmt)
self.html_formatter = html_fmt
def getSubject(self, record): def getSubject(self, record):
fmt = FlaskMailSubjectFormatter(self.subject_template) fmt = FlaskMailSubjectFormatter(self.subject_template)
subject = fmt.format(record) subject = fmt.format(record)
#Since templates can cause header problems, and we rather have a incomplete email then an error, we fix this # Since templates can cause header problems, and we rather have a incomplete email then an error, we fix this
if _is_bad_subject(subject): if _is_bad_subject(subject):
subject="FlaskMailHandler log-entry from %s [original subject is replaced, because it would result in a bad header]" % self.mailer.app.name subject="FlaskMailHandler log-entry from ContentDB [original subject is replaced, because it would result in a bad header]"
return subject return subject
def emit(self, record): 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 text = self.format(record) if self.formatter else None
html = self.html_formatter.format(record) if self.html_formatter else None html = "<pre>{}</pre>".format(text)
for email in self.send_to: for email in self.send_to:
send_user_email.delay(email, self.getSubject(record), text, html) send_user_email.delay(email, self.getSubject(record), text, html)
def register_mail_error_handler(app, mailer): def build_handler(app):
subject_template = "ContentDB %(message)s (%(module)s > %(funcName)s)" subject_template = "ContentDB %(message)s (%(module)s > %(funcName)s)"
text_template = """ text_template = ("Message type: %(levelname)s\n"
Message type: %(levelname)s "Location: %(pathname)s:%(lineno)d\n"
Location: %(pathname)s:%(lineno)d "Module: %(module)s\n"
Module: %(module)s "Function: %(funcName)s\n"
Function: %(funcName)s "Time: %(asctime)s\n"
Time: %(asctime)s "Message: %(message)s\n\n")
Message:
%(message)s"""
html_template = """
<style>th { text-align: right}</style><table>
<tr><th>Message type:</th><td>%(levelname)s</td></tr>
<tr> <th>Location:</th><td>%(pathname)s:%(lineno)d</td></tr>
<tr> <th>Module:</th><td>%(module)s</td></tr>
<tr> <th>Function:</th><td>%(funcName)s</td></tr>
<tr> <th>Time:</th><td>%(asctime)s</td></tr>
</table>
<h2>%(message)s</h2>"""
mail_handler = FlaskMailHandler(mailer, subject_template) mail_handler = FlaskMailHandler(app.config["MAIL_UTILS_ERROR_SEND_TO"], subject_template)
mail_handler.setLevel(logging.ERROR) mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(text_template, html_template) mail_handler.setFormatter(text_template)
app.logger.addHandler(mail_handler) return mail_handler

View File

@ -13,12 +13,13 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from logging import Filter
import flask import flask
from celery import Celery from celery import Celery, signals
from celery.schedules import crontab from celery.schedules import crontab
from app.models import * from app import app
class TaskError(Exception): class TaskError(Exception):
def __init__(self, value): def __init__(self, value):
@ -35,18 +36,18 @@ class FlaskCelery(Celery):
self.init_app(kwargs['app']) self.init_app(kwargs['app'])
def patch_task(self): def patch_task(self):
TaskBase = self.Task BaseTask : celery.Task = self.Task
_celery = self _celery = self
class ContextTask(TaskBase): class ContextTask(BaseTask):
abstract = True abstract = True
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
if flask.has_app_context(): if flask.has_app_context():
return TaskBase.__call__(self, *args, **kwargs) return super(BaseTask, self).__call__(*args, **kwargs)
else: else:
with _celery.app.app_context(): with _celery.app.app_context():
return TaskBase.__call__(self, *args, **kwargs) return super(BaseTask, self).__call__(*args, **kwargs)
self.Task = ContextTask self.Task = ContextTask
@ -83,4 +84,24 @@ CELERYBEAT_SCHEDULE = {
} }
celery.conf.beat_schedule = CELERYBEAT_SCHEDULE celery.conf.beat_schedule = CELERYBEAT_SCHEDULE
from . import importtasks, forumtasks, emails, pkgtasks from . import importtasks, forumtasks, emails, pkgtasks, celery
# noinspection PyUnusedLocal
@signals.after_setup_logger.connect
def on_after_setup_logger(**kwargs):
from app.maillogger import build_handler
class ExceptionFilter(Filter):
def filter(self, record):
if record.exc_info:
exc, _, _ = record.exc_info
if exc == TaskError:
return False
return True
logger = celery.log.get_default_logger()
handler = build_handler(app)
handler.addFilter(ExceptionFilter())
logger.addHandler(handler)

View File

@ -66,7 +66,7 @@
{% endblock %} {% endblock %}
{% block scriptextra %} {% block scriptextra %}
<script src="/static/jquery-ui.min.js"></script> <script src="/static/jquery-ui.min.js?v=2"></script>
<script> <script>
function update() { function update() {
const elements = Array.from(document.getElementsByClassName("sortable")[0].children); const elements = Array.from(document.getElementsByClassName("sortable")[0].children);