Explorar o código

优化日志显示
添加响应时间判断

yunwinLiu hai 5 meses
pai
achega
1bf45d738a
Modificáronse 10 ficheiros con 120 adicións e 65 borrados
  1. 9 0
      .gitignore
  2. 1 1
      DingDing.py
  3. 63 32
      common/log_util.py
  4. 10 1
      common/request_util.py
  5. 3 0
      common/yaml_util.py
  6. 13 2
      config.yaml
  7. 1 15
      extract.yaml
  8. 6 0
      test_case/__init__.py
  9. 7 7
      test_case/qccq/test_allAPI.py
  10. 7 7
      test_case/test_allAPI.py

+ 9 - 0
.gitignore

@@ -9,6 +9,9 @@ __pycache__/
 
 # Distribution / packaging
 .Python
+D:\TestPythonProject\TEST\test_case\test_allAPI.py
+logs/
+venv/
 env/
 build/
 develop-eggs/
@@ -58,3 +61,9 @@ docs/_build/
 # PyBuilder
 target/
 
+!/test_case/test_allAPI.py
+!/test_case/qccq/test_allAPI.py
+!/temps/
+!/logs/
+!/venv/
+!/.gitignore

+ 1 - 1
DingDing.py

@@ -7,7 +7,7 @@ import datetime
 import jenkins  # 安装pip install python-jenkins
 
 # 获取本机IP
-# s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 # s.connect(("8.8.8.8", 80))
 # ip = s.getsockname()[0]
 # jenkins登录地址

+ 63 - 32
common/log_util.py

@@ -1,11 +1,21 @@
 import logging
+import colorlog
 import time
 
 from common.yaml_util import get_project_path, read_config_yaml
 
 
 class LoggerUtil:
-    def create_log(self, logger_name='log'):
+    def create_log(self, logger_name='log',level='INFO'):
+
+        level_relations = {
+            'debug': logging.DEBUG,
+            'info': logging.INFO,
+            'warning': logging.WARNING,
+            'error': logging.ERROR,
+            'crit': logging.CRITICAL
+        }
+
 
         # 创建一个日志对象
         self.logger = logging.getLogger(logger_name)
@@ -19,19 +29,22 @@ class LoggerUtil:
             # 2创建文件日志控制器
             self.file_hander = logging.FileHandler(self.file_log_path, encoding='UTF-8')
             # 3设置文件日志级别
-            file_log_level = str(read_config_yaml("log", "log_level")).lower()
-            if file_log_level == "debug":
-                self.file_hander.setLevel(logging.DEBUG)
-            elif file_log_level == "info":
-                self.file_hander.setLevel(logging.INFO)
-            elif file_log_level == "warning":
-                self.file_hander.setLevel(logging.WARNING)
-            elif file_log_level == "error":
-                self.file_hander.setLevel(logging.ERROR)
-            elif file_log_level == "critical":
-                self.file_hander.setLevel(logging.CRITICAL)
-            else:
-                self.file_hander.setLevel(logging.DEBUG)
+            # file_log_level = str(read_config_yaml("log", "log_level")).lower()
+            # 设置日志显示级别
+            self.logger.setLevel(level_relations.get(level))
+
+            # if file_log_level == "debug":
+            #     self.file_hander.setLevel(logging.DEBUG)
+            # elif file_log_level == "info":
+            #     self.file_hander.setLevel(logging.INFO)
+            # elif file_log_level == "warning":
+            #     self.file_hander.setLevel(logging.WARNING)
+            # elif file_log_level == "error":
+            #     self.file_hander.setLevel(logging.ERROR)
+            # elif file_log_level == "critical":
+            #     self.file_hander.setLevel(logging.CRITICAL)
+            # else:
+            #     self.file_hander.setLevel(logging.DEBUG)
             # 4创建文件的格式
             self.file_hander.setFormatter(logging.Formatter(read_config_yaml("log", "log_format")))
             # 将文件日志的控制器加入日志对象
@@ -40,21 +53,31 @@ class LoggerUtil:
             # 1创建控制台日志控制器
             self.console_hander = logging.StreamHandler()
             # 2设置控制台日志级别
-            console_log_level = str(read_config_yaml("log", "log_level")).lower()
-            if console_log_level == "debug":
-                self.console_hander.setLevel(logging.DEBUG)
-            elif console_log_level == "info":
-                self.console_hander.setLevel(logging.INFO)
-            elif console_log_level == "warning":
-                self.console_hander.setLevel(logging.WARNING)
-            elif console_log_level == "error":
-                self.console_hander.setLevel(logging.ERROR)
-            elif console_log_level == "critical":
-                self.console_hander.setLevel(logging.CRITICAL)
-            else:
-                self.console_hander.setLevel(logging.DEBUG)
+            self.logger.setLevel(level_relations.get(level))
+
+            # console_log_level = str(read_config_yaml("log", "log_level")).lower()
+            # if console_log_level == "debug":
+            #     self.console_hander.setLevel(logging.DEBUG)
+            # elif console_log_level == "info":
+            #     self.console_hander.setLevel(logging.INFO)
+            # elif console_log_level == "warning":
+            #     self.console_hander.setLevel(logging.WARNING)
+            # elif console_log_level == "error":
+            #     self.console_hander.setLevel(logging.ERROR)
+            # elif console_log_level == "critical":
+            #     self.console_hander.setLevel(logging.CRITICAL)
+            # else:
+            #     self.console_hander.setLevel(logging.DEBUG)
+
+            # formatter = colorlog.ColoredFormatter(
+            #     '%(log_color)s[%(asctime)s] [%(name)s] [%(levelname)s]: %(message)s',
+            #     log_colors=log_colors_config)
+            formatter = colorlog.ColoredFormatter(
+                '%(log_color)s[%(asctime)s] [%(name)s] [%(levelname)s]: %(message)s',
+                log_colors=read_config_yaml("log","log_colors_config"))
+
             # 3创建控制台的日志格式
-            self.console_hander.setFormatter(logging.Formatter(read_config_yaml("log", "log_format")))
+            self.console_hander.setFormatter(formatter)
             # 将控制台日志加入日志对象
             self.logger.addHandler(self.console_hander)
 
@@ -64,14 +87,22 @@ class LoggerUtil:
 
 # 错误日志的输出
 def error_log(message):
-    LoggerUtil().create_log().error(message)
-    raise Exception(message)
+    LoggerUtil().create_log(level="error").error(message)
+    # raise Exception(message)
+
+
+def warring_log(message):
+    LoggerUtil().create_log(level="warning").warning(message)
+    # raise Exception(message)
+
 
 
 # 信息日志的输出
 def logs(message):
-    LoggerUtil().create_log().info(message)
+    LoggerUtil().create_log(level="info").info(message)
+    # raise Exception(message)
+
 
 
 if __name__ == '__main__':
-    error_log("车务通")
+    warring_log("车务通")

+ 10 - 1
common/request_util.py

@@ -5,7 +5,7 @@ import jsonpath
 import requests
 
 from common.database_util import DatabaseUtil
-from common.log_util import error_log, logs
+from common.log_util import error_log, logs, warring_log
 from common.yaml_util import write_yaml, read_yaml, read_config_yaml, write_yaml_projectCode
 from random1111 import DubugTalk
 
@@ -110,6 +110,14 @@ class RequestUtil:
                     url = arg_names['request'].pop("url")
                     # 发送请求
                     res = self.send_request(name, method, url, **arg_names['request'])
+                    #获取接口的请求时间
+                    res_time = round(res.elapsed.total_seconds() * 1000, 2)
+                    act_time = read_config_yaml("request", "request_out_time")
+                    # 判断响应时间
+                    if res_time >= act_time:
+                        warring_log("接口实际请求时间{res_time}毫秒,请求时间大于{act_time}毫秒,请关注".format(res_time = res_time,act_time = act_time))
+                    # TODO 发送通知
+
                     # print(res.json())
                     return_text = res.text
                     # print(return_text)
@@ -146,6 +154,7 @@ class RequestUtil:
 
                     # 断言结果
                     self.assert_result(arg_names['validate'], return_json, return_code)
+
                 else:
                     error_log("request下面是否包含method,url")
             else:

+ 3 - 0
common/yaml_util.py

@@ -76,3 +76,6 @@ def read_case(yaml_path):
     with open(os.getcwd() + yaml_path, mode='r', encoding='utf-8') as f:
         value = yaml.load(stream=f, Loader=yaml.FullLoader)
         return value
+
+if __name__ == '__main__':
+    print(os.path.abspath(os.getcwd().split("common")[0]))

+ 13 - 2
config.yaml

@@ -6,5 +6,16 @@ base:
 #日志配置
 log:
   log_name: logs_
-  log_level: info
-  log_format: '[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s] %(message)s'
+  log_format: '[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s] %(message)s'
+  log_colors_config:
+    DEBUG: 'cyan'
+    INFO: 'green'
+    WARNING: 'yellow'
+    ERROR: 'red'
+    CRITICAL: 'red'
+
+
+request:
+  request_out_time: 1000
+
+

+ 1 - 15
extract.yaml

@@ -1,15 +1 @@
-token: PHONE_ohanzgywsqragdiezpzvbkiz
-rdid: '1750694538670026752'
-qczcid: '1754069117703229440'
-xspxid: '1741114772279947264'
-xxpxid: '14'
-qczdid: '1751907844915245056'
-qcssid: '1740571928649682944'
-sstdid: '1751797500327436288'
-hdzxid: '43'
-kjid: '1715329303230861312'
-wjid: d37ab86511db3b832fb3957de245b8c6
-lxid: '1744545080781602816'
-jdid: '1715272152655396864'
-jxid: '1744358625178320896'
-dsid: 10138
+token: PHONE_qvwtesenmtjpljknbcoeuboe

+ 6 - 0
test_case/__init__.py

@@ -0,0 +1,6 @@
+from pathlib import Path
+
+path = Path(__file__).parent.glob("**/*.yaml")
+
+for i in path:
+    print(type(i))

+ 7 - 7
test_case/qccq/test_allAPI.py

@@ -8,8 +8,7 @@ import pytest
 from common.parameterize import ddt, read_testcase
 from common.request_util import RequestUtil
 
-path = Path(__file__).parent.glob("**/*.yaml")
-
+# path = Path(__file__).parent.glob("**/*.yaml")
 
 def creat_case(yaml_path):
     @pytest.mark.parametrize("arg_names", read_testcase(rf"{yaml_path}"))
@@ -32,10 +31,11 @@ class TestAPI:
 
 
 
-data = []
-for yaml_path in path:
-    data.append(yaml_path)
-data.sort()
+data = [Path(r"test_case/qccq/个人中心/test_001_个人中心-我的消息.yaml")]
+# for yaml_path in path:
+#     data.append(yaml_path)
+# data.sort()
 for yaml_path in data:
     yaml_name = yaml_path.name[:-5]
-    setattr(TestAPI, yaml_name, creat_case(yaml_path))
+    setattr(TestAPI, yaml_name, creat_case(yaml_path))
+

+ 7 - 7
test_case/test_allAPI.py

@@ -8,8 +8,7 @@ import pytest
 from common.parameterize import ddt, read_testcase
 from common.request_util import RequestUtil
 
-path = Path(__file__).parent.glob("**/*.yaml")
-
+# path = Path(__file__).parent.glob("**/*.yaml")
 
 def creat_case(yaml_path):
     @pytest.mark.parametrize("arg_names", read_testcase(rf"{yaml_path}"))
@@ -32,10 +31,11 @@ class TestAPI:
 
 
 
-data = []
-for yaml_path in path:
-    data.append(yaml_path)
-data.sort()
+data = [Path(r"D:\TestPythonProject\TEST\test_case\qccq\个人中心\test_002_个人中心-我的收藏.yaml")]
+# for yaml_path in path:
+#     data.append(yaml_path)
+# data.sort()
 for yaml_path in data:
     yaml_name = yaml_path.name[:-5]
-    setattr(TestAPI, yaml_name, creat_case(yaml_path))
+    setattr(TestAPI, yaml_name, creat_case(yaml_path))
+