Skip to content

perf: avoid eager formatting in debug log calls - #4929

Merged
Calcium-Ion merged 3 commits into
QuantumNous:mainfrom
seefs001:perf/log-debug
May 19, 2026
Merged

perf: avoid eager formatting in debug log calls#4929
Calcium-Ion merged 3 commits into
QuantumNous:mainfrom
seefs001:perf/log-debug

Conversation

@seefs001

@seefs001 seefs001 commented May 17, 2026

Copy link
Copy Markdown
Collaborator

⚠️ 提交说明 / 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 to use the project logger and structured formatting; several debug prints now emit via the logger unconditionally.
    • Centralized admin permission checks introduced for user management and OAuth/passkey/2FA admin actions.
  • Bug Fix

    • User access tokens removed from JSON outputs to prevent accidental exposure.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 17, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

Pull request was closed or merged during review

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

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: bb822263-b46c-4059-a7be-ad171d1f7c1f

📥 Commits

Reviewing files that changed from the base of the PR and between 3a0da08 and e4cc2a8.

📒 Files selected for processing (18)
  • controller/custom_oauth.go
  • controller/passkey.go
  • controller/twofa.go
  • controller/user.go
  • logger/logger.go
  • model/channel.go
  • model/channel_cache.go
  • model/user.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 (3)
  • relay/helper/stream_scanner.go
  • relay/channel/openai/relay-openai.go
  • relay/channel/api_request.go
🚧 Files skipped from review as they are similar to previous changes (8)
  • service/quota.go
  • relay/helper/price.go
  • relay/channel/gemini/relay-gemini-native.go
  • logger/logger.go
  • relay/channel/gemini/relay-gemini.go
  • model/channel_cache.go
  • service/token_counter.go
  • relay/mjproxy_handler.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 and centralize admin role checks via canManageTargetRole.

Changes

Debug logging format refactoring & small auth centralization

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, service/midjourney.go
Text request, embedding, image request, price helper, file detection/service, and Midjourney debug logs updated to call logger.LogDebug with format-string arguments instead of fmt.Sprintf or raw prints; Midjourney empty-response check adjusted.
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.
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, model/user.go
Added logger imports; replaced deferred/conditional println with logger.LogDebug; ensure channelsIDM init when nil and log before/after cache states; User.AccessToken JSON tag set to -.
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 labels

enhancement

Suggested reviewers

  • Calcium-Ion

"🐰 I hopped through lines to clean each log,
swapped prints for formats, neat as a cog.
When debug sleeps, JSON stays still—no fright,
now traces whisper clearly in the night.
A carrot-coded patch—small, but just right."

🚥 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 title accurately summarizes the main objective of the PR: optimizing debug logging by avoiding eager string formatting.
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.

@Calcium-Ion
Calcium-Ion merged commit 0936e25 into QuantumNous:main May 19, 2026
1 check was pending
@coderabbitai coderabbitai Bot mentioned this pull request May 21, 2026
reggie-lula pushed a commit to WhaleCrane/new-api-hz that referenced this pull request May 21, 2026
xyfacai pushed a commit to xyfacai/new-api that referenced this pull request May 30, 2026
SamuelSxy pushed a commit to SamuelSxy/new-api-rh that referenced this pull request Jun 7, 2026
fx247562340 pushed a commit to fx247562340/vancine-platform that referenced this pull request Jun 11, 2026
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
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.

2 participants