Python的logging,记录log的包
2015-09-16 22:15
489 查看
最近在做自动化测试时,想给他加上日志,所以用到logging的模块,以下是python增加log的几种方式
一、python代码配置方式(当然还有一种是可以多模块通用的一个python代码设置,这个网上有很多例子,就不在这里赘述了)
importlogging
importlogging.handlers
#LOG_FILE是要输出的日志的文件地址
LOG_FILE=r"C:\Users\min.sun\Desktop\自动化测试\log.txt"
handler=logging.handlers.RotatingFileHandler(LOG_FILE,maxBytes=1024*1024,backupCount=5)
fmt="%(asctime)s-%(filename)s:%(lineno)s-%(name)s-%(message)s"
formatter=logging.Formatter(fmt)
handler.setFormatter(formatter)
logger=logging.getLogger('tst')
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.info('firstinfomessage')
logger.debug('firstdebugmessage')
try:
1/0
exceptExceptionase:
logger.debug(e)
print(e)
说明:log里可以包含变量,用法如下,是将name变量中的内容替换到%s上。不仅是info,其他级别的也是这么用
logger.info("这是一个变量%s",name)
二、使用配置文件
使用配置文件可以更灵活,更方便,下面是配置文件的一些使用方法
配置文件:
一、python代码配置方式(当然还有一种是可以多模块通用的一个python代码设置,这个网上有很多例子,就不在这里赘述了)
importlogging
importlogging.handlers
#LOG_FILE是要输出的日志的文件地址
LOG_FILE=r"C:\Users\min.sun\Desktop\自动化测试\log.txt"
handler=logging.handlers.RotatingFileHandler(LOG_FILE,maxBytes=1024*1024,backupCount=5)
fmt="%(asctime)s-%(filename)s:%(lineno)s-%(name)s-%(message)s"
formatter=logging.Formatter(fmt)
handler.setFormatter(formatter)
logger=logging.getLogger('tst')
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.info('firstinfomessage')
logger.debug('firstdebugmessage')
try:
1/0
exceptExceptionase:
logger.debug(e)
print(e)
说明:log里可以包含变量,用法如下,是将name变量中的内容替换到%s上。不仅是info,其他级别的也是这么用
logger.info("这是一个变量%s",name)
二、使用配置文件
使用配置文件可以更灵活,更方便,下面是配置文件的一些使用方法
配置文件:
[loggers] keys=root,example01,example02,performtest [logger_root] level=DEBUG handlers=hand01,hand02 [logger_example01] handlers=hand03 qualname=example01 propagate=0 [logger_example02] handlers=hand01,hand03 qualname=example02 propagate=0 [logger_performtest] handlers=hand03 qualname=performtest propagate=0 ############################################### [formatters] keys=form01,form02 [formatter_form01] format=%(asctime)s-[%(filename)s:%(lineno)s]-%(levelname)s-%(message)s datefmt= [formatter_form02] format=%(name)-12s:%(levelname)-8s%(message)s datefmt=%a,%d%b%Y%H:%M:%S ############################################### [handlers] keys=hand01,hand02,hand03 [handler_hand01] class=StreamHandler level=INFO formatter=form02 args=(sys.stderr,) [handler_hand02] class=FileHandler level=DEBUG formatter=form01
#要存储log的文件地址 args=(r"C:\Users\min.sun\Desktop\testlog.txt",'a') [handler_hand03] class=handlers.RotatingFileHandler level=INFO formatter=form01 args=(r"C:\Users\min.sun\Desktop\testlog.txt",'a',10*1024*1024,5)
(说明:重点注意一下handler中的class,logging提供了多种Handler,不同的代表了不同的日志存储方式,具体内容可以见我下图给出的参考)
模块中调用的方法:
importlogging
importlogging.config
logging.config.fileConfig(r"C:\Users\min.sun\Desktop\autotest\com\log.conf")
logger=logging.getLogger("example01")
logger.debug("debugmessage")
logger.info("infomessage")
logger.warn("warnmessage")
logger.error("errormessage")
logger.critical("criticalmessage"
说明:在用logging的过程中遇到了编码的问题,一个是配置文件中的存储log的文件路径中包含中文,但是执行时提示GBK的不能读取;一个是存储的log包含中文,结果存储到日志文件中后乱码。
解决方法:将log文件更改编码为UTF8无BOM格式编码。配置文件也更改为这个格式或者GBK格式。 参考文章:http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html
相关文章推荐
- Python实现的常用排序方法
- 【python学习笔记】1.python环境搭建
- python读取文件处理网页粘贴代码出现的问题
- Python+OpenCV学习(16)---视频流背景剔除
- wxpython初学者(四)之 事件驱动
- Python学习资料与博客推荐
- [Python] 专题五.列表基础知识 二维list排序、获取下标和处理txt文本实例
- Python学习笔记 (一)
- Python个人代码库-beanstalkd操作类
- python读取excel的内容
- python之web模块学习-- urlparse
- python 读取git图片每一帧
- Python 获取时间戳
- Python 获取时间戳
- Python处理JSON(转)
- python常用模块
- Python+OpenCV学习(15)---Lucas Kanade 角点光流轨迹跟踪
- 【D2】Python一周入门
- Python 进行RSA私钥加密,公钥解密
- 升级Python导致yum报错