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

yipeiwu_com6年前Python基础

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 
 
#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随机读取文件实现实例

Python随机读取文件 代码如下 import os import random rootdir = "d:\\face\\train" file_names = [] for...

python小项目之五子棋游戏

python小项目之五子棋游戏

本文实例为大家分享了python五子棋游戏的具体代码,供大家参考,具体内容如下 1.项目简介 在刚刚学习完python套接字的时候做的一个五子棋小游戏,可以在局域网内双人对战,也可以和电...

基于进程内通讯的python聊天室实现方法

本文实例讲述了基于进程内通讯的python聊天室实现方法。分享给大家供大家参考。具体如下: #!/usr/bin/env python # Added by <ctang@re...

python matplotlib 画dataframe的时间序列图实例

在python中经常会用到pandas来处理数据,最常用的数据类型是dataframe,但是有时候在dataframe有时间字段需要画时间序列图的时候会遇到一些问题,下面是我处理这个问题...

Python ldap实现登录实例代码

下面一段代码是小编给大家介绍的Python ldap实现登录实例代码,一起看看吧 ldap_config = { 'ldap_path': 'ldap://xx.xx.xx.xx...