Skip to content

The console can only print the logger log of the main process #1164

@1066196847

Description

@1066196847

Bug Description

mcp is a child process, which will start a separate logger file, resulting in a different logger from the main process. The console only prints the main process.

Bug solved method

1、logger.py
def define_log_level(print_level="INFO", logfile_level="DEBUG", name: str = None):
"""Adjust the log level to above level"""
global _print_level
_print_level = print_level

current_date = datetime.now()
formatted_date = current_date.strftime("%Y%m%d%H%M%S")
process_id = os.getpid()
log_name = (
    f"{name}_{formatted_date}_{process_id}" if name else f"{formatted_date}_{process_id}"
)

_logger.remove()
_logger.add(
    sys.stderr,
    level=print_level,
    format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{process}</cyan> | <level>{message}</level>",
    colorize=True,
)
_logger.add(
    PROJECT_ROOT / f"logs/{log_name}.log",
    level=logfile_level,
    enqueue=True,
    rotation="10 MB",
    format="{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {process} | {message}",
)
return _logger

2、tool/mcp.py
async def connect_stdio(
self, command: str, args: List[str], server_id: str = ""
) -> None:
"""Connect to an MCP server using stdio transport."""
import threading
if not command:
raise ValueError("Server command is required.")

    server_id = server_id or command

    # Always ensure clean disconnection before new connection
    if server_id in self.sessions:
        await self.disconnect(server_id)

    exit_stack = AsyncExitStack()
    self.exit_stacks[server_id] = exit_stack

    proc = subprocess.Popen(
        [command] + args,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        bufsize=1,
        universal_newlines=True,
    )

    def print_subprocess_output(pipe):
        for line in iter(pipe.readline, ''):
            print(line, end='')
    threading.Thread(target=print_subprocess_output, args=(proc.stdout,), daemon=True).start()

    server_params = StdioServerParameters(command=command, args=args, stdin=proc.stdin, stdout=proc.stdout)
    stdio_transport = await exit_stack.enter_async_context(
        stdio_client(server_params)
    )
    read, write = stdio_transport
    session = await exit_stack.enter_async_context(ClientSession(read, write))
    self.sessions[server_id] = session

    await self._initialize_and_list_tools(server_id)

Environment information

  • System version: windows10
  • Python version: 3.12
  • OpenManus version or branch: main
  • Installation method (e.g., pip install -r requirements.txt or pip install -e .):pip install -r requirements.txt

Extra information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginactive

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions