# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test65
# FileName:      __init__.py
# Author:       lao_zhao
# Datetime:     2025/3/12 11:03
# Description:
# 
# ---------------------------------------------------------------------------
import functools
import logging
import os
import time

from faker.providers.ssn.es_CL import rut_check_digit


def log_func(file_path):
    """获取写日志的logger"""
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    handler = logging.FileHandler(file_path, mode="a", encoding="utf-8")
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger


# 动态获取日志文件存放目录
log_dir = os.path.join(os.path.join(os.path.dirname(__file__), "report"), "log")
# 获取日志文件名称
log_name = "apiAutoTest_"+time.strftime("%Y_%m_%d_%H_%M_%S")+".log"
# 日志文件的路径
log_path = os.path.join(log_dir, log_name)
# 获取写日志的logger
log = log_func(log_path)


# 创建写日志的装饰器
def log_decorator(func_name):
    @functools.wraps(func_name)
    def inner(*args, **kwargs):
        log.info(f"执行的功能为：{func_name.__name__}, 功能描述为：{func_name.__doc__}, 所在的文件：{func_name.__code__.co_filename}, 所在行为：{func_name.__code__.co_firstlineno}")
        try:
            result = func_name(*args, **kwargs)
        except Exception as e:
            log.error(f"生产错误为：{type(e)}, 错误的描述为:{e}，执行的功能为：{func_name.__name__}, 功能描述为：{func_name.__doc__}, 所在的文件：{func_name.__code__.co_filename}, 所在行为：{func_name.__code__.co_firstlineno}")
            raise e
        else:
            return result
    return inner