定义日志的类型、级别、格式等信息。
[loggers] # 定义了三种写日志的方式:logger_root、logger_example01、logger_example02 keys=root,example01,example02 [logger_root] # 日志的颗粒度级别:critical > error > warning > info > debug level=DEBUG # 日志设置:设定了日志的格式、位置、级别等信息 handlers=hand01,hand02 [logger_example01] handlers=hand01,hand02 # 日志格式名字 qualname=example01 propagate=0 [logger_example02] handlers=hand01,hand03 qualname=example02 propagate=0 ############################################### [handlers] # 日志的设置:3种 keys=hand01,hand02,hand03 [handler_hand01] # 流模式:打印到屏幕上(标准输出,标准错误输出) class=StreamHandler # 日志级别 level=DEBUG # 日志的格式 formatter=form01 # 标准错误输出 args=(sys.stderr,) [handler_hand02] # 日志打印到文件 class=FileHandler level=DEBUG formatter=form01 # 设定日志文件位置,a表示追加打印 args=(‘e:\\AutoTestLog.log‘, ‘a‘) [handler_hand03] # 回滚日志打印 class=handlers.RotatingFileHandler level=INFO formatter=form01 args=(‘e:\\AutoTestLog.log‘, ‘a‘, 10*1024*1024, 5) # 10*1024*1024:一个日志文件最大是10m,最多打印5个文件 # 打印超过50mb的日志信息,那么会把第一个日志文件做覆盖写 ############################################### [formatters] keys=form01,form02 # form01格式,设定每一行日志的格式信息 [formatter_form01] format=%(asctime)s %(filename)s[line:%(lineno)d] 【%(levelname)s】 %(message)s # %(asctime):时间 # %(filename):当前执行的文件名 # %(lineno):当前执行代码行号 # %(levelname):日志的级别 # %(message):具体的日志信息 # 日期格式:年-月-日 时-分-秒 datefmt=%Y-%m-%d %H:%M:%S [formatter_form02] format=%(name)-12s: %(levelname)-8s %(message)s datefmt=%Y-%m-%d %H:%M:%S
format: 指定输出的格式和内容,format 可以输出很多有用信息,如下所示:
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
封装 4 种日志级别的函数。
1 import logging.config 2 import logging 3 4 5 # 写绝对路径和相对路径都可以 6 logging.config.fileConfig("Logger.conf") 7 logger = logging.getLogger("example01") 8 9 def debug(message): 10 # 打印debug级别的日志方法 11 logger.debug(message) 12 13 def warning(message): 14 # 打印warning级别的日志方法 15 logger.warning(message) 16 17 def info(message): 18 # 打印info级别的日志方法 19 logger.info(message) 20 21 def error(message): 22 # 打印error级别的日志方法 23 logger.error(message) 24 25 26 if __name__=="__main__": 27 debug("hi") 28 info("gloryroad") 29 warning("hello") 30 error("这是一个error日志")
使用工具类进行测试。
1 from selenium import webdriver 2 from LogUtil import * 3 4 5 driver = webdriver.Chrome() 6 debug("============== 开始搜索测试 ==============") 7 url = "http://www.sogou.com" 8 debug("访问网址:http://www.sogou.com") 9 driver.get(url) 10 debug("定位搜索框") 11 driver.find_element_by_id("query").send_keys("hiphop") 12 info("在输入框中输入搜索关键字串“hiphop”" + driver.find_element_by_id("query").get_attribute("value")) 13 debug("单击搜索按钮") 14 driver.find_element_by_id("stb").click() 15 debug("关闭浏览器") 16 driver.quit() 17 debug("============== 搜索测试结束 ==============")
屏幕及日志文件的打印内容:
2021-01-10 23:47:25 LogUtil.py[line:7] 【DEBUG】 ============== 开始搜索测试 ============== 2021-01-10 23:47:25 LogUtil.py[line:7] 【DEBUG】 访问网址:http://www.sogou.com 2021-01-10 23:47:27 LogUtil.py[line:7] 【DEBUG】 定位搜索框 2021-01-10 23:47:27 LogUtil.py[line:13] 【INFO】 在输入框中输入搜索关键字串“hiphop”hiphop 2021-01-10 23:47:27 LogUtil.py[line:7] 【DEBUG】 单击搜索按钮 2021-01-10 23:47:28 LogUtil.py[line:7] 【DEBUG】 关闭浏览器 2021-01-10 23:47:31 LogUtil.py[line:7] 【DEBUG】 ============== 搜索测试结束 ==============
遗留问题:文件名及行号仍然对应工具类。
原文:https://www.cnblogs.com/juno3550/p/14260291.html