日志模块详细介绍:
https://www.jb51.net/article/214933.htm
import logging import time import os from Base.basePath import BasePath as BP from Base.utils import read_config_ini config = read_config_ini(BP.CONFIG_FILE)['日志打印配置'] rq = time.strftime('%Y%m%d_%H', time.localtime()) + '.log' class Logger(object): '''创建logger 创建handler 定义formatter 给handler添加formatter 给logger添加handler ''' def __init__(self, name): self.name = name self.logger = logging.getLogger(self.name) # 设置日志总开关级别,级别取自配置文件 self.logger.setLevel(config['level']) # 控制台输入日志对象 self.streamHandler = logging.StreamHandler() # 指定日志文件写入路径 self.fileHandler = logging.FileHandler(os.path.join(BP.PROJECT_ROOT, "Log",rq), 'a',encoding='utf-8') # 指定总开关书写格式 self.formatter = logging.Formatter(config['formatter']) # 指定控制台日志对象级别 self.streamHandler.setLevel(config['stream_handler_level']) # 指定日志文件级别 self.fileHandler.setLevel(config['file_handler_level']) # 指定日志文件格式 self.fileHandler.setFormatter(self.formatter) # 指定控制台日志格式 self.streamHandler.setFormatter(self.formatter) # 添加handler对象给记录器 self.logger.addHandler(self.streamHandler) self.logger.addHandler(self.fileHandler) def getLogger(self): return self.logger
标签:logging,self,Pytest,日志,formatter,config,logger From: https://www.cnblogs.com/pingzi66-ww/p/17103530.html