| 30 | |
| 31 | |
| 32 | class FastDeployLogger: |
| 33 | _instance = None |
| 34 | _initialized = False |
| 35 | _lock = threading.RLock() |
| 36 | |
| 37 | def __new__(cls): |
| 38 | """单例模式实现""" |
| 39 | if cls._instance is None: |
| 40 | with cls._lock: |
| 41 | if cls._instance is None: |
| 42 | cls._instance = super().__new__(cls) |
| 43 | return cls._instance |
| 44 | |
| 45 | def _initialize(self): |
| 46 | """显式初始化日志系统""" |
| 47 | with self._lock: |
| 48 | if not self._initialized: |
| 49 | setup_logging() |
| 50 | self._initialized = True |
| 51 | |
| 52 | def get_logger(self, name, file_name=None, without_formater=False, print_to_console=False): |
| 53 | """ |
| 54 | 获取日志记录器(兼容原有接口) |
| 55 | |
| 56 | Args: |
| 57 | name: 日志器名称 |
| 58 | file_name: 日志文件名(保持兼容性) |
| 59 | without_formater: 是否不使用格式化器 |
| 60 | print_to_console: 是否打印到控制台 |
| 61 | """ |
| 62 | # 如果只有一个参数,使用新的统一命名方式 |
| 63 | if file_name is None and not without_formater and not print_to_console: |
| 64 | # 延迟初始化 |
| 65 | if not self._initialized: |
| 66 | self._initialize() |
| 67 | return self._get_unified_logger(name) |
| 68 | |
| 69 | # 兼容原有接口 |
| 70 | return self._get_legacy_logger(name, file_name, without_formater, print_to_console) |
| 71 | |
| 72 | def _get_unified_logger(self, name): |
| 73 | """ |
| 74 | 新的统一日志获取方式 |
| 75 | """ |
| 76 | if name is None: |
| 77 | return logging.getLogger("fastdeploy") |
| 78 | |
| 79 | # 处理 __main__ 特殊情况 |
| 80 | if name == "__main__": |
| 81 | import __main__ |
| 82 | |
| 83 | # 获取主模块的 __file__ 属性 |
| 84 | if hasattr(__main__, "__file__"): |
| 85 | # 获取主模块的文件名 |
| 86 | base_name = Path(__main__.__file__).stem |
| 87 | # 创建带前缀的日志器 |
| 88 | return logging.getLogger(f"fastdeploy.main.{base_name}") |
| 89 | return logging.getLogger("fastdeploy.main") |
no outgoing calls