Skip to content

feat(logging): add Enabled(level) to Logger interface#578

Merged
gfyrag merged 1 commit into
mainfrom
feat/logger-enabled-level
Mar 23, 2026
Merged

feat(logging): add Enabled(level) to Logger interface#578
gfyrag merged 1 commit into
mainfrom
feat/logger-enabled-level

Conversation

@gfyrag
Copy link
Copy Markdown
Contributor

@gfyrag gfyrag commented Mar 23, 2026

Summary

  • Add Level type (DebugLevel, InfoLevel, ErrorLevel) and Enabled(level Level) bool method to the Logger interface
  • Allows callers to guard expensive log calls (e.g. WithFields + Debugf) and avoid allocations when the level is disabled
  • Implemented for LogrusLogger (via IsLevelEnabled) and HcLogLoggerAdapter (via GetLevel)
  • Mock regenerated

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:

if logger.Enabled(logging.DebugLevel) {
    logger.WithFields(map[string]any{...}).Debugf("...")
}

Zero allocation when the level is off.

Test plan

  • Existing tests pass (go test ./pkg/observe/log/...)
  • Verify in ledger-v3-poc that contention drops after guarding preloader debug logs

🤖 Generated with Claude Code

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>
@gfyrag gfyrag requested review from a team as code owners March 23, 2026 15:55
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 23, 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: 0543d53a-2c85-4f9a-b300-e47d0213d897

📥 Commits

Reviewing files that changed from the base of the PR and between af785cc and 4fc9caf.

📒 Files selected for processing (4)
  • pkg/observe/log/adapter_hclog.go
  • pkg/observe/log/adapter_logrus.go
  • pkg/observe/log/logging.go
  • pkg/observe/log/logging_generated.go

Walkthrough

Added level-checking capabilities to the logging infrastructure by introducing a new Level type with severity constants and implementing an Enabled() method across the logger interface and multiple adapters to help guard expensive log calls.

Changes

Cohort / File(s) Summary
Core Logging Interface
pkg/observe/log/logging.go
Introduced new Level type with DebugLevel, InfoLevel, ErrorLevel constants. Extended Logger interface with Enabled(level Level) bool method for guarding expensive log calls.
Mock Implementation
pkg/observe/log/logging_generated.go
Added mock implementation of Enabled() method to MockLogger with corresponding expectation recorder for test support.
Adapter Implementations
pkg/observe/log/adapter_hclog.go, pkg/observe/log/adapter_logrus.go
Implemented Enabled() method in both adapters with level mapping functions (toHcLogLevel() and toLogrusLevel()) to translate project levels to backend-specific levels. LogrusLogger extended with logger field propagated across derived loggers.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A rabbit's delight, so eager and keen,
New levels to check, the finest I've seen!
From hclog to logrus, the mappings align,
With Enabled methods that work oh so fine—
Log guards stand ready, no waste shall be mine! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding the Enabled(level) method to the Logger interface to support log-level checking.
Description check ✅ Passed The description clearly relates to the changeset, providing summary, motivation, and test plan for the Level type and Enabled method additions across the logging adapters.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/logger-enabled-level

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.

Comment thread pkg/observe/log/adapter_hclog.go
@gfyrag gfyrag added this pull request to the merge queue Mar 23, 2026
Merged via the queue into main with commit 21e6135 Mar 23, 2026
4 of 5 checks passed
@gfyrag gfyrag deleted the feat/logger-enabled-level branch March 23, 2026 16:01
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