# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test61
# FileName:      log.py
# Author:       lao_zhao
# Datetime:     2024/7/12 17:20
# Description:
# 
# ---------------------------------------------------------------------------
import logging
import os
import time

from APIAutoTest_v3_1.common.read_basic_ini import ReadBasicIni


def get_log():
    # 将日志写入到文件中---流程
    # 创建logger
    logger = logging.getLogger()
    # 设置日志文件中写入的级别
    logger.setLevel(level=logging.DEBUG)
    # 需要设置日志的名称
    log_name = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime(time.time())) + ".log"
    # 获取日志的目录
    log_dir = ReadBasicIni().get_log_dir("log")
    # 日志路径拼接
    log_path = os.path.join(log_dir, log_name)
    # 设置日志文件
    file_handler = logging.FileHandler(log_path, mode="a", encoding="utf-8")
    # 设置日志在文件中显示的格式
    log_format = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
    # 将日志设置格式添加到日志文件中
    file_handler.setFormatter(log_format)
    # 将日志文件的handler添加到logger对象中
    logger.addHandler(file_handler)

    return logger
