Skip to content

perf: avoid eager formatting in debug log calls#4929

Open
seefs001 wants to merge 3 commits into
QuantumNous:mainfrom
seefs001:perf/log-debug
Open

perf: avoid eager formatting in debug log calls#4929
seefs001 wants to merge 3 commits into
QuantumNous:mainfrom
seefs001:perf/log-debug

Conversation

@seefs001
Copy link
Copy Markdown
Collaborator

@seefs001 seefs001 commented May 17, 2026

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)

对debug模式未开启情况下log.debug多余的分配开销进行优化

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

Summary by CodeRabbit

  • Refactor
    • Standardized debug logging across the codebase to use formatted project logger calls instead of ad-hoc printing or pre-formatted strings.
    • Replaced many conditional stdout prints with structured debug logging.
    • Short-circuited JSON marshaling when debug is disabled to avoid unnecessary work.
    • No public API or functional behavior changes beyond logging.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4c7ae0c5-252d-4089-9b27-302703834083

📥 Commits

Reviewing files that changed from the base of the PR and between c29fc7c and 025db64.

📒 Files selected for processing (13)
  • logger/logger.go
  • model/channel.go
  • model/channel_cache.go
  • relay/channel/api_request.go
  • relay/channel/claude/relay-claude.go
  • relay/channel/gemini/relay-gemini-native.go
  • relay/channel/gemini/relay-gemini.go
  • relay/channel/openai/relay-openai.go
  • relay/helper/price.go
  • relay/helper/stream_scanner.go
  • relay/mjproxy_handler.go
  • service/quota.go
  • service/token_counter.go
✅ Files skipped from review due to trivial changes (2)
  • relay/mjproxy_handler.go
  • relay/helper/stream_scanner.go

Walkthrough

Replace pre-formatted fmt.Sprintf/println debug messages with direct format-string + args to logger.LogDebug across controller, relay, model, and service packages; add LogJson early-return when debug is disabled.

Changes

Debug logging format refactoring

Layer / File(s) Summary
Logger core optimization
logger/logger.go
logHelper nil-safe id handling and LogJson short-circuits when common.DebugEnabled is false; JSON is marshaled and logged only in debug mode.
Video/task-polling logging
controller/task_video.go, service/task_polling.go
Task upstream response bodies, parsed payloads, and taskResult are logged via logger.LogDebug with format-string arguments instead of pre-formatting.
OpenAI multipart & SSE logging
relay/channel/openai/adaptor.go, relay/channel/openai/relay-openai.go
Multipart form fields, file upload parts, Content-Type header, and extracted audio-model SSE usage debug logs now pass format strings and args directly to logger.LogDebug.
Gemini relay logging
relay/channel/gemini/relay-gemini.go, relay/gemini_handler.go, relay/channel/gemini/relay-gemini-native.go
Gemini request/embedding request body logs, SendResponseCount, and response-body logging switched to formatted logger.LogDebug calls.
Ali image handler logging
relay/channel/ali/image.go
Async task wait and image-result debug logs now use logger.LogDebug with format strings and log originRespBody directly.
Generic handler & service logging
relay/compatible_handler.go, relay/embedding_handler.go, relay/gemini_handler.go, relay/helper/price.go, relay/image_handler.go, service/file_decoder.go, service/file_service.go
Text request, embedding, image request, price helper, and file service debug logs updated to call logger.LogDebug with format-string arguments instead of fmt.Sprintf or raw prints.
Relay handlers import & request-body logging
relay/claude_handler.go, relay/common_handler/rerank.go, relay/rerank_handler.go, relay/responses_handler.go
Files add logger imports and replace conditional println debug prints with logger.LogDebug calls for request/response bodies.
Midjourney logging switch
service/midjourney.go
Replaced log.Printf with logger.LogDebug for Midjourney response-body logging and changed empty-response check to len(responseBody) == 0.
Channel API / SSE ping logging
relay/channel/api_request.go
DoApiRequest/DoFormRequest and SSE ping keepalive goroutine now use logger.LogDebug/LogError for resolved URLs, panic recovery, tick/stop/start, ping errors, and ping success.
Stream scanner & other relay logs
relay/helper/stream_scanner.go, relay/mjproxy_handler.go, relay/channel/claude/relay-claude.go
Stream scanner lifecycle, per-SSE-line, ping lifecycle, and several relay debug prints converted to structured logger.LogDebug calls.
Model channel & cache logging
model/channel.go, model/channel_cache.go
Added logger imports; replaced deferred/conditional println with logger.LogDebug; ensure channelsIDM init when nil and log before/after cache states.
Token counter and quota logging
service/token_counter.go, service/quota.go
Switched image-token and final-group-ratio debug logs from log.Printf to logger.LogDebug and adjusted imports.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • creamlike1024

Poem

🐰 I hopped through lines to change each log,

Swapped prints for formats, trimmed the fog.
When debug sleeps I keep JSON still,
Now logs behave with tidy will,
A carrot-coded trail—clean and cog.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title directly and accurately summarizes the main change: refactoring debug logging calls to avoid eager string formatting when debug mode is disabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant