logging日志在终端显示的日志缺失,在.log文件和.html报告中正常
在搭建python+request+unittest自动化接口框架时,发现在终端运行时(cmd或Linux或Jenkins)执行时日志缺失,(其他库用到:openpyxl、logging、ddt、HtmlTestRunner等)from config.filePath import logPath
import logging
from basework.getConfig import getConfig
class getLogging():
def __init__(self):
self.gc = getConfig()
self.loglevel = self.gc.getLoglevel()
# 判断日志等级
if self.loglevel == 'info':
self.level = logging.INFO
elif self.loglevel == 'error':
self.level = logging.ERROR
elif self.loglevel == 'debug':
self.level = logging.DEBUG
elif self.loglevel == 'warn':
self.level = logging.WARN
def getlogs(self,logger='log',filehandler=True,streamhandler=True):
self.logger = logging.getLogger(logger)
self.logger.handlers.clear()
self.logger.setLevel(self.level)
self.logname = logPath
self.fh = logging.FileHandler(self.logname, encoding='utf-8')
self.fh.setLevel(self.level)
# 控制台输出
self.ch = logging.StreamHandler()
self.ch.setLevel(self.level)
# 日志格式
formatter = logging.Formatter('[%(asctime)s] [%(levelname)s]>>> %(message)s')
self.fh.setFormatter(formatter)
self.ch.setFormatter(formatter)
#根据配置是否显示到终端和写入文件
filehandler, streamhandler=self.gc.getLogHandler()
if filehandler == True:
self.logger.addHandler(self.fh)
else:
pass
if streamhandler == True:
self.logger.addHandler(self.ch)
else:
pass
self.fh.close()
self.ch.close()
return self.logger
if __name__ == '__main__':
lg = getLogging().getlogs('log')
lg.info('testlog')
print('aaaaa', lg.info('testlog'))
运行结果及报错内容在pycharm中运行日志在cmd中运行日志在HTML报告中的日志在log文件中的日志执行测试用例函数的文件,在这个文件运行,终端显示的日志是全的用例类所继承的类最终执行的文件不知道问题出现在哪?
页:
[1]