# project > util > log_util.py
import logging
import os.path
import time
root_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
log_path = os.path.join(root_path, "log")
class Logger:
def __init__(self):
# 定义日志位置和文件名
self.logname = os.path.join(log_path, "{}.log".format(time.strftime("%Y-%m-%d")))
# 定义一个日志容器
self.logger = logging.getLogger("log")
# 设置日志打印的级别
self.logger.setLevel(logging.DEBUG)
# 创建日志输入的格式
self.formater = logging.Formatter(
'[%(asctime)s][%(filename)s %(lineno)d][%(levelname)s]: %(message)s')
# 创建日志处理器,用来存放日志文件
self.filelogger = logging.FileHandler(self.logname, mode='a', encoding="UTF-8")
# 创建日志处理器,在控制台打印
self.console = logging.StreamHandler()
# 设置控制台打印日志界别
self.console.setLevel(logging.DEBUG)
# 文件存放日志级别
self.filelogger.setLevel(logging.DEBUG)
# 文件存放日志格式
self.filelogger.setFormatter(self.formater)
# 控制台打印日志格式
self.console.setFormatter(self.formater)
# 将日志输出渠道添加到日志收集器中
self.logger.addHandler(self.filelogger)
self.logger.addHandler(self.console)
def test(self):
self.logger.info("hahahaha")
logger = Logger().logger
if __name__ == '__main__':
logger.debug("我打印debug日志")
logger.info("我打印info日志")
logger.warning("我打印warning日志")
logger.error("我打印error日志")
# Logger().test()
# project > test_cases > test_post.py
import pytest
from api.api import try_json
from utils.read_test import base_data
from utils.log_util import logger
def test_post():
logger.info("test_post start...")
json_data = base_data.read_data()['json_data']
result = try_json(json_data)
print(result)
assert result['id'] == 101
logger.info("test_post completed!")
if __name__ == '__main__':
pytest.main()
# result
test_post.py [2024-02-15 17:01:58,217][test_post.py 7][INFO]: test_post start...
{'title': 'foo', 'body': 'bar', 'userId': 1, 'id': 101}
.
============================== 1 passed in 1.08s ==============================
[2024-02-15 17:01:59,202][test_post.py 12][INFO]: test_post completed!
网友评论