logging获取日志
获取日志并保存有两种写法,分别如下
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-07-03 16:36 # @File : logging_test.py ''' logging获取日志 longging模块包括logger,Handler,Filter,Formatter logger:记录器,用于设置日志采集 Handler:处理器,将日志发送到合适的路径 Filter:过滤器,提供了更好的粒度控制,他可以决定输出那些日志记录 Formatter:格式化器,指明了最重输出中的日志的格式 basicConfig(**kwargs)为日志记录系统做基本配置 ''' import logging # logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.INFO,filename='runlog.log', format='%(asctime)s %(filename)s[line:%(lineno)s %(levelname)s %(message)s]') #filename可以设置生成的日志保存路径,不存在的会新创建 #format指明输出格式 logging.debug('debug info') logging.info('hello world') logging.warning('waining info') logging.error('error info') logging.critical('critical')
另一种需要先写一个.conf文件用于存放日志格式等
log.conf
[loggers] keys=root,infoLogger [logger_root] level=DEBUG handlers=consoleHandler,fileHandler [logger_infoLogger] handlers=consoleHandler,fileHandler qualname=infoLogger propagate=0 [handlers] keys=consoleHandler,fileHandler [handler_consoleHandler] class=StreamHandler level=INFO formatter=form02 args=(sys.stdout,) [handler_fileHandler] class=FileHandler level=INFO formatter=form01 args=('runlog_conf.log', 'a') [formatters] keys=form01,form02 [formatter_form01] format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s [formatter_form02] format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-07-03 11:05 # @Author : zhouyang # @File : capability_yaml.py ''' 从desired_caps.yaml文件中获取capability数据,登录考研帮app,把日志保存在文件中 ''' from appium import webdriver import yaml import logging import logging.config from selenium.common.exceptions import NoSuchElementException file=open('../yaml/desired_caps.yaml','r') data=yaml.load(file) CON_LOG='log.conf' logging.config.fileConfig(CON_LOG) logging=logging.getLogger() desired_caps={} desired_caps['platformName']=data['platformName'] desired_caps['platformVerion']=data['platformVersion'] desired_caps['deviceName']=data['deviceName'] desired_caps['app']=data['app'] desired_caps['noReset']=data['noReset'] desired_caps['appPackage']=data['appPackage'] desired_caps['appActivity']=data['appActivity'] logging.info('start info...') driver=webdriver.Remote('http://'+str(data['ip'])+':'+str(data['port'])+'/wd/hub',desired_caps) def check_cancelBtn(): logging.info('start check cancelBtn') try: cancelBtn = driver.find_element_by_id('android:id/button2') except NoSuchElementException: logging.info('no cancelBtn') else: cancelBtn.click() def check_skipBtn(): logging.info('start check skipBtn') try: skipBtn = driver.find_element_by_id('com.tal.kaoyan:id/tv_skip') except NoSuchElementException: logging.info('no skipBtn') else: skipBtn.click() check_cancelBtn() check_skipBtn()