diff --git a/swanlab/data/run/main.py b/swanlab/data/run/main.py index 5610228a6..3c78fc39c 100644 --- a/swanlab/data/run/main.py +++ b/swanlab/data/run/main.py @@ -21,7 +21,9 @@ from ..formater import check_key_format from swanlab.env import get_mode, get_swanlog_dir import random -from swankit.env import is_windows + + +MAX_LIST_LENGTH = 108 class SwanLabRunState(Enum): @@ -313,6 +315,9 @@ def log(self, data: dict, step: int = None): and all([isinstance(i, (Line, MediaType)) for i in v]) and all([i.__class__ == v[0].__class__ for i in v]) ): + if len(v) > MAX_LIST_LENGTH: + swanlog.warning(f"List length '{k}' is too long, cut to {MAX_LIST_LENGTH}.") + v = v[:MAX_LIST_LENGTH] v = DataWrapper(k, v) else: # 其余情况被当作是非法的数据类型,交给Line处理 diff --git a/swanlab/log/console.py b/swanlab/log/console.py index f1a9996f9..20abe948b 100644 --- a/swanlab/log/console.py +++ b/swanlab/log/console.py @@ -6,6 +6,9 @@ import re +MAX_UPLOAD_LEN = 200 + + class SwanWriterProxy: """ 标准输出流拦截代理 @@ -81,7 +84,8 @@ def _(message): except UnicodeEncodeError: # 遇到编码问题,直接pass,此时表现为终端不输出 pass - message = FONT.clear(message) + # 限制上传长度 + message = FONT.clear(message)[:MAX_UPLOAD_LEN] self.write_callback and self.write_callback(message) # 检查文件分片 diff --git a/test/unit/data/run/test_main.py b/test/unit/data/run/test_main.py index 63b3321c8..8c2c66d6a 100644 --- a/test/unit/data/run/test_main.py +++ b/test/unit/data/run/test_main.py @@ -199,6 +199,9 @@ def test_log_text_ok(self): # list ll3 = run.log({"a": [Text("abc"), Text("def")]}) assert ll3["a"].data == ["abc", "def"] + data = {"a": [Text("abc")] * 109} + ll4 = run.log(data, step=4) + assert ll4["a"].data == ["abc"] * 108 # ---------------------------------- 解析log Audio ---------------------------------- diff --git a/test/unit/log/test_log.py b/test/unit/log/test_log.py index 122084d52..dc0d41e97 100644 --- a/test/unit/log/test_log.py +++ b/test/unit/log/test_log.py @@ -82,6 +82,18 @@ def test_write_to_file(self): assert content[-2] == a + "\n" assert content[-1] == b + "\n" + def test_write_to_file_long_test(self): + console_dir = self.create_console_dir() + swanlog.install(console_dir) + # 加一行防止其他问题 + print("\ntest write to file") + a = generate(size=201) + print(a) + files = os.listdir(console_dir) + with open(os.path.join(console_dir, files[0]), "r") as f: + content = f.readlines() + assert content[-1] == a[:200] + "\n" + def test_write_logging_to_file(self): console_dir = self.create_console_dir() swanlog.install(console_dir, log_level="debug")