def emit(self,record):
current_time = now()
current_hour = current_time.hour
if current_hour > self.hour:
self.hour = current_hour
self.sent = 0
if self.sent == self.flood_level:
# send critical error
record = LogRecord(
name = 'flood',
level = CRITICAL,
pathname = '',
lineno = 0,
msg = """Too Many Log Entries
More than %s entries have been logged that would have resulted in
emails being sent.
No further emails will be sent for log entries generated between
%s and %i:00:00
Please consult any other configured logs, such as a File Logger,
that may contain important entries that have not been emailed.
""" % (self.sent,current_time.strftime('%H:%M:%S'),current_hour+1),
args = (),
exc_info = None)
if not self.send_empty_entries and not record.msg.strip():
return
elif self.sent > self.flood_level:
# do nothing, we've sent too many emails already
return
self.sent += 1
# actually send the mail
try:
import smtplib
port = self.mailport
if not port:
port = smtplib.SMTP_PORT
smtp = smtplib.SMTP(self.mailhost, port)
msg = self.format(record)
email = MIMEText(msg)
email['Subject']=self.getSubject(record)
email['From']=self.fromaddr
email['To']=', '.join(self.toaddrs)
email['X-Mailer']='MailingLogger'
if self.username:
if self.secure is not None:
smtp.starttls(*self.secure)
smtp.login(self.username, self.password)
smtp.sendmail(self.fromaddr, self.toaddrs, email.as_string())
smtp.quit()
except:
self.handleError(record)
评论列表
文章目录