diff --git a/unilabos/app/main.py b/unilabos/app/main.py index abe43058..ea758672 100644 --- a/unilabos/app/main.py +++ b/unilabos/app/main.py @@ -218,7 +218,7 @@ def main(): if hasattr(BasicConfig, "log_level"): logger.info(f"Log level set to '{BasicConfig.log_level}' from config file.") - configure_logger(loglevel=BasicConfig.log_level) + configure_logger(loglevel=BasicConfig.log_level, working_dir=working_dir) if args_dict["addr"] == "test": print_status("使用测试环境地址", "info") diff --git a/unilabos/devices/virtual/virtual_multiway_valve.py b/unilabos/devices/virtual/virtual_multiway_valve.py index d0d792e7..1512f33d 100644 --- a/unilabos/devices/virtual/virtual_multiway_valve.py +++ b/unilabos/devices/virtual/virtual_multiway_valve.py @@ -7,7 +7,7 @@ class VirtualMultiwayValve: """ 虚拟九通阀门 - 0号位连接transfer pump,1-8号位连接其他设备 🔄 """ - def __init__(self, port: str = "VIRTUAL", positions: int = 8): + def __init__(self, port: str = "VIRTUAL", positions: int = 8, **kwargs): self.port = port self.max_positions = positions # 1-8号位 self.total_positions = positions + 1 # 0-8号位,共9个位置 diff --git a/unilabos/devices/workstation/workstation_base.py b/unilabos/devices/workstation/workstation_base.py index 97db1505..75fd7ea8 100644 --- a/unilabos/devices/workstation/workstation_base.py +++ b/unilabos/devices/workstation/workstation_base.py @@ -147,7 +147,7 @@ class WorkstationBase(ABC): def __init__( self, - deck: Deck, + deck: Optional[Deck], *args, **kwargs, # 必须有kwargs ): @@ -349,5 +349,5 @@ class WorkstationBase(ABC): class ProtocolNode(WorkstationBase): - def __init__(self, deck: Optional[PLRResource], *args, **kwargs): + def __init__(self, protocol_type: List[str], deck: Optional[PLRResource], *args, **kwargs): super().__init__(deck, *args, **kwargs) diff --git a/unilabos/registry/devices/work_station.yaml b/unilabos/registry/devices/work_station.yaml index 74987ee0..e1be7f3d 100644 --- a/unilabos/registry/devices/work_station.yaml +++ b/unilabos/registry/devices/work_station.yaml @@ -6036,7 +6036,12 @@ workstation: properties: deck: type: string + protocol_type: + items: + type: string + type: array required: + - protocol_type - deck type: object data: diff --git a/unilabos/utils/log.py b/unilabos/utils/log.py index 3894233b..af03d946 100644 --- a/unilabos/utils/log.py +++ b/unilabos/utils/log.py @@ -124,11 +124,14 @@ class ColoredFormatter(logging.Formatter): def _format_basic(self, record): """基本格式化,不包含颜色""" datetime_str = datetime.fromtimestamp(record.created).strftime("%y-%m-%d [%H:%M:%S,%f")[:-3] + "]" - filename = os.path.basename(record.filename).rsplit(".", 1)[0] # 提取文件名(不含路径和扩展名) + filename = record.filename.replace(".py", "").split("\\")[-1] # 提取文件名(不含路径和扩展名) + if "/" in filename: + filename = filename.split("/")[-1] module_path = f"{record.name}.{filename}" func_line = f"{record.funcName}:{record.lineno}" + right_info = f" [{func_line}] [{module_path}]" - formatted_message = f"{datetime_str} [{record.levelname}] [{module_path}] [{func_line}]: {record.getMessage()}" + formatted_message = f"{datetime_str} [{record.levelname}] {record.getMessage()}{right_info}" if record.exc_info: exc_text = self.formatException(record.exc_info) @@ -150,7 +153,7 @@ class ColoredFormatter(logging.Formatter): # 配置日志处理器 -def configure_logger(loglevel=None): +def configure_logger(loglevel=None, working_dir=None): """配置日志记录器 Args: @@ -191,9 +194,30 @@ def configure_logger(loglevel=None): # 添加处理器到根日志记录器 root_logger.addHandler(console_handler) + + # 如果指定了工作目录,添加文件处理器 + if working_dir is not None: + logs_dir = os.path.join(working_dir, "logs") + os.makedirs(logs_dir, exist_ok=True) + + # 生成日志文件名:日期 时间.log + log_filename = datetime.now().strftime("%Y-%m-%d %H-%M-%S") + ".log" + log_filepath = os.path.join(logs_dir, log_filename) + + # 创建文件处理器 + file_handler = logging.FileHandler(log_filepath, encoding="utf-8") + file_handler.setLevel(root_logger.level) + + # 使用不带颜色的格式化器 + file_formatter = ColoredFormatter(use_colors=False) + file_handler.setFormatter(file_formatter) + + root_logger.addHandler(file_handler) + logging.getLogger("asyncio").setLevel(logging.INFO) logging.getLogger("urllib3").setLevel(logging.INFO) + # 配置日志系统 configure_logger()