跟着博主 http://blog.csdn.net/u011541946/article/details/70198676 的文章,在python+selenium 走向自动化框架还需要学会python logging模块,进行日志类的封装,其实不太了解logging模块,所以先了解一下logging模块
一、日志级别:debug、info、warning、error、critical,notset默认级别为warning
日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET
import logging
logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything
上面代码片中warning可打印到控制台中,info不会打印,因为默认的日志级别warning
二、将日志写入文件中
import logging
import os
file_path = os.path.dirname(os.getcwd())
logging.basicConfig(filename=file_path+r'\example.txt',level=logging.DEBUG)
logging.debug("this message should go to the log file")
logging.info('include this')
logging.warning('and this')
#print(os.getcwd()) 获得当前路径
#print(os.path.dirname(os.getcwd())) 获得当前路径上一级路径
将日志文件写入文件example.txt,设置日志级别为DBUG,则info、warning级别日志都能写入文件中
三、日志输出格式及方式配置
1、默认输出格式
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
#按照格式日志级别、日志信息格式输出(感觉就像用字典格式化字符串的写法&#x