MCPcopy Create free account
hub / github.com/CJackHwang/AIstudioProxyAPI / StreamToLogger

Class StreamToLogger

models/logging.py:11–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9
10
11class StreamToLogger:
12 def __init__(self, logger_instance: logging.Logger, log_level: int = logging.INFO):
13 self.logger = logger_instance
14 self.log_level = log_level
15 self.linebuf = ""
16
17 def write(self, buf: str):
18 try:
19 temp_linebuf = self.linebuf + buf
20 self.linebuf = ""
21 for line in temp_linebuf.splitlines(True):
22 if line.endswith(("\n", "\r")):
23 self.logger.log(self.log_level, line.rstrip())
24 else:
25 self.linebuf += line
26 except Exception as e:
27 print(f"StreamToLogger Error: {e}", file=sys.__stderr__)
28
29 def flush(self):
30 try:
31 if self.linebuf != "":
32 self.logger.log(self.log_level, self.linebuf.rstrip())
33 self.linebuf = ""
34 except Exception as e:
35 print(f"StreamToLogger Flush Error: {e}", file=sys.__stderr__)
36
37 def isatty(self):
38 return False
39
40
41class WebSocketConnectionManager:

Calls

no outgoing calls