Skip to content

Commit

Permalink
fix: console recorder not enabled in cloud mode (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
SAKURA-CAT authored Mar 3, 2025
1 parent 067740b commit 324f34f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
1 change: 1 addition & 0 deletions swanlab/data/callbacker/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def before_run(self, settings: SwanLabSharedSettings):
self.settings = settings

def on_run(self):
swanlog.install()
http = get_http()
# 注册实验信息
http.mount_exp(
Expand Down
4 changes: 3 additions & 1 deletion swanlab/log/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
@File: swanlab/log/__init__.py
@IDE: vscode
@Description:
日志记录模块,在设计上swanlog作为一个独立的模块被使用,你可以在除了utils的任何地方使用它
日志记录模块,在设计上swanlog作为一个独立的模块被使用
FIXME: shit code
"""
from typing import Optional

from .log import SwanLog

swanlog: Optional["SwanLog"] = SwanLog("swanlab")
Expand Down
40 changes: 21 additions & 19 deletions swanlab/log/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ def _(message):
self.write_callback = _

def init(self, path):
self.console_folder = path
# path 是否存在
if not os.path.exists(path):
os.makedirs(path)
# 日志文件路径
self.now = datetime.now().strftime("%Y-%m-%d")
console_path = os.path.join(path, f"{self.now}.log")
# 拿到日志文件句柄
self.file = open(console_path, "a", encoding="utf-8")
if path:
self.console_folder = path
# path 是否存在
if not os.path.exists(path):
os.makedirs(path)
# 日志文件路径
self.now = datetime.now().strftime("%Y-%m-%d")
console_path = os.path.join(path, f"{self.now}.log")
# 拿到日志文件句柄
self.file = open(console_path, "a", encoding="utf-8")
# 封装sys.stdout
self.write_handler = sys.stdout.write

Expand All @@ -91,16 +92,17 @@ def _(message):
message = FONT.clear(message)[:MAX_UPLOAD_LEN]
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()
if path:
# 检查文件分片
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()

sys.stdout.write = _

Expand Down
8 changes: 4 additions & 4 deletions swanlab/log/log.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .console import SwanConsoler
from swankit.log import SwanLabSharedLog

from .console import SwanConsoler


class SwanLog(SwanLabSharedLog):

Expand Down Expand Up @@ -37,9 +38,8 @@ def install(self, console_dir: str = None, log_level: str = None) -> "SwanLog":
if log_level is not None:
self.level = log_level
# 初始化控制台记录器
if console_dir:
self.debug("Init consoler to record console log")
self.__consoler.install(console_dir)
self.debug("Init consoler to record console log")
self.__consoler.install(console_dir)
self.__installed = True
return self

Expand Down

0 comments on commit 324f34f

Please sign in to comment.