Skip to content

Conversation

@jhaynie
Copy link
Member

@jhaynie jhaynie commented Dec 24, 2025

Summary

Add level checking capabilities to the Logger interface to allow programs to optimize fast-path checks.

Changes

Interface additions:

  • IsLevelEnabled(level LogLevel) bool - generic level check
  • IsTraceEnabled(), IsDebugEnabled(), IsInfoEnabled(), IsWarnEnabled(), IsErrorEnabled() - convenience methods

Implementations updated:

  • consoleLogger
  • jsonLogger
  • otelLogger
  • TestLogger (always returns true for testing)

Usage

Enables fast-path optimization to avoid expensive operations when logging is disabled:

if logger.IsDebugEnabled() {
    logger.Debug("expensive: %v", expensiveOperation())
}

Testing

Added TestConsoleLoggerIsLevelEnabled test covering all log levels.

Summary by CodeRabbit

  • New Features

    • Added per-level query methods to logging (ability to check if Trace/Debug/Info/Warn/Error are enabled).
  • Tests

    • Added tests covering level-enabled query behavior.
    • Updated DNS-related tests to stop asserting the resolv.conf search domain (retained nameserver and comment checks).

✏️ Tip: You can customize this high-level summary in your review settings.

Add level checking capabilities to the Logger interface:
- IsLevelEnabled(level LogLevel) bool - generic level check
- IsTraceEnabled(), IsDebugEnabled(), IsInfoEnabled(), IsWarnEnabled(), IsErrorEnabled() - convenience methods

Implemented in all logger types: consoleLogger, jsonLogger, otelLogger, TestLogger.
Enables fast-path optimization to avoid expensive operations when logging is disabled.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 24, 2025

Walkthrough

The PR adds level-enablement query methods to the Logger interface and its implementations, plus a unit test for console logger checks and a small test change in DNS resolv conf tests. No existing logging output behavior is modified.

Changes

Cohort / File(s) Summary
Logger Interface
logger/init.go
Added six query methods to Logger: IsLevelEnabled(level LogLevel) bool, IsTraceEnabled(), IsDebugEnabled(), IsInfoEnabled(), IsWarnEnabled(), IsErrorEnabled().
Logger Implementations
logger/console.go, logger/json.go, logger/otel.go, logger/test.go
Implemented the six methods on each logger type. console.go/json.go check logLevel and optional sinkLogLevel; test.go methods return true; otel.go also updated NewOtelLogger to set logLevel: LevelTrace.
Logger Tests
logger/console_test.go
Added TestConsoleLoggerIsLevelEnabled, table-driven tests validating IsLevelEnabled and the per-level predicates across configurations.
DNS Tests
dns/resolv_test.go
Removed assertions/verifications related to the search line from TestWriteResolvConf and TestWriteResolvConf_Content; search-line tracking variable/assertions dropped.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I hopped through logs with glee,
Six tiny checks now follow me.
Trace to Error, quick and neat,
Peeking levels on each beat.
A rabbit nods — everything's complete.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and specifically describes the main change: adding IsLevelEnabled and convenience level check methods to the logger package.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/logger-level-enabled-checks

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 603d5a9 and 4984559.

📒 Files selected for processing (1)
  • dns/resolv_test.go
💤 Files with no reviewable changes (1)
  • dns/resolv_test.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build
  • GitHub Check: Analyze (go)

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

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
logger/otel.go (1)

218-222: Critical bug: levels parameter ignored.

The function accepts a levels parameter but always initializes the logger with LevelTrace, ignoring the provided value. This prevents callers from configuring the log level for the otel logger.

🔎 Proposed fix
 func NewOtelLogger(otelsLogger otelLog.Logger, levels LogLevel) Logger {
 	return &otelLogger{
 		otelLogger: otelsLogger,
-		logLevel:   LevelTrace,
+		logLevel:   levels,
 	}
 }
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f5fcec3 and 603d5a9.

📒 Files selected for processing (6)
  • logger/console.go
  • logger/console_test.go
  • logger/init.go
  • logger/json.go
  • logger/otel.go
  • logger/test.go
🧰 Additional context used
🧬 Code graph analysis (5)
logger/test.go (2)
logger/init.go (1)
  • LogLevel (12-12)
env/env.go (1)
  • LogLevel (414-427)
logger/json.go (2)
logger/init.go (6)
  • LogLevel (12-12)
  • LevelTrace (15-15)
  • LevelDebug (16-16)
  • LevelInfo (17-17)
  • LevelWarn (18-18)
  • LevelError (19-19)
env/env.go (1)
  • LogLevel (414-427)
logger/init.go (1)
env/env.go (1)
  • LogLevel (414-427)
logger/console.go (2)
logger/init.go (6)
  • LogLevel (12-12)
  • LevelTrace (15-15)
  • LevelDebug (16-16)
  • LevelInfo (17-17)
  • LevelWarn (18-18)
  • LevelError (19-19)
env/env.go (1)
  • LogLevel (414-427)
logger/otel.go (2)
logger/init.go (6)
  • LogLevel (12-12)
  • LevelTrace (15-15)
  • LevelDebug (16-16)
  • LevelInfo (17-17)
  • LevelWarn (18-18)
  • LevelError (19-19)
env/env.go (1)
  • LogLevel (414-427)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build
  • GitHub Check: Analyze (go)
🔇 Additional comments (6)
logger/console_test.go (1)

322-396: LGTM! Comprehensive test coverage.

The table-driven test thoroughly validates the new level-enablement API across all log levels, checking both the generic IsLevelEnabled method and all convenience wrappers.

logger/json.go (1)

214-236: LGTM! Correct dual-threshold implementation.

The IsLevelEnabled method correctly accounts for both console and sink log levels, matching the behavior of the existing Log method. The convenience wrappers appropriately delegate to the generic method.

logger/test.go (1)

122-144: LGTM! Appropriate test fixture behavior.

Returning true for all level checks is the correct approach for a test fixture, ensuring the test logger doesn't interfere with test logic that relies on level enablement.

logger/init.go (1)

66-77: LGTM! Clean interface additions.

The new methods are well-documented and provide both a generic IsLevelEnabled check and convenient per-level wrappers, enhancing the API ergonomics without breaking existing code.

logger/otel.go (1)

194-216: LGTM! Consistent implementation.

The IsLevelEnabled method correctly checks only the logger's threshold (no sink logic), which matches the behavior of the log method at line 111. The convenience wrappers properly delegate to the generic method.

logger/console.go (1)

244-266: LGTM! Correct dual-threshold implementation.

The IsLevelEnabled method correctly accounts for both console and sink log levels, matching the behavior of the existing Log method at line 155. The convenience wrappers appropriately delegate to the generic method.

@jhaynie jhaynie merged commit 9570931 into main Dec 24, 2025
5 checks passed
@jhaynie jhaynie deleted the feat/logger-level-enabled-checks branch December 24, 2025 15:58
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