加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

python使用logging模块发送邮件代码示例

发布时间:2020-12-15 01:03:41 所属栏目:C语言 来源:网络整理
导读: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=No

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,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 
 
#zhangdongsheng@itouzi.com 
errlog = logging.getLogger() 
sh = logging.handlers.SMTPHandler("smtp.163.com",'xigongda200608@163.com','381084992@qq.com',"logging from my app",credentials=('xigongda200608','password'),secure=()) 
errlog.addHandler(sh) 
sh.setFormatter(EncodingFormatter('%(message)s',encoding='utf-8')) 
errlog.error(u'追加文件时出错') 

总结

以上就是本文关于python使用logging模块发送邮件代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

您可能感兴趣的文章:

  • python logging 日志轮转文件不删除问题的解决方法
  • 详解Python中logging日志模块在多进程环境下的使用
  • 详解使用python的logging模块在stdout输出的两种方法
  • python中logging库的使用总结
  • python中 logging的使用详解
  • python logging日志模块的详解
  • Python logging管理不同级别log打印和存储实例

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读