使用logging模块发送邮件
2016-07-27 08:55
309 查看
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'追加文件时出错')
#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'追加文件时出错')
相关文章推荐
- hdoj-4135-Co-prime
- VC++中subclassdlgitem函数的功能及用法
- Nginx负载均衡的4种方案配置实例
- 实习见闻(5)——mysql解压版的安装与环境变量搭建
- 给The Lab Renderer for Unity中地形添加阴影
- iOS滑动视图通过偏移量判断显示第几个item
- linux启动时间测试
- NYOJ A+B IV 小数相加(大数问题)
- 6174问题
- 使用Python求一个全排列
- React-Native笔记
- 如何在命令行使用cov-run-desktop
- css系列(4)简介
- 第一次接触神奇的Bootstrap网格系统
- JAVA(五)Java选择与循环语句
- [Cloud Computing]Mechanisms: Trusted Platform Module
- Web项目中用mybatis配置多个数据库
- 动画----补间动画
- 尺寸规范
- CodeForces 540D Bad Luck Island