import logging
import uuid
import os
from datetime import datetime
import sys
import threading

# 自定义trace_filter属性名
TRACE_FILTER_ATTR = "trace_filter"
# 当前线程的local_trace, 需要添加全局trace_id, 使用示例：trace.trace_id
local_trace = threading.local()
formatter = logging.Formatter(
    'time:%(asctime)s, traceId:%(trace_id)s, %(asctime)s - %(name)s - %(levelname)s - %(message)s')


class TraceFilter(logging.Filter):
    """
    通过在record中添加trace_id, 实现调用跟踪和日志打印的分离
    """

    def __init__(self, name=""):
        """
        init
        @param name: filter name
        """
        super().__init__(name)

    def filter(self, record):
        """
        重写filter方法
        @param record: record
        @return:
        """
        trace_id = local_trace.trace_id if hasattr(local_trace, 'trace_id') else uuid.uuid1()
        record.trace_id = trace_id
        return True


class TraceLogger:
    @staticmethod
    def get_log_file_path(logger_name):
        # 创建日志文件路径
        basedir = os.path.abspath('..')
        log_dir_name = 'logs'
        current_working_directory = os.getcwd()
        log_file_path = os.path.join(current_working_directory, log_dir_name)
        log_dir = os.path.join(basedir, log_file_path)
        if not os.path.exists(log_dir):
            os.mkdir(log_dir)
        log_file_name = "user_{}_log.{}.log".format(logger_name, str(datetime.now().strftime('%Y-%m-%d')))
        log_file_path = os.path.join(log_dir, log_file_name)
        return log_file_path

    @staticmethod
    def get_logger_func(logger_name):
        # 创建一个名为'access'的日志记录器实例
        access_filename = TraceLogger.get_log_file_path(logger_name)
        access_logger = logging.getLogger(logger_name)
        access_logger.setLevel(logging.INFO)  # 设置日志记录器的最低捕获级别为DEBUG
        # 添加日志跟踪filter
        trace_filter = TraceFilter()
        access_logger.addFilter(trace_filter)
        # 创建一个文件处理器，用于输出logger1的日志到'debug.log'
        access_info = logging.FileHandler(access_filename, encoding='utf-8', delay=False)
        access_info.setLevel(logging.INFO)  # 设置处理器的级别为DEBUG
        # 将格式化器添加到处理器中
        access_info.setFormatter(formatter)
        # 将处理器添加到日志记录器中
        access_logger.addHandler(access_info)
        return access_logger


# 创建一个日志格式化器


access_logger = TraceLogger.get_logger_func("access")
server_logger = TraceLogger.get_logger_func("server")
error_logger = TraceLogger.get_logger_func("error")

# error_logger = TraceLogger.get_logger()
console_handler = logging.StreamHandler()
# logger = TraceLogger.get_logger()
# server_logger = TraceLogger.get_server_logger()
# error_logger = TraceLogger.get_server_logger()
