Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions fastchat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def build_logger(logger_name, logger_filename):
sys.stdout = sl

stderr_logger = logging.getLogger("stderr")
stderr_logger.setLevel(logging.ERROR)
stderr_logger.setLevel(logging.DEBUG)
sl = StreamToLogger(stderr_logger, logging.ERROR)
sys.stderr = sl

Expand Down Expand Up @@ -106,17 +106,33 @@ def write(self, buf):
# translates them so this is still cross platform.
if line[-1] == "\n":
encoded_message = line.encode("utf-8", "ignore").decode("utf-8")
self.logger.log(self.log_level, encoded_message.rstrip())
level = infer_stream_log_level(encoded_message, self.log_level)
self.logger.log(level, encoded_message.rstrip())
else:
self.linebuf += line

def flush(self):
if self.linebuf != "":
encoded_message = self.linebuf.encode("utf-8", "ignore").decode("utf-8")
self.logger.log(self.log_level, encoded_message.rstrip())
level = infer_stream_log_level(encoded_message, self.log_level)
self.logger.log(level, encoded_message.rstrip())
self.linebuf = ""


def infer_stream_log_level(line: str, default: int) -> int:
"""Map uvicorn-style stderr prefixes to the correct log level."""
stripped = line.lstrip()
if stripped.startswith("INFO:"):
return logging.INFO
if stripped.startswith("WARNING:") or stripped.startswith("WARN:"):
return logging.WARNING
if stripped.startswith("ERROR:") or stripped.startswith("CRITICAL:"):
return logging.ERROR
if stripped.startswith("DEBUG:"):
return logging.DEBUG
return default


def disable_torch_init():
"""
Disable the redundant torch default initialization to accelerate model creation.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_stream_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import logging

from fastchat.utils import infer_stream_log_level


def test_infer_stream_log_level_maps_uvicorn_prefixes():
assert infer_stream_log_level("INFO: Started server", logging.ERROR) == logging.INFO
assert infer_stream_log_level("WARNING: deprecated", logging.ERROR) == logging.WARNING
assert infer_stream_log_level("ERROR: boom", logging.ERROR) == logging.ERROR
assert infer_stream_log_level("plain stderr line", logging.ERROR) == logging.ERROR
Loading