From 90b0765561900a33bfe3a50c56e04ac70fc82976 Mon Sep 17 00:00:00 2001 From: XianBW <36835909+XianBW@users.noreply.github.com> Date: Mon, 16 Jun 2025 19:52:44 +0800 Subject: [PATCH] fix: log info (#965) * fix log caller_info * make env info beauty --- rdagent/log/logger.py | 10 ++++++---- rdagent/log/utils/__init__.py | 4 ++-- rdagent/utils/env.py | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/rdagent/log/logger.py b/rdagent/log/logger.py index a60bacb3..7cb57146 100644 --- a/rdagent/log/logger.py +++ b/rdagent/log/logger.py @@ -20,7 +20,7 @@ from rdagent.core.utils import SingletonBaseClass, import_class from .base import Storage from .storage import FileStorage -from .utils import get_caller_info +from .utils import LogColors, get_caller_info class RDAgentLog(SingletonBaseClass): @@ -106,15 +106,17 @@ class RDAgentLog(SingletonBaseClass): return pid_chain def log_object(self, obj: object, *, tag: str = "") -> None: - caller_info = get_caller_info() + caller_info = get_caller_info(level=2) tag = f"{self._tag}.{tag}.{self.get_pids()}".strip(".") for storage in [self.storage] + self.other_storages: logp = storage.log(obj, tag=tag) - logger.patch(lambda r: r.update(caller_info)).info(f"Log object to [{storage}], uri: {logp}") + logger.patch(lambda r: r.update(caller_info)).info( + f"{LogColors.GRAY}Log object to [{storage}], uri: {logp}{LogColors.END}" + ) def _log(self, level: str, msg: str, *, tag: str = "", raw: bool = False) -> None: - caller_info = get_caller_info() + caller_info = get_caller_info(level=3) tag = f"{self._tag}.{tag}.{self.get_pids()}".strip(".") if raw: diff --git a/rdagent/log/utils/__init__.py b/rdagent/log/utils/__init__.py index 33a0236c..2aa28ede 100644 --- a/rdagent/log/utils/__init__.py +++ b/rdagent/log/utils/__init__.py @@ -66,11 +66,11 @@ class CallerInfo(TypedDict): name: Optional[str] -def get_caller_info() -> CallerInfo: +def get_caller_info(level: int = 2) -> CallerInfo: # Get the current stack information stack = inspect.stack() # The second element is usually the caller's information - caller_info = stack[2] + caller_info = stack[level] frame = caller_info[0] info: CallerInfo = { "line": caller_info.lineno, diff --git a/rdagent/utils/env.py b/rdagent/utils/env.py index b3f187a6..bddc02f3 100644 --- a/rdagent/utils/env.py +++ b/rdagent/utils/env.py @@ -469,11 +469,11 @@ class LocalEnv(Env[ASpecificLocalConf]): print(Rule("[bold green]LocalEnv Logs Begin[/bold green]", style="dark_orange")) table = Table(title="Run Info", show_header=False) table.add_column("Key", style="bold cyan") - table.add_column("Value", style="bold magenta") + table.add_column("Value", style="bold magenta", no_wrap=True) table.add_row("Entry", entry) table.add_row("Local Path", local_path or "") table.add_row("Env", "\n".join(f"{k}:{v}" for k, v in env.items())) - table.add_row("Volumes", "\n".join(f"{k}:{v}" for k, v in volumes.items())) + table.add_row("Volumes", "\n".join(f"{k}:\n {v}" for k, v in volumes.items())) print(table) cwd = Path(local_path).resolve() if local_path else None