Skip to content

Comments

Feat/logger#1353

Merged
Zeyi-Lin merged 9 commits intomainfrom
feat/logger
Dec 5, 2025
Merged

Feat/logger#1353
Zeyi-Lin merged 9 commits intomainfrom
feat/logger

Conversation

@SAKURA-CAT
Copy link
Member

  1. 支持SWANLAB_LOG_LEVEL环境变量设置swanlab打印日志等级
  2. 取消202响应后自动停止上传的特性
  3. 添加一些终端打印,包括:上传时遇见202、所有涉及后端的网络请求打印响应信息

for #1132

Introduced SWANLAB_LOG_LEVEL environment variable to control SwanLab's log output level. The log level now prioritizes the environment variable over the log_level argument, improving configurability.
Moved metrics upload batching logic from upload.py to a new batch.py module, introducing improved chunking and client state management. Updated related imports and test cases to use the new batch uploader. Enhanced logging in client/__init__.py and improved type handling in toolkit/logger.py.
Refactored logger initialization to support environment variable for log level and improved handling of invalid log levels. Added unit tests to verify environment variable log level and defaulting to 'info' for invalid levels.
@SAKURA-CAT SAKURA-CAT requested a review from Zeyi-Lin December 4, 2025 11:01
@SAKURA-CAT SAKURA-CAT self-assigned this Dec 4, 2025
@SAKURA-CAT SAKURA-CAT added 🐛 bug Something isn't working 💪 enhancement New feature or request labels Dec 4, 2025
Removed the client_state_guard decorator and integrated client state checks directly into the trace_metrics function. This streamlines the request logic and eliminates unnecessary indirection, making the code easier to follow and maintain.
Updated test_log.py to use instance-level logger management, allowing tests to operate on different SwanLog instances and improving test isolation. Modified start_proxy to accept a logger parameter and return both log file and logger. Minor improvements in batch.py to handle empty data in trace_metrics and expanded type hints.
Ensures that the LOG_LEVEL environment variable is deleted if present when resetting environment variables in reset_some_env.
Log level is now consistently set to lowercase in SwanLog. Test setup now resets swanlog to ensure proper cleanup, handling potential RuntimeError. Removed LOG_LEVEL env var deletion from reset_some_env for better environment management.
@Zeyi-Lin
Copy link
Member

Zeyi-Lin commented Dec 5, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 主要实现了三个功能:支持 SWANLAB_LOG_LEVEL 环境变量、取消 202 响应后自动停止上传的特性,以及为 HTTP 请求添加了更详细的日志记录。日志级别配置的实现很完善,正确处理了函数参数和环境变量之间的优先级。将上传逻辑重构到独立的 batch.py 文件中也改善了代码结构。但是,还存在一些需要解决的问题:新的 trace_metrics 函数中存在一个潜在的 TypeError,一个单元测试用例已经过时,不再反映当前逻辑,以及一些代码风格和可维护性方面的小改进点。具体建议请参见详细评论。

Centralized the '/house/metrics' URL as HOUSE_URL in upload.py and updated all references. Improved logging in client/__init__.py and batch.py. Fixed and clarified test for trace_metrics behavior when client is pending.
Updated trace_metrics to handle cases where data is None by defaulting to an empty list, preventing errors when calculating total_len.
@SAKURA-CAT
Copy link
Member Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the metric upload logic by moving batch processing functions (MetricDict, create_data, trace_metrics) from swanlab/core_python/uploader/upload.py to a new file, swanlab/core_python/uploader/batch.py. The trace_metrics function now includes a time.sleep(1) delay between batched requests and logs debug information for HTTP responses. Additionally, the PR introduces support for configuring the logging level via an environment variable (SWANLAB_LOG_LEVEL), updating swanlab/env.py, swanlab/log/log.py, and swanlab/data/sdk.py to handle this. The SwanKitLogger class in swanlab/toolkit/logger.py is also updated to improve log level handling and error prevention for invalid levels. Unit tests for logging and uploader functionalities have been adjusted to reflect these changes, including a modification in test_upload.py to assert that trace_metrics attempts an upload even if the client is in a pending state. A review comment suggests removing commented-out code related to client.pending checks in swanlab/core_python/uploader/batch.py for better code hygiene and alignment with the PR's objective of not automatically stopping uploads after a 202 response.

@Zeyi-Lin Zeyi-Lin merged commit 69ace65 into main Dec 5, 2025
5 checks passed
@SAKURA-CAT SAKURA-CAT deleted the feat/logger branch December 7, 2025 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working 💪 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants