Skip to content

feat: add internal diagnostic log & console/file logging - #1479

Merged
SAKURA-CAT merged 8 commits into
mainfrom
feat/log
Mar 9, 2026
Merged

SAKURA-CAT merged 8 commits into
mainfrom
feat/log

Conversation

@SAKURA-CAT

Copy link
Copy Markdown
Member

引入一个新的内部诊断日志模块(swanlab.sdk.internal.pkg.log),该模块具有内存缓冲启动功能、RotatingFileHandler绑定(bindfile)以及重置语义。

将控制台与诊断日志集成(当启用SWANLAB_DEBUG时,控制台输出将同步记录到日志中),并调整导入以使用internal.pkg.console。

增强核心客户端会话功能,以记录HTTP请求时间、跟踪ID,并通过新日志模块写入调试/错误诊断信息。新增针对日志生命周期和bindfile行为的单元测试,并更新测试配置(conftest),确保在测试间重置诊断日志记录器和客户端。

对于 SWANLAB_DEBUG 的效果如下,当 SWANLAB_DEBUG 开启(设置为["true", "1", "yes", "on"]之一)时:

  1. 所有console.log会打印至日志文件中
  2. 网络日志会将请求体、响应体(前1k字符)打印至日志文件中

for #1132

Introduce a new internal diagnostic logging module (swanlab.sdk.internal.pkg.log) with memory-buffered startup, RotatingFileHandler binding (bindfile), and reset semantics. Integrate console with the diagnostic log (console now mirrors output to the log when SWANLAB_DEBUG is enabled) and adjust imports to use internal.pkg.console. Enhance core client session to record HTTP request timings, trace IDs and write debug/error diagnostics via the new log module. Add unit tests for the log lifecycle and bindfile behavior and update test conftest to reset the diagnostic logger and client between tests.
Introduce DEBUG-driven HTTP request/response logging and tests. Added a DEBUG flag in swanlab.sdk.pkg.helper.env and updated SessionWithRetry to log request/response headers and bodies when DEBUG is enabled (with a 1000-char truncation and explicit markers). Console debug now checks helper.env.DEBUG instead of a local env var. Unit tests were added to cover successful responses, error responses (including original error body), and truncation behavior. Minor imports adjusted to use the shared helper.env.DEBUG.
Introduce format_body_preview to safely decode and truncate request/response bodies (handles bytes with utf-8 replace, returns empty for None, appends " ... (truncated)" when > max_len). Integrate it into SessionWithRetry debug logging to replace ad-hoc slicing and provide safer previews (including error-response logging). Update tests: add unit tests for format_body_preview, adapt DEBUG logging tests to assert formatted args and truncation behavior, and import the new helper.
@SAKURA-CAT
SAKURA-CAT requested a review from Nexisato March 9, 2026 09:42
@SAKURA-CAT SAKURA-CAT self-assigned this Mar 9, 2026
@SAKURA-CAT SAKURA-CAT added the 💪 enhancement New feature or request label Mar 9, 2026
@SAKURA-CAT
SAKURA-CAT requested a review from Zeyi-Lin March 9, 2026 09:42
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在显著提升 SwanLab SDK 的内部可观测性和调试能力。通过引入一个全新的、功能丰富的诊断日志模块,并将其与现有的控制台输出以及核心网络请求逻辑深度集成,开发者现在可以在 SWANLAB_DEBUG 模式下获得更详细、更持久的内部运行信息。这不仅有助于快速定位和解决问题,还能在不影响用户体验的前提下,提供强大的后台诊断支持。

Highlights

  • 新增内部诊断日志模块: 引入了新的内部诊断日志模块 (swanlab.sdk.internal.pkg.log),支持内存缓冲启动、RotatingFileHandler 文件绑定以及重置功能,用于更深入的内部诊断。
  • 控制台与诊断日志集成: 当环境变量 SWANLAB_DEBUG 启用时,控制台输出将同步镜像到诊断日志文件中,便于统一查看所有调试信息。
  • 增强核心客户端网络日志: 核心客户端会话功能得到增强,现在可以记录 HTTP 请求时间、跟踪 ID,并在调试模式下详细记录请求体和响应体(前1K字符),极大地提升了网络请求的调试能力。
  • 模块路径调整: 将 console 模块从 swanlab.sdk.pkg 移动到 swanlab.sdk.internal.pkg,并更新了所有相关导入路径,以更好地组织内部包结构。
  • 新增和更新单元测试: 新增了针对诊断日志模块生命周期、文件绑定行为以及客户端会话网络日志的单元测试,并更新了 conftest.py 以确保测试环境的正确隔离和重置。

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • swanlab/sdk/cmd/login.py
    • 更新了 console 模块的导入路径。
  • swanlab/sdk/internal/apikey/init.py
    • 更新了 console 模块的导入路径。
  • swanlab/sdk/internal/core_python/client/init.py
    • 更新了 console 模块的导入路径。
  • swanlab/sdk/internal/core_python/client/session.py
    • 引入了 time 模块和新的日志工具。
    • 添加了 format_body_preview 函数,用于格式化和截断请求/响应体。
    • 修改了 send 方法以记录 HTTP 请求时间、跟踪ID以及在调试模式下的请求/响应体预览。
  • swanlab/sdk/internal/pkg/log/init.py
    • 添加了新的内部诊断日志模块,实现了基于 logging 标准库的内存缓冲、文件轮转以及日志重置功能。
  • swanlab/sdk/internal/settings/init.py
    • 更新了 console 模块的导入路径。
  • swanlab/sdk/pkg/console/init.py
    • 重命名并移动到 swanlab/sdk/internal/pkg/console/init.py。
    • 移除了 os 导入,引入了 log 和 helper 模块。
    • 新增了 _to_plain_text 函数,用于将 rich markup 转换为纯文本。
    • 修改了日志函数以在 SWANLAB_DEBUG 模式下将输出镜像到诊断日志。
  • swanlab/sdk/pkg/helper/env.py
    • 添加了 DEBUG 环境变量的判断逻辑。
  • tests/unit/conftest.py
    • 导入了 log 模块。
    • 在测试清理阶段增加了对诊断日志记录器的重置。
  • tests/unit/sdk/internal/core_python/client/test_client_session.py
    • 导入了 MagicMock 和 format_body_preview。
    • 新增了对 format_body_preview 函数以及客户端会话调试日志记录的单元测试。
  • tests/unit/sdk/internal/pkg/test_log.py
    • 添加了新的单元测试文件,用于验证诊断日志模块的生命周期、文件绑定行为和日志格式。
Activity
  • 此拉取请求自创建以来,尚未有外部评审活动或评论。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

Change the function signature from `bytes | str | None` to `Optional[bytes | str]` for format_body_preview to standardize the typing annotation and improve readability. This is a type-only change with no behavioral impact.
Import typing.Union and replace PEP 604 style union (bytes | str) with Optional[Union[bytes, str]] in format_body_preview's type hint. This ensures the annotation is compatible with Python versions and tooling that don't support the X | Y union syntax.
Replace the private alias _log with log and make console handlers (info, warning, error, critical) forward messages to the internal logger only when helper.env.DEBUG is true. Debug logging still requires DEBUG (behavior unchanged except for the import). This avoids emitting duplicate/noisy logs in non-debug environments. (modified: swanlab/sdk/internal/pkg/console/__init__.py)

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This PR introduces a well-designed internal diagnostic logging module, integrating it with the console and HTTP client, which is a great feature enhancement. While aspects like memory buffering, delayed binding, and enhanced HTTP session logging are commendable, a critical security concern has been identified. When SWANLAB_DEBUG is enabled, the current implementation logs sensitive information such as session tokens and potentially API keys in plain text to a log file. Furthermore, the log file is created with default system permissions, potentially allowing unauthorized access in multi-user environments. To address these risks, it is highly recommended to implement sanitization for sensitive headers and bodies before logging and to restrict log file permissions to the current user.

Comment thread swanlab/sdk/internal/core_python/client/session.py
Comment thread swanlab/sdk/internal/core_python/client/session.py
Comment thread swanlab/sdk/internal/core_python/client/session.py
Comment thread swanlab/sdk/internal/pkg/console/__init__.py
Comment thread swanlab/sdk/internal/pkg/log/__init__.py Outdated
Comment thread swanlab/sdk/internal/core_python/client/session.py
Add SecureRotatingFileHandler that enforces 0o600 file permissions when opening/creating the log file on POSIX systems (to reduce risk of credential leakage on shared clusters). Replace RotatingFileHandler with SecureRotatingFileHandler in bindfile. Add unit test (test_secure_file_permissions) to validate the generated debug.log has 0600 permissions on POSIX; import os/stat in tests. Windows behavior unchanged (permission enforcement skipped).
Add type annotations and small refactors to improve typing and clarity. In session.py, annotate request_retries_ctx as ContextVar[Optional[int]] and introduce local variables for request.body and response.text when formatting debug previews to avoid repeated property access. In console/__init__.py, import Any and annotate _to_plain_text(*args: Any) to make varargs typing explicit. These changes are purely local/clarity improvements to aid static checking and logging readability.
@SAKURA-CAT
SAKURA-CAT merged commit 02d4932 into main Mar 9, 2026
21 checks passed
@SAKURA-CAT
SAKURA-CAT deleted the feat/log branch March 9, 2026 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

💪 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants