-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextra_utils.py
131 lines (109 loc) · 4.95 KB
/
extra_utils.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import smtplib
from email.mime.text import MIMEText
import os,datetime,socket,sys
import logging,logging.handlers
##############################
# Custom formatter
# http://stackoverflow.com/questions/1343227/can-pythons-logging-format-be-modified-depending-on-the-message-log-level
class MyFormatter(logging.Formatter):
err_fmt = '\n\n# %(asctime)s %(levelname)s:%(name)s: %(message)s\n\n'
warning_fmt = '\n# %(asctime)s %(levelname)s:%(name)s: %(message)s\n'
other_fmt = '# %(asctime)s %(levelname)s:%(name)s: %(message)s'
def __init__(self, fmt=other_fmt):
logging.Formatter.__init__(self, fmt)
def format(self, record):
# Save the original format configured by the user
# when the logger formatter was instantiated
format_orig = self._fmt
# Replace the original format with one customized by logging level
if record.levelno == logging.WARNING:
self._fmt = MyFormatter.warning_fmt
elif record.levelno >= logging.ERROR:
self._fmt = MyFormatter.err_fmt
# Call the original formatter class to do the grunt work
result = logging.Formatter.format(self, record)
# Restore the original format configured by the user
self._fmt = format_orig
return result
######################################################################
def makelogger(name):
fmt = MyFormatter()
# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.DEBUG,
datefmt='%Y-%m-%d %H:%M:%S',
filename='/dev/null')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.WARNING)
console.setFormatter(fmt)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
filehandler = logging.handlers.RotatingFileHandler(name + '.log',
backupCount=10)
filehandler.setLevel(logging.DEBUG)
filehandler.setFormatter(fmt)
filehandler.doRollover()
logging.getLogger('').addHandler(filehandler)
return logging.getLogger(name)
######################################################################
class ExitHandler():
"""
a handler for exiting from a script
will do various cleanup and notification items on exit
including sending an email if desired
"""
def __init__(self,
name,
email=None):
self.name=name
self.email=email
def sendemail(self):
if self.email is None:
return
fromaddr='%s@%s' % (self.name,socket.gethostname())
toaddr=self.email
# check if we are in a screen
if os.environ.has_key('STY'):
subject='%s (PID=%s, STY=%s) finished at %s with exit code %d' % (self.name,
os.getpid(),
os.environ['STY'],
datetime.datetime.now(),
self.code)
else:
subject='%s (PID=%s) finished at %s with exit code %d' % (self.name,
os.getpid(),
datetime.datetime.now(),
self.code)
logfile=logging.getLogger('').handlers[2].baseFilename
f=open(logfile)
lines=f.readlines()
f.close()
# filter out some extra aegean output if needed
goodlines=[]
for line in lines:
if not 'DEBUG:Aegean' in line:
goodlines.append(line)
msg=MIMEText(''.join(goodlines))
msg['Subject']=subject
msg['To']=toaddr
msg['From']=fromaddr
server = smtplib.SMTP('localhost')
try:
server.sendmail(fromaddr, [toaddr], msg.as_string())
logging.getLogger('').info('Sent email to %s' % toaddr)
except Exception,e:
logging.getLogger('').error('Cannot send notification email to %s:\n\t%s' % (toaddr,e))
msg=MIMEText('[message text too large]')
msg['Subject']=subject
msg['To']=toaddr
msg['From']=fromaddr
try:
server.sendmail(fromaddr, [toaddr], msg.as_string())
logging.getLogger('').info('Sent truncated email to %s' % toaddr)
except Exception,e:
logging.getLogger('').error('Cannot send truncated notification email to %s:\n\t%s' % (toaddr,e))
server.quit()
def exit(self, code):
self.code=code
self.sendemail()
sys.exit(code)