-
Notifications
You must be signed in to change notification settings - Fork 16
/
error_handler.py
81 lines (72 loc) · 2.96 KB
/
error_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import logging
import logging.handlers
import sys
# just use GMail
class TlsSMTPHandler(logging.handlers.SMTPHandler):
def emit(self, record):
"""
Emit a record.
Format the record and send it to the specified addressees.
"""
try:
import smtplib
import string # for tls add this line
try:
from email.utils import formatdate
except ImportError:
formatdate = self.date_time
port = self.mailport
if not port:
port = smtplib.SMTP_PORT
smtp = smtplib.SMTP(self.mailhost, port)
msg = self.format(record)
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
self.fromaddr,
self.toaddrs + ",",
self.getSubject(record),
formatdate(), msg)
if self.username:
smtp.ehlo() # for tls add this line
smtp.starttls() # for tls add this line
smtp.ehlo() # for tls add this line
smtp.login(self.username, self.password)
smtp.sendmail(self.fromaddr, self.toaddrs, msg)
smtp.quit()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
def setup_error_logging(app):
"""
~~ErrorHandler:Feature->Logging:Library~~
~~->Email:ExternalService~~
:param app:
:return:
"""
# Custom logging WILL BE IGNORED by Flask if app.debug == True -
# even if you remove the condition below.
if app.debug:
return
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
send_to = app.config.get('ERROR_LOGGING_EMAIL', app.config.get('ADMIN_EMAIL'))
if send_to and not app.config.get('SUPPRESS_ERROR_EMAILS'):
if 'ERROR_MAIL_USERNAME' in app.config and 'ERROR_MAIL_PASSWORD' in app.config and 'ERROR_MAIL_HOSTNAME' in app.config:
import platform
hostname = platform.uname()[1]
# We have to duplicate our email config here as we can't import app_email at this point
mail_handler = TlsSMTPHandler(
(app.config['ERROR_MAIL_HOSTNAME'], 587),
'server-error@' + hostname,
send_to,
'DOAJ Flask Error',
credentials=(app.config['ERROR_MAIL_USERNAME'], app.config['ERROR_MAIL_PASSWORD'])
)
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(formatter)
app.logger.addHandler(mail_handler)
# send errors to stderr, supervisord will capture them in the app's
# error log
send_errors_to_supervisor = logging.StreamHandler(sys.stderr)
send_errors_to_supervisor.setLevel(logging.ERROR)
send_errors_to_supervisor.setFormatter(formatter)
app.logger.addHandler(send_errors_to_supervisor)