From 11b5101b430a2057e180f6ce42b140abbad95c05 Mon Sep 17 00:00:00 2001 From: Ramimashkouk Date: Fri, 29 Nov 2024 18:41:56 +0800 Subject: [PATCH] fix: Set a logger for service_replacer --- .../service_replacer.py | 23 +++++++++++++------ backend/chatsky_ui/services/process.py | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/backend/chatsky_ui/services/json_converter_new2/logic_component_converter/service_replacer.py b/backend/chatsky_ui/services/json_converter_new2/logic_component_converter/service_replacer.py index 2253fd2d..4af3dd61 100644 --- a/backend/chatsky_ui/services/json_converter_new2/logic_component_converter/service_replacer.py +++ b/backend/chatsky_ui/services/json_converter_new2/logic_component_converter/service_replacer.py @@ -2,15 +2,23 @@ from ast import NodeTransformer from typing import Dict, List from pathlib import Path - +import logging from chatsky_ui.core.logger_config import get_logger -logger = get_logger(__name__) - class ServiceReplacer(NodeTransformer): def __init__(self, new_services: List[str]): self.new_services_classes = self._get_classes_def(new_services) + self._logger = None + + @property + def logger(self): + if self._logger is None: + raise ValueError("Logger has not been configured. Call set_logger() first.") + return self._logger + + def set_logger(self): + self._logger = get_logger(__name__) def _get_classes_def(self, services_code: List[str]) -> Dict[str, ast.ClassDef]: parsed_codes = [ast.parse(service_code) for service_code in services_code] @@ -24,11 +32,11 @@ def _extract_class_defs(self, parsed_code: ast.Module, service_code: str): if isinstance(node, ast.ClassDef): classes[node.name] = node else: - logger.error("No class definition found in new_service: %s", service_code) + self.logger.error("No class definition found in new_service: %s", service_code) return classes def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef: - logger.debug("Visiting class '%s' and comparing with: %s", node.name, self.new_services_classes.keys()) + self.logger.debug("Visiting class '%s' and comparing with: %s", node.name, self.new_services_classes.keys()) if node.name in self.new_services_classes: return self._get_class_def(node) return node @@ -36,7 +44,7 @@ def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef: def _get_class_def(self, node: ast.ClassDef) -> ast.ClassDef: service = self.new_services_classes[node.name] del self.new_services_classes[node.name] - logger.info("Updating class '%s'", node.name) + self.logger.info("Updating class '%s'", node.name) return service def generic_visit(self, node: ast.AST): @@ -46,7 +54,7 @@ def generic_visit(self, node: ast.AST): return node def _append_new_services(self, node: ast.Module): - logger.info("Services not found, appending new services: %s", list(self.new_services_classes.keys())) + self.logger.info("Services not found, appending new services: %s", list(self.new_services_classes.keys())) for _, service in self.new_services_classes.items(): node.body.append(service) @@ -56,6 +64,7 @@ def store_custom_service(services_path: Path, services: List[str]): conditions_tree = ast.parse(file.read()) replacer = ServiceReplacer(services) + replacer.set_logger() replacer.visit(conditions_tree) with open(services_path, "w") as file: diff --git a/backend/chatsky_ui/services/process.py b/backend/chatsky_ui/services/process.py index 765e069f..93ef839e 100644 --- a/backend/chatsky_ui/services/process.py +++ b/backend/chatsky_ui/services/process.py @@ -183,7 +183,7 @@ async def check_telegram_readiness(stream, name): return response.json()["status"] == "ok" except Exception as e: self.logger.info( - f"Process '{self.id}' isn't alive on port '{HTTP_INTERFACE_PORT}'. " + f"Process '{self.id}' isn't alive on port '{HTTP_INTERFACE_PORT}' yet. " f"Ignore this if you're not connecting via HTTPInterface. Exception caught: {e}" )