2021-10-22 08:02:46 +01:00
|
|
|
import logging
|
|
|
|
from logging.handlers import SMTPHandler
|
|
|
|
from logging import Formatter
|
|
|
|
from flask import request
|
|
|
|
|
2021-11-14 07:58:40 +00:00
|
|
|
PROJECT = "osm-wikidata"
|
|
|
|
|
2021-10-22 08:02:46 +01:00
|
|
|
|
|
|
|
class MatcherSMTPHandler(SMTPHandler):
|
|
|
|
def getSubject(self, record): # noqa: N802
|
2021-11-14 07:58:40 +00:00
|
|
|
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}"
|
|
|
|
)
|
2021-10-22 08:02:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
class RequestFormatter(Formatter):
|
|
|
|
def format(self, record):
|
|
|
|
record.request = request
|
|
|
|
return super().format(record)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_error_mail(app):
|
2021-11-14 07:58:40 +00:00
|
|
|
if not app.config.get("ERROR_MAIL"):
|
2021-10-22 08:02:46 +01:00
|
|
|
return
|
2021-11-14 07:58:40 +00:00
|
|
|
formatter = RequestFormatter(
|
|
|
|
"""
|
2021-10-22 08:02:46 +01:00
|
|
|
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}
|
2021-11-14 07:58:40 +00:00
|
|
|
""",
|
|
|
|
style="{",
|
|
|
|
)
|
|
|
|
|
|
|
|
mail_handler = MatcherSMTPHandler(
|
|
|
|
app.config["SMTP_HOST"],
|
|
|
|
app.config["MAIL_FROM"],
|
|
|
|
app.config["ADMINS"],
|
|
|
|
app.name + " error",
|
|
|
|
)
|
2021-10-22 08:02:46 +01:00
|
|
|
mail_handler.setFormatter(formatter)
|
|
|
|
|
|
|
|
mail_handler.setLevel(logging.ERROR)
|
|
|
|
app.logger.propagate = True
|
|
|
|
app.logger.addHandler(mail_handler)
|