@@ -68,7 +68,8 @@ def _bind_zmq_socket(sock, bind_addr: str) -> str:
6868 return actual_addr
6969 except RuntimeError :
7070 # Fallback to random port
71- return sock .bind_to_random_port ("tcp://127.0.0.1" )
71+ port = sock .bind_to_random_port ("tcp://127.0.0.1" )
72+ return f"tcp://127.0.0.1:{ port } "
7273
7374
7475class ZMQLogHandler (logging .Handler ):
@@ -84,21 +85,23 @@ def __init__(self, addr=None, tool_name="unknown_tool"):
8485 # Try to connect to the address
8586 try :
8687 self .sock .connect (addr )
87- print (f"ZMQ handler connected to: { addr } " )
88+ logging . getLogger ( __name__ ). info (f"ZMQ handler connected to: { addr } " )
8889 except zmq .error .ZMQError as e :
8990 # If connection fails, disable the handler
90- print ( f"Warning: Could not connect to ZMQ listener at { addr } : { e } " )
91- print ("Disabling ZMQ logging for this handler" )
91+ logging . getLogger ( __name__ ). warning ( f" Could not connect to ZMQ listener at { addr } : { e } " )
92+ logging . getLogger ( __name__ ). warning ("Disabling ZMQ logging for this handler" )
9293 self .sock = None
9394
9495 self .task_id = os .environ .get ("TASK_ID" , "0" )
9596 self .tool_name = tool_name
9697
9798 def emit (self , record ):
99+ if self .sock is None :
100+ return
101+
98102 try :
99- if self .sock is not None :
100- msg = f"{ record .getMessage ()} "
101- self .sock .send_string (f"{ self .task_id } ||{ self .tool_name } ||{ msg } " )
103+ msg = f"{ record .getMessage ()} "
104+ self .sock .send_string (f"{ self .task_id } ||{ self .tool_name } ||{ msg } " )
102105 except Exception :
103106 self .handleError (record )
104107
@@ -110,13 +113,13 @@ async def zmq_log_listener(bind_addr="tcp://127.0.0.1:6000"):
110113 # Bind to available port
111114 actual_addr = _bind_zmq_socket (sock , bind_addr )
112115 set_zmq_address (actual_addr )
113- print (f"ZMQ listener bound to: { actual_addr } " )
116+ logging . getLogger ( __name__ ). info (f"ZMQ listener bound to: { actual_addr } " )
114117
115118 root_logger = logging .getLogger ()
116119
117120 while True :
118121 raw = await sock .recv_string ()
119- if "|" in raw :
122+ if "|| " in raw :
120123 task_id , tool_name , msg = raw .split ("||" , 2 )
121124
122125 record = root_logger .makeRecord (
0 commit comments