Skip to content
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
4 changes: 2 additions & 2 deletions swanlab/api/upload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def create_data(metrics: List[dict], metrics_type: str) -> dict:


@async_error_handler
async def upload_logs(logs: List[str], level: str = "INFO"):
async def upload_logs(logs: List[dict], level: str = "INFO"):
"""
模拟一下,上传日志和实验指标信息
:param logs: 日志列表
:param level: 日志级别,'INFO', 'ERROR',默认INFO
"""
http = get_http()
# 将logs解析为json格式
metrics = [{"level": level, "message": x} for x in logs]
metrics = [{"level": level, **x} for x in logs]
data = create_data(metrics, "log")
await http.post(url, data)

Expand Down
9 changes: 6 additions & 3 deletions swanlab/data/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from swanlab.package import version_limit, get_package_version, get_host_api, get_host_web
from swanlab.error import KeyFileError
from swanlab.cloud import LogSnifferTask, ThreadPool
from swanlab.utils import create_time

run: Optional["SwanLabRun"] = None
"""Global runtime instance. After the user calls finish(), run will be set to None."""
Expand Down Expand Up @@ -179,9 +180,8 @@ def init(
if login_info is None and cloud:
# 用户登录
login_info = _login_in_init()
# 初始化会话信息
if cloud:
http = create_http(login_info)
# 获取当前项目信息
exp_num = http.mount_project(project, workspace).history_exp_count

# 连接本地数据库,要求路径必须存在,但是如果数据库文件不存在,会自动创建
Expand Down Expand Up @@ -367,7 +367,10 @@ async def _():
await run.settings.pool.finish()
# 上传错误日志
if error is not None:
await upload_logs([e + '\n' for e in error.split('\n')], level='ERROR')
await upload_logs(
[{"message": error, "create_time": create_time(), "epoch": swanlog.epoch + 1}],
level='ERROR'
)
await asyncio.sleep(1)

asyncio.run(FONT.loading("Waiting for uploading complete", _(), interval=0.5))
Expand Down
File renamed without changes.
17 changes: 15 additions & 2 deletions swanlab/log/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime
from ..utils import FONT
from swanlab.cloud.task_types import UploadType
from typing import Optional
from swanlab.utils import create_time


class LeverCtl(object):
Expand Down Expand Up @@ -150,6 +150,14 @@ def __init__(self, *args, **kwargs):
self.__init_status = False
self.original_stdout = sys.stdout # 保存原始的 sys.stdout
self.pool = None
self.__epoch = 0

@property
def epoch(self):
"""
write 函数调用次数
"""
return self.__epoch

@property
def init_status(self) -> bool:
Expand Down Expand Up @@ -251,7 +259,12 @@ def get_sum(self):
def upload_message(self, message):
if self.pool is None:
return
self.pool.queue.put((UploadType.LOG, [message]))
self.__epoch += 1
# 将日志信息放入队列
self.pool.queue.put((
UploadType.LOG,
[{"message": message, "create_time": create_time(), "epoch": self.__epoch}]
))


class SwanConsoler:
Expand Down
7 changes: 7 additions & 0 deletions swanlab/log/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ def init(
if file_level:
self.set_file_level(file_level)

@property
def epoch(self):
"""
获取当前日志的 epoch
"""
return self.__consoler.consoler.epoch

# 检测日志处理器是否重复注册
def _check_init(func):
"""装饰器,防止多次注册处理器"""
Expand Down