add mail error logger
This commit is contained in:
parent
359294e166
commit
105dbb4313
2 changed files with 71 additions and 3 deletions
9
run.py
9
run.py
|
@ -10,6 +10,7 @@ from stacosys.conf.config import Config, ConfigParameter
|
||||||
from stacosys.core import database
|
from stacosys.core import database
|
||||||
from stacosys.core.rss import Rss
|
from stacosys.core.rss import Rss
|
||||||
from stacosys.core.mailer import Mailer
|
from stacosys.core.mailer import Mailer
|
||||||
|
from stacosys.core.mailer import SSLSMTPHandler
|
||||||
from stacosys.interface import app
|
from stacosys.interface import app
|
||||||
from stacosys.interface import api
|
from stacosys.interface import api
|
||||||
from stacosys.interface import form
|
from stacosys.interface import form
|
||||||
|
@ -46,7 +47,7 @@ def stacosys_server(config_pathname):
|
||||||
# initialize config
|
# initialize config
|
||||||
conf = Config.load(config_pathname)
|
conf = Config.load(config_pathname)
|
||||||
logger.info(conf.__repr__())
|
logger.info(conf.__repr__())
|
||||||
|
|
||||||
# check database file exists (prevents from creating a fresh db)
|
# check database file exists (prevents from creating a fresh db)
|
||||||
db_pathname = conf.get(ConfigParameter.DB_SQLITE_FILE)
|
db_pathname = conf.get(ConfigParameter.DB_SQLITE_FILE)
|
||||||
if not os.path.isfile(db_pathname):
|
if not os.path.isfile(db_pathname):
|
||||||
|
@ -82,8 +83,14 @@ def stacosys_server(config_pathname):
|
||||||
conf.get_bool(ConfigParameter.SMTP_SSL),
|
conf.get_bool(ConfigParameter.SMTP_SSL),
|
||||||
conf.get(ConfigParameter.SMTP_LOGIN),
|
conf.get(ConfigParameter.SMTP_LOGIN),
|
||||||
conf.get(ConfigParameter.SMTP_PASSWORD),
|
conf.get(ConfigParameter.SMTP_PASSWORD),
|
||||||
|
conf.get(ConfigParameter.SITE_ADMIN_EMAIL)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# configure mailer logger
|
||||||
|
mail_handler = mailer.get_error_handler()
|
||||||
|
logger.addHandler(mail_handler)
|
||||||
|
app.logger.addHandler(mail_handler)
|
||||||
|
|
||||||
# configure scheduler
|
# configure scheduler
|
||||||
scheduler.configure(
|
scheduler.configure(
|
||||||
conf.get_int(ConfigParameter.IMAP_POLLING),
|
conf.get_int(ConfigParameter.IMAP_POLLING),
|
||||||
|
|
|
@ -3,7 +3,10 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import smtplib
|
import smtplib
|
||||||
|
import email.utils
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
|
from email.message import EmailMessage
|
||||||
|
from logging.handlers import SMTPHandler
|
||||||
|
|
||||||
from stacosys.core import imap
|
from stacosys.core import imap
|
||||||
|
|
||||||
|
@ -24,6 +27,7 @@ class Mailer:
|
||||||
smtp_ssl,
|
smtp_ssl,
|
||||||
smtp_login,
|
smtp_login,
|
||||||
smtp_password,
|
smtp_password,
|
||||||
|
site_admin_email,
|
||||||
):
|
):
|
||||||
self._imap_host = imap_host
|
self._imap_host = imap_host
|
||||||
self._imap_port = imap_port
|
self._imap_port = imap_port
|
||||||
|
@ -36,6 +40,7 @@ class Mailer:
|
||||||
self._smtp_ssl = smtp_ssl
|
self._smtp_ssl = smtp_ssl
|
||||||
self._smtp_login = smtp_login
|
self._smtp_login = smtp_login
|
||||||
self._smtp_password = smtp_password
|
self._smtp_password = smtp_password
|
||||||
|
self._site_admin_email = site_admin_email
|
||||||
|
|
||||||
def _open_mailbox(self):
|
def _open_mailbox(self):
|
||||||
return imap.Mailbox(
|
return imap.Mailbox(
|
||||||
|
@ -70,10 +75,11 @@ class Mailer:
|
||||||
if self._smtp_ssl:
|
if self._smtp_ssl:
|
||||||
s = smtplib.SMTP_SSL(self._smtp_host, self._smtp_port)
|
s = smtplib.SMTP_SSL(self._smtp_host, self._smtp_port)
|
||||||
else:
|
else:
|
||||||
s = smtplib.SMTP(self._smtp_host, self._smtp_port)
|
s = smtplib.SMTP(self._smtp_host, self._smtp_port)
|
||||||
if self._smtp_starttls:
|
if self._smtp_starttls:
|
||||||
s.starttls()
|
s.starttls()
|
||||||
s.login(self._smtp_login, self._smtp_password)
|
if self._smtp_login:
|
||||||
|
s.login(self._smtp_login, self._smtp_password)
|
||||||
s.send_message(msg)
|
s.send_message(msg)
|
||||||
s.quit()
|
s.quit()
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -87,3 +93,58 @@ class Mailer:
|
||||||
mbox.delete_message(id)
|
mbox.delete_message(id)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("delete mail exception")
|
logger.exception("delete mail exception")
|
||||||
|
|
||||||
|
def get_error_handler(self):
|
||||||
|
if self._smtp_ssl:
|
||||||
|
mail_handler = SSLSMTPHandler(
|
||||||
|
mailhost=(
|
||||||
|
self._smtp_host,
|
||||||
|
self._smtp_port,
|
||||||
|
),
|
||||||
|
credentials=(
|
||||||
|
self._smtp_login,
|
||||||
|
self._smtp_password,
|
||||||
|
),
|
||||||
|
fromaddr=self._smtp_login,
|
||||||
|
toaddrs=self._site_admin_email,
|
||||||
|
subject="Stacosys error",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
mail_handler = SMTPHandler(
|
||||||
|
mailhost=(
|
||||||
|
self._smtp_host,
|
||||||
|
self._smtp_port,
|
||||||
|
),
|
||||||
|
credentials=(
|
||||||
|
self._smtp_login,
|
||||||
|
self._smtp_password,
|
||||||
|
),
|
||||||
|
fromaddr=self._smtp_login,
|
||||||
|
toaddrs=self._site_admin_email,
|
||||||
|
subject="Stacosys error",
|
||||||
|
)
|
||||||
|
mail_handler.setLevel(logging.ERROR)
|
||||||
|
return mail_handler
|
||||||
|
|
||||||
|
|
||||||
|
class SSLSMTPHandler(SMTPHandler):
|
||||||
|
def emit(self, record):
|
||||||
|
"""
|
||||||
|
Emit a record.
|
||||||
|
|
||||||
|
Format the record and send it to the specified addressees.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
smtp = smtplib.SMTP_SSL(self.mailhost, self.mailport)
|
||||||
|
msg = EmailMessage()
|
||||||
|
msg["From"] = self.fromaddr
|
||||||
|
msg["To"] = ",".join(self.toaddrs)
|
||||||
|
msg["Subject"] = self.getSubject(record)
|
||||||
|
msg["Date"] = email.utils.localtime()
|
||||||
|
msg.set_content(self.format(record))
|
||||||
|
if self.username:
|
||||||
|
smtp.login(self.username, self.password)
|
||||||
|
smtp.send_message(msg)
|
||||||
|
smtp.quit()
|
||||||
|
except Exception:
|
||||||
|
self.handleError(record)
|
||||||
|
|
Loading…
Add table
Reference in a new issue