| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- '''
- Created on 6 juil. 2017
- @author: olivier.massot
- '''
- from email.mime.text import MIMEText
- import logging.config
- import smtplib
- from path import Path
- import yaml
- LOG_DIR = Path(__file__).parent / "logs"
- LOG_DIR.makedirs_p()
- def getLogger(name):
- # charge la configuration du logging depuis le fichier 'logging.yaml'
- configfile = Path(__file__).parent
- with open(configfile / 'logging.yaml', 'rt') as f:
- conf = yaml.load(f)
- logging.config.dictConfig(conf)
- return logging.getLogger(name)
- # ******************************
- class BufferingSMTPHandler(logging.handlers.BufferingHandler):
- def __init__(self, mailhost, fromaddr, toaddrs, subject, capacity):
- logging.handlers.BufferingHandler.__init__(self, capacity)
- self.mailhost = mailhost
- self.mailport = None
- self.fromaddr = fromaddr
- self.toaddrs = toaddrs
- self.subject = subject
- def flush(self):
- try:
- if len(self.buffer) > 0:
- port = self.mailport if self.mailport else smtplib.SMTP_PORT
- msg = "\n".join([self.format(record) for record in self.buffer])
- msg = MIMEText(msg.encode('utf-8'), _charset='utf-8')
- msg['Subject'] = self.subject
- msg['From'] = self.fromaddr
- msg['To'] = ",".join(self.toaddrs)
- smtp = smtplib.SMTP(self.mailhost, port)
- smtp.sendmail(self.fromaddr, self.toaddrs, msg.as_string())
- smtp.quit()
- self.buffer = []
- except Exception as e:
- print(e)
- raise
|