Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Trevor Cappallo
OperationalLogging
Commits
23b58585
Commit
23b58585
authored
Aug 28, 2015
by
Trevor Cappallo
Browse files
add better mailing
parent
ec8a30ab
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
22 deletions
+27
-22
lib/OperationalLogger.py
lib/OperationalLogger.py
+4
-14
lib/OperationalMail.py
lib/OperationalMail.py
+23
-8
No files found.
lib/OperationalLogger.py
View file @
23b58585
...
...
@@ -34,7 +34,6 @@ def __exit_handler(logger, notify):
logger
.
email_log
(
addresses
)
elif
'DEBUG'
in
notify
and
notify
[
'DEBUG'
]:
addresses
=
list
(
set
(
notify
[
'DEBUG'
]))
logging
.
info
(
'Sending full DEBUG email to addresses: %s'
,
', '
.
join
(
addresses
))
logger
.
email_log
(
addresses
,
level
=
logging
.
DEBUG
)
else
:
logging
.
info
(
'No errors found. Email not sent'
)
...
...
@@ -141,25 +140,16 @@ class OperationalLogger:
"""Send email to addresses with current buffered errors."""
if
from_address
is
None
:
from_address
=
self
.
__from_address
logging
.
info
(
"
Send
ing log
email
to %s"
,
addresses
)
logging
.
info
(
"
Email
ing log to
:
%s"
,
', '
.
join
(
addresses
)
)
subject_prefix
=
"ERROR"
if
level
==
logging
.
CRITICAL
:
try
:
buf
=
self
.
__critical_buffer
.
getvalue
()
except
UnicodeDecodeError
:
buf
=
self
.
__critical_buffer
.
getvalue
().
decode
(
'utf8'
)
buf
=
self
.
__critical_buffer
.
getvalue
()
subject_prefix
=
"CRITICAL"
elif
level
==
logging
.
DEBUG
:
try
:
buf
=
self
.
__debug_buffer
.
getvalue
()
except
UnicodeDecodeError
:
buf
=
self
.
__debug_buffer
.
getvalue
().
decode
(
'utf8'
)
buf
=
self
.
__debug_buffer
.
getvalue
()
subject_prefix
=
"debug"
else
:
try
:
buf
=
self
.
__error_buffer
.
getvalue
()
except
UnicodeDecodeError
:
buf
=
self
.
__error_buffer
.
getvalue
().
decode
(
'utf8'
)
buf
=
self
.
__error_buffer
.
getvalue
()
message
=
"<br />
\n
"
.
join
([
self
.
__class__
.
__colorize
(
line
)
for
line
in
buf
.
split
(
'
\n
'
)])
OperationalMail
.
send_status
(
subject
=
'[{}] Log for {}:{}'
.
format
(
subject_prefix
,
self
.
__server_name
,
os
.
path
.
basename
(
sys
.
argv
[
0
])),
...
...
lib/OperationalMail.py
View file @
23b58585
...
...
@@ -5,19 +5,22 @@
import
os
import
smtplib
import
logging
from
os.path
import
basename
from
email.mime.application
import
MIMEApplication
from
email.mime.multipart
import
MIMEMultipart
from
email.mime.text
import
MIMEText
def
send_status
(
subject
,
message
,
to_list
,
from_address
=
'no-reply@aer.com'
,
reply_to
=
'no-reply@aer.com'
,
password
=
None
,
text_attachment
=
None
,
use_monospaced
=
True
,
smtp_server
=
'smtp.aer.com'
,
def
send_status
(
subject
,
message
,
to_list
,
from_address
=
'no-reply@aer.com'
,
reply_to
=
None
,
password
=
None
,
text_attachment
=
None
,
attachments
=
None
,
use_monospaced
=
True
,
smtp_server
=
'smtp.aer.com'
,
**
kwargs
):
"""Send an email message to a given recipient list."""
mail
=
MIMEMultipart
(
'mixed'
)
mail
[
'From'
]
=
from_address
mail
[
'Subject'
]
=
subject
mail
[
'To'
]
=
', '
.
join
(
to_list
)
if
reply_to
!=
from_address
:
if
reply_to
is
not
None
and
reply_to
!=
from_address
:
mail
[
'Reply-To'
]
=
reply_to
for
key
,
value
in
kwargs
.
iteritems
():
...
...
@@ -40,13 +43,25 @@ def send_status(subject, message, to_list, from_address='no-reply@aer.com', repl
mail
.
attach
(
contentPart
)
if
text_attachment
:
attachm
ent
=
None
text_cont
ent
=
None
if
os
.
path
.
exists
(
text_attachment
):
attachm
ent
=
MIMEText
(
file
(
text_attachment
).
read
())
text_cont
ent
=
MIMEText
(
file
(
text_attachment
).
read
())
else
:
attachment
=
MIMEText
(
text_attachment
)
attachment
.
add_header
(
'Content-Disposition'
,
'attachment'
,
filename
=
'full_log.txt'
)
mail
.
attach
(
attachment
)
text_content
=
MIMEText
(
text_attachment
)
text_content
.
add_header
(
'Content-Disposition'
,
'attachment'
,
filename
=
'full_log.txt'
)
mail
.
attach
(
text_content
)
if
attachments
is
not
None
:
for
filename
in
attachments
:
attachment
=
None
if
os
.
path
.
exists
(
filename
):
with
open
(
filename
,
"rb"
)
as
f
:
attachment
=
MIMEApplication
(
f
.
read
(),
Content_Disposition
=
'attachment; filename="%s"'
%
basename
(
filename
),
Name
=
basename
(
filename
)
)
mail
.
attach
(
attachment
)
server
=
smtplib
.
SMTP
(
smtp_server
)
if
password
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment