python使用logging模块发送邮件代码示例
logging模块不只是能记录log,还能发送邮件,使用起来非常简单方便
#coding=utf-8 ''''' Created on 2016-3-21 @author: Administrator ''' import logging, logging.handlers class EncodingFormatter(logging.Formatter): def __init__(self, fmt, datefmt=None, encoding=None): logging.Formatter.__init__(self, fmt, datefmt) self.encoding = encoding def format(self, record): result = logging.Formatter.format(self, record) if isinstance(result, unicode): result = result.encode(self.encoding or 'utf-8') return result #[email protected] errlog = logging.getLogger() sh = logging.handlers.SMTPHandler("smtp.163.com", '[email protected]', '[email protected]', "logging from my app", credentials=('xigongda200608', 'password'), secure=()) errlog.addHandler(sh) sh.setFormatter(EncodingFormatter('%(message)s', encoding='utf-8')) errlog.error(u'追加文件时出错')
总结
以上就是本文关于python使用logging模块发送邮件代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16