python使用logging模块发送邮件代码示例
logging模块不只是能记录log,还能发送邮件,使用起来非常简单方便
#coding=utf-8
'''''
Createdon2016-3-21
@author:Administrator
'''
importlogging,logging.handlers
classEncodingFormatter(logging.Formatter):
def__init__(self,fmt,datefmt=None,encoding=None):
logging.Formatter.__init__(self,fmt,datefmt)
self.encoding=encoding
defformat(self,record):
result=logging.Formatter.format(self,record)
ifisinstance(result,unicode):
result=result.encode(self.encodingor'utf-8')
returnresult
#zhangdongsheng@itouzi.com
errlog=logging.getLogger()
sh=logging.handlers.SMTPHandler("smtp.163.com",'xigongda200608@163.com','381084992@qq.com',
"loggingfrommyapp",
credentials=('xigongda200608','password'),
secure=())
errlog.addHandler(sh)
sh.setFormatter(EncodingFormatter('%(message)s',encoding='utf-8'))
errlog.error(u'追加文件时出错')
总结
以上就是本文关于python使用logging模块发送邮件代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
热门推荐
10 生日祝福语男友妈妈简短
11 公司最好的祝福语简短
12 元旦祝福语 简短独特群发
13 谢谢学校老师祝福语简短
14 送给客人简短的祝福语
15 喜得孙子祝福语简短唯美
16 朋友生闺女简短祝福语
17 交接同事退休祝福语简短
18 庆祝当生日祝福语简短