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

yipeiwu_com5年前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实现调用另一个路径下py文件中的函数方法总结

Python实现调用另一个路径下py文件中的函数方法总结

本文实例讲述了Python实现调用另一个路径下py文件中的函数方法。分享给大家供大家参考,具体如下: 针对这个问题,网上有很多的解决方式。其实最主要的原因是因为Python无法正确找到你...

Python中装饰器的一个妙用

好吧,我知道是大半夜……,但我还是觉得赶紧花上半个小时,把这最新的想法分享出来是值得的~直接进入正题~ 我们来模拟一个场景,需要你去抓去一个页面,然后这个页面有好多url也要分别去抓取,...

python的常见矩阵运算(小结)

python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入numpy的包。 1.numpy的导入和使用 from numpy import *;#导入nu...

详解Python nose单元测试框架的安装与使用

详解Python nose单元测试框架的安装与使用

本文介绍了Python nose单元测试框架的安装与使用 ,分享给大家,具体如下: 安装(Python2下安装) pip install nose 原理与命名规则 Nose会自动查...

简单学习Python多进程Multiprocessing

简单学习Python多进程Multiprocessing

1.1 什么是 Multiprocessing 多线程在同一时间只能处理一个任务。 可把任务平均分配给每个核,而每个核具有自己的运算空间。 1.2 添加进程 Process 与线程类似,...