Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sharding and encoding #636

Merged
merged 3 commits into from
Jun 26, 2024
Merged
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
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ ipython<8.13; python_version < '3.9'
ipykernel
torch
torchvision
python-dotenv
python-dotenv
freezegun
35 changes: 15 additions & 20 deletions swanlab/log/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()

Expand All @@ -112,7 +108,6 @@ class SwanConsoler:
def __init__(self):
"""
控制台输出重定向器
WARNING 一旦此类被初始化,不能将其设置为None,否则会导致输出流无法正常恢复
"""
self.writer = SwanWriterProxy()
self.__installed = False
Expand Down
19 changes: 18 additions & 1 deletion test/unit/log/pytest_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
2 changes: 2 additions & 0 deletions tutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
4 changes: 2 additions & 2 deletions tutils/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down