当前位置:网站首页>unittest自动测试多个用例时,logging模块重复打印解决
unittest自动测试多个用例时,logging模块重复打印解决
2022-06-30 19:55:00 【执笔写回憶】
网上好多说是用这个方法,但没有解决:
logger.propagate = False
成功解决方法, 如果当前日志器里handler已存在就不重复添加,为空再添加,避免日志重复输出:
import logging,os,sys
class Log_info(object):
def __init__(self,fileName):
path = os.path.dirname(os.path.dirname(__file__))
try:
self.logs_path = path + r'/'+ fileName + '/'
except:
print("未找到指定文件夹:%s,请核实!" % fileName)
os._exit(0)
# 指定日志文件名,获取当前执行的py文件名,将日志存放到指定test_logs文件夹中
self.filename = self.logs_path + str(os.path.basename(sys.argv[0]).split(".")[0]) + '.log'
# 指定输出的格式和内容
self.format = '%(asctime)s [%(filename)s]-<%(funcName)s> %(levelname)s:%(message)s'
# 指定时间格式
self.datefmt = '%Y-%m-%d %H:%M:%S'
self.logger = logging.getLogger(__name__)
def main(self):
self.logger.setLevel(logging.INFO)
format = logging.Formatter(self.format,datefmt=self.datefmt)
# 如果当前日志器里handler已存在就不重复添加,为空再添加,避免日志重复输出
if not self.logger.handlers:
# 日志输出到文件
file_handler = logging.FileHandler(self.filename)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(format)
# 使用StreamHandler输出到屏幕
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(format)
# 添加两个Handler
self.logger.addHandler(file_handler)
self.logger.addHandler(console)
return self.logger.info边栏推荐
- Openfire在使用MySQL数据库后的中文乱码问题解决
- Halcon知识:盘点一下计量对象【1】
- 最新海康摄像机、NVR、流媒体服务器、回放取流RTSP地址规则说明[通俗易懂]
- VB的基本语法
- Jerry's touch key recognition process [chapter]
- Django上传excel表格并将数据写入数据库的详细步骤
- exness:流动性系列-流动性清洗和反转、决策区间
- Redis ziplist 压缩列表的源码解析
- Spark - 一文搞懂 Partitioner
- Solve the problems of Devops landing in complex environment with various tools with full stack and full function solutions
猜你喜欢

MySQL master-slave synchronization

What is the difference between tolocal8bit and toutf8() in QT

分析超700万个研发需求发现,这八大编程语言才是行业最需要的

Maya House Modeling

TorchDrug--药物属性预测
Application of JDBC in performance test

Transport layer uses sliding window to realize flow control

exness:流动性系列-流动性清洗和反转、决策区间

Cv+deep learning network architecture pytoch recurrence series basenets (backbones) (I)

To eliminate bugs, developers must know several bug exploration and testing artifacts.
随机推荐
静态类使用@Resource注解注入
Basic syntax of VB
TorchDrug--药物属性预测
Maya house modeling
微信小程序开发实战 云音乐
Convert seconds to * * hours * * minutes
Why should offline stores do new retail?
[iccv 2019] characteristics precise supervision of feature super resolution for small object detection
Smarter! Airiot accelerates the upgrading of energy conservation and emission reduction in the coal industry
Lingyun going to sea | 10 leap &huawei cloud: jointly helping Africa with inclusive financial services
Description of the latest RTSP address rules for Hikvision camera, NVR, streaming media server, playback and streaming [easy to understand]
Black apple server system installation tutorial, black apple installation tutorial, teach you how to install black apple in detail [easy to understand]
Primary school, session 3 - afternoon: Web_ xxe
GeoServer安装
How to pass the PMP Exam quickly?
Jerry's touch key recognition process [chapter]
Spark - 一文搞懂 Partitioner
Jerry's touch key recognition process [chapter]
HeartBeat基于CRM使用NFS对MySQL高可用
如何快速通过PMP考试?