Conversation
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.
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.
|
/gemini review |
There was a problem hiding this comment.
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.
|
/gemini review |
There was a problem hiding this comment.
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.
SWANLAB_LOG_LEVEL环境变量设置swanlab打印日志等级for #1132