diff --git a/requirements-dev.txt b/requirements-dev.txt index 8b42f18d..b4aedc63 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,4 +7,5 @@ ipython<8.13; python_version < '3.9' ipykernel torch torchvision -python-dotenv \ No newline at end of file +python-dotenv +freezegun \ No newline at end of file diff --git a/swanlab/log/console.py b/swanlab/log/console.py index cc0839d8..f1a9996f 100644 --- a/swanlab/log/console.py +++ b/swanlab/log/console.py @@ -3,23 +3,7 @@ from datetime import datetime from swankit.log import FONT from swankit.env import create_time - - -# 检查当前日期是否和控制台日志文件名一致 -def check_file_name(func): - """装饰器,判断是否需要根据日期对控制台输出进行分片存储""" - - def wrapper(self, *args, **kwargs): - now = datetime.now().strftime("%Y-%m-%d") - # 检测now是否和self.now一致 - if now != self.now: - self.now = now - if hasattr(self, "console") and not self.file.closed: - self.file.close() - self.file = open(os.path.join(self.console_folder, self.now + ".log"), "a", encoding="utf-8") - return func(self, *args, **kwargs) - - return wrapper +import re class SwanWriterProxy: @@ -90,12 +74,24 @@ def init(self, path): self.file = open(console_path, "a", encoding="utf-8") # 封装sys.stdout self.write_handler = sys.stdout.write - a = self.write_handler def _(message): - self.write_handler and self.write_handler(message) + try: + self.write_handler and self.write_handler(message) + except UnicodeEncodeError: + # 遇到编码问题,直接pass,此时表现为终端不输出 + pass message = FONT.clear(message) self.write_callback and self.write_callback(message) + + # 检查文件分片 + now = datetime.now().strftime("%Y-%m-%d") + if now != self.now: + self.now = now + if hasattr(self, "console") and not self.file.closed: + self.file.close() + self.file = open(os.path.join(self.console_folder, self.now + ".log"), "a", encoding="utf-8") + self.file.write(message) self.file.flush() @@ -112,7 +108,6 @@ class SwanConsoler: def __init__(self): """ 控制台输出重定向器 - WARNING 一旦此类被初始化,不能将其设置为None,否则会导致输出流无法正常恢复 """ self.writer = SwanWriterProxy() self.__installed = False diff --git a/test/unit/log/pytest_log.py b/test/unit/log/pytest_log.py index b457c799..122084d5 100644 --- a/test/unit/log/pytest_log.py +++ b/test/unit/log/pytest_log.py @@ -7,11 +7,13 @@ @Description: 测试swanlog类,只需测试其日志监听功能 """ +import os +import sys import pytest from swanlab.log import swanlog from tutils import TEMP_PATH from nanoid import generate -import os +from freezegun import freeze_time @pytest.fixture(scope="function", autouse=True) @@ -111,3 +113,18 @@ def test_can_write_logging(self): content = f.readlines() assert content[-2] != "swanlab: " + a + "\n" assert content[-1] == "swanlab: " + b + "\n" + + def test_write_sharding(self, monkeypatch): + """ + 测试日志文件分片 + """ + console_dir = self.create_console_dir() + with freeze_time('2020-10-06'): + swanlog.install(console_dir) + print("1234") + assert os.path.exists(os.path.join(console_dir, "2020-10-06.log")) + with freeze_time('2020-10-07'): + p = os.path.join(console_dir, "2020-10-07.log") + assert not os.path.exists(p) + print("1234") + assert os.path.exists(p) diff --git a/tutils/__init__.py b/tutils/__init__.py index a1f50c2a..e9f58598 100644 --- a/tutils/__init__.py +++ b/tutils/__init__.py @@ -24,6 +24,8 @@ def reset_some_env(): os.environ[SwanLabEnv.SWANLAB_WEB_HOST.value] = web +if not os.path.exists(TEMP_PATH): + os.mkdir(TEMP_PATH) reset_some_env() diff --git a/tutils/check.py b/tutils/check.py index a60cc305..30c77363 100644 --- a/tutils/check.py +++ b/tutils/check.py @@ -19,8 +19,8 @@ swanboard = subprocess.run("pip show swanboard", shell=True, capture_output=True).stdout.decode() swankit = subprocess.run("pip show swankit", shell=True, capture_output=True).stdout.decode() -swanboard_version = [i.split(": ")[1] for i in swanboard.split("\n") if i.startswith("Version")][0] -swankit_version = [i.split(": ")[1] for i in swankit.split("\n") if i.startswith("Version")][0] +swanboard_version = [i.split(": ")[1] for i in swanboard.split("\n") if i.startswith("Version")][0].split('\r')[0] +swankit_version = [i.split(": ")[1] for i in swankit.split("\n") if i.startswith("Version")][0].split('\r')[0] with open(os.path.join(swanlab_dir, "requirements.txt"), "r") as f: packages = f.read().split("\n") packages = [i for i in packages if "swanboard" in i or "swankit" in i]