import logging
import os
logger = None
class Logger:
    _instance = None

    def __new__(cls, log_file):
        if cls._instance is None:
            cls._instance = super(Logger, cls).__new__(cls)
            cls._instance._initialize_logger(log_file)
        return cls._instance

    def _initialize_logger(self, log_file):
        # 确保日志目录存在
        os.makedirs(os.path.dirname(log_file), exist_ok=True)
        
        # 设置日志格式
        formatter = logging.Formatter(
            "[%(asctime)s] - [%(levelname)s] - [%(filename)s] [line:%(lineno)d] - [%(message)s]"
        )
        
        # 创建文件处理器
        file_handler = logging.FileHandler(log_file, mode='a', encoding='utf-8')
        file_handler.setFormatter(formatter)

        # 创建控制台处理器
        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(formatter)


        # 配置日志记录器
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)
        self.logger.addHandler(file_handler)
        self.logger.addHandler(stream_handler)

    def debug(self, message):
        """记录调试信息"""
        self.logger.debug(message)

    def info(self, message):
        """记录普通信息"""
        self.logger.info(message)

    def warning(self, message):
        """记录警告信息"""
        self.logger.warning(message)

    def error(self, message):
        """记录错误信息"""
        self.logger.error(message)

    def critical(self, message):
        """记录严重错误信息"""
        self.logger.critical(message)
def get_logger(log_path):
    """
    返回已配置的全局日志记录器。
    """
    global logger
    if not logger:
        logger = Logger(log_path).logger
    return logger
# 使用示例
if __name__ == "__main__":
    path = "/home/kevin/kevin/zhaoyuan/evaluate_zhaoyuan/log" 
    log = Logger()
    
    log.info("程序开始运行")
    log.debug("这是一个调试消息")
    log.warning("这是一个警告消息")
    log.error("这是一个错误消息")
    log.critical("这是一个严重错误消息")
