__author__ = "starry"
# 定义一个log类
from logging import handlers
import logging
import os
class Logger(object):
#封装log级别
level_relations = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL
}
# fileName log生成的路径, level log的级别, fmt是log 内容的格式, when 时间间隔单位, backCount 保留几个分割后的日志文件
def __init__(self,fileName = "./log/server automation.log",when = 'D',level = 'debug',backCount = 3, fmt = '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'):
if not os.path.exists("./log/"):
os.makedirs("./log/")
self.logger = logging.getLogger(fileName)
format_str = logging.Formatter(fmt)
self.logger.setLevel(self.level_relations.get(level))
# log输出到控制台
self.sh = logging.StreamHandler()
self.sh.setFormatter(format_str)
# log输出到文件
self.th = handlers.TimedRotatingFileHandler(filename = fileName,backupCount = backCount,when = when,encoding='utf-8' )
self.th.setFormatter(format_str)
# 添加控制台和文件的句柄
self.logger.addHandler(self.sh)
self.logger.addHandler(self.th)
class searchFile(unittest.TestCase):
def setUp(self) -> None:
# 生成一个log对象
self.log = Logger.Logger()
self.logger = self.log.logger
# 打印出log
self.logger.debug("<------------------------------------------------------------------------------------------------------->")
self.logger.debug("<----------------------------------------------test search file----------------------------------------->")
self.logger.debug("<------------------------------------------------------------------------------------------------------->")
self.search = searchOperation.search_operation(self.driver,self.logger)
我的博客主页:https://blog.csdn.net/sinat_38251543