feat(logging): add Enabled(level) to Logger interface#578
Conversation
Add a Level type and Enabled(level Level) bool method to the Logger
interface, allowing callers to guard expensive log calls and avoid
unnecessary allocations when the level is disabled.
This is particularly important in hot paths where WithFields allocates
a map and a new logger wrapper even when the log will be discarded.
Usage:
if logger.Enabled(logging.DebugLevel) {
logger.WithFields(map[string]any{...}).Debugf("...")
}
Implemented for LogrusLogger (via IsLevelEnabled) and
HcLogLoggerAdapter (via GetLevel). Mock regenerated.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
WalkthroughAdded level-checking capabilities to the logging infrastructure by introducing a new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Summary
Leveltype (DebugLevel,InfoLevel,ErrorLevel) andEnabled(level Level) boolmethod to theLoggerinterfaceWithFields+Debugf) and avoid allocations when the level is disabledLogrusLogger(viaIsLevelEnabled) andHcLogLoggerAdapter(viaGetLevel)Motivation
In hot paths,
logger.WithFields(map[string]any{...}).Debugf(...)allocates a map and a new logger wrapper even when debug level is disabled. Profiling on ledger-v3-poc showed this pattern causing significant mutex contention in the OTEL/zap logging pipeline (~78% of contention samples).With this change, callers can write:
Zero allocation when the level is off.
Test plan
go test ./pkg/observe/log/...)🤖 Generated with Claude Code