# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test59
# FileName:      __init__.py
# Author:       laozhao
# Datetime:     2024/3/18 16:11
# Description:
# 
# ---------------------------------------------------------------------------
import functools
import logging
import os
import time


def my_log(filename):
    # 创建logger对象
    logger = logging.getLogger()
    # 设置日志的显示级别
    logger.level = logging.NOTSET
    # 设置日志写入的文件
    handler = logging.FileHandler(filename, mode="a", encoding="utf-8")
    # 设置日志文件中日志的级别
    handler.setLevel(logging.NOTSET)
    # 设置日志文件中日志的格式
    formatter = logging.Formatter('%(asctime)s - %(levelname)s: %(message)s')
    # 将格式设置到日志文件中
    handler.setFormatter(formatter)
    # 将日志文件加载到logger中
    logger.addHandler(handler)
    return logger


log_path = os.path.join(os.path.join(os.path.join(os.path.dirname(__file__), "report"), "log"), time.strftime("%Y_%m_%d_%H_%M_%S")+".log")
# log_path = os.path.join(os.path.join(os.path.join(os.path.dirname(__file__), "report"), "log"), "BPM.log")
log = my_log(log_path)


def log_decorator(func_name):
    @functools.wraps(func_name)
    def inner(*args, **kwargs):
        try:
            log.info("执行的函数或方法为："+str(func_name.__name__)+"，其功能为："+str(func_name.__doc__))
            return func_name(*args, **kwargs)
        except Exception as e:
            log.error(e)
            raise e
    return inner