1.log类的写法:
import logging import os.path import time class Logger(object): def __init__(self, logger): """ 指定保存日志的文件路径,日志级别,以及调用文件 将日志存入到指定的文件中 :param logger: """ # 拼接日志文件夹,如果不存在则自动创建 cur_path = os.path.dirname(os.path.realpath(__file__)) log_path = os.path.join(os.path.dirname(cur_path), 'logs') if not os.path.exists(log_path):os.mkdir(log_path) # 创建一个logger self.logger = logging.getLogger(logger) self.logger.setLevel(logging.DEBUG) # 创建一个handler,用于写入日志文件 rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time())) # log_path = os.path.dirname(os.getcwd()) + '/Logs/' # log_name = log_path + rq + '.log' log_name = os.path.join(log_path,'%s.log ' %rq) fh = logging.FileHandler(log_name) fh.setLevel(logging.INFO) # 再创建一个handler,用于输出到控制台 ch = logging.StreamHandler() ch.setLevel(logging.INFO) # 定义handler的输出格式 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) # 给logger添加handler self.logger.addHandler(fh) self.logger.addHandler(ch) def getlog(self): return self.logger
调用并测试log
import time from selenium import webdriver from autoFramewrok.utils.log import Logger logger = Logger(logger='TestMylog').getlog() class TestMylog(object): def print_log(self): driver = webdriver.Chrome() # logger.info("打开浏览器") driver.maximize_window() logger.info("最大化浏览器窗口。") driver.implicitly_wait(8) driver.get("https://www.baidu.com") logger.info("打开百度首页。") time.sleep(1) logger.info("暂停一秒。") driver.quit() logger.info("关闭并退出浏览器。") testlog = TestMylog() testlog.print_log()