Repository: istio-ecosystem/authservice
Pull request #276 (2025-03-23 11:25:11)

fix logr configuration
Fixes the logging bridge configuration as it was not properly honoring the logging level configuration.

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/istio-ecosystem/authservice/pull/276?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=istio-ecosystem) Report
Attention: Patch coverage is `84.61538%` with `2 lines` in your changes missing coverage. Please review.
> Please [upload](https://docs.codecov.com/docs/codecov-uploader) report for BASE (`main@3702a37`). [Learn more](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=istio-ecosystem#section-missing-base-commit) about missing BASE report.

| [Files with missing lines](https://app.codecov.io/gh/istio-ecosystem/authservice/pull/276?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=istio-ecosystem) | Patch % | Lines |
|---|---|---|
| [internal/logr.go](https://app.codecov.io/gh/istio-ecosystem/authservice/pull/276?src=pr&el=tree&filepath=internal%2Flogr.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=istio-ecosystem#diff-aW50ZXJuYWwvbG9nci5nbw==) | 84.61% | [2 Missing :warning: ](https://app.codecov.io/gh/istio-ecosystem/authservice/pull/276?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=istio-ecosystem) |

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #276   +/-   ##
=======================================
  Coverage        ?   90.28%           
=======================================
  Files           ?       25           
  Lines           ?     2049           
  Branches        ?        0           
=======================================
  Hits            ?     1850           
  Misses          ?      156           
  Partials        ?       43           
```

| [Files with missing lines](https://app.codecov.io/gh/istio-ecosystem/authservice/pull/276?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=istio-ecosystem) | Coverage Δ | |
|---|---|---|
| [internal/logr.go](https://app.codecov.io/gh/istio-ecosystem/authservice/pull/276?src=pr&el=tree&filepath=internal%2Flogr.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=istio-ecosystem#diff-aW50ZXJuYWwvbG9nci5nbw==) | `94.73% <84.61%> (ø)` | |

</details>
<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>

Patch:
diff --git a/internal/logr.go b/internal/logr.go
index e0673ff..278bcff 100644
--- a/internal/logr.go
+++ b/internal/logr.go
@@ -26,7 +26,7 @@ const debugLevelThreshold = 5
 // logrAdapter is a type that adapts the log.Logger interface so it can be used with our loggers
 type logrAdapter struct {
 	scope telemetry.Logger
-	kvs   map[string]interface{}
+	kvs   []any
 }
 
 // NewLogrAdapter creates a new logger to bridge the logr.Logger to our logging system
@@ -38,19 +38,25 @@ func NewLogrAdapter(s telemetry.Logger) logr.Logger {
 func (l *logrAdapter) Init(_ logr.RuntimeInfo) {}
 
 func (l *logrAdapter) Enabled(level int) bool {
-	return int(l.scope.Level()) >= level
+	switch l.scope.Level() {
+	case telemetry.LevelDebug:
+		return true
+	case telemetry.LevelInfo | telemetry.LevelError:
+		return level < debugLevelThreshold
+	default: // telemetry.LevelNone
+		return false
+	}
 }
 
-func (l *logrAdapter) Info(_ int, msg string, kvs ...interface{}) {
+func (l *logrAdapter) Info(level int, msg string, kvs ...interface{}) {
 	if len(kvs)%2 != 0 {
 		kvs = append(kvs, "(MISSING)")
 	}
-	logger := l.scope.With(kvs...)
 
-	if l.scope.Level() > debugLevelThreshold {
-		logger.Debug(msg)
+	if level >= debugLevelThreshold {
+		l.scope.Debug(msg, append(l.kvs, kvs...)...)
 	} else {
-		logger.Info(msg)
+		l.scope.Info(msg, append(l.kvs, kvs...)...)
 	}
 }
 
@@ -58,8 +64,7 @@ func (l *logrAdapter) Error(err error, msg string, kvs ...interface{}) {
 	if len(kvs)%2 != 0 {
 		kvs = append(kvs, "(MISSING)")
 	}
-	logger := l.scope.With(kvs...)
-	logger.Error(msg, err)
+	l.scope.Error(msg, err, append(l.kvs, kvs...)...)
 }
 
 func (l *logrAdapter) WithName(string) logr.LogSink { return l }
@@ -68,19 +73,11 @@ func (l *logrAdapter) WithValues(kvs ...interface{}) logr.LogSink {
 	if len(kvs) == 0 {
 		return l
 	}
-
 	if len(kvs)%2 != 0 {
 		kvs = append(kvs, "(MISSING)")
 	}
-	all := make(map[string]interface{}, len(l.kvs)+len(kvs)/2)
-	for k, v := range l.kvs {
-		all[k] = v
-	}
-	for i := 0; i < len(kvs); i += 2 {
-		all[kvs[i].(string)] = kvs[i+1]
-	}
 	return &logrAdapter{
 		scope: l.scope,
-		kvs:   all,
+		kvs:   append(l.kvs, kvs...),
 	}
 }


Test patch:
diff --git a/internal/logr_test.go b/internal/logr_test.go
index 230ccc5..e8b2daf 100644
--- a/internal/logr_test.go
+++ b/internal/logr_test.go
@@ -32,7 +32,7 @@ func TestWithValues(t *testing.T) {
 	a = a.WithValues("three")
 	a = a.WithValues()
 
-	require.Equal(t, map[string]interface{}{"one": 1, "two": 2, "three": "(MISSING)"}, a.(*logrAdapter).kvs)
+	require.Equal(t, []any{"one", 1, "two", 2, "three", "(MISSING)"}, a.(*logrAdapter).kvs)
 }
 
 func TestWithName(t *testing.T) {
@@ -44,13 +44,15 @@ func TestWithName(t *testing.T) {
 
 func TestEnable(t *testing.T) {
 	logger := log.New()
+	a := logrAdapter{scope: logger}
+
 	logger.SetLevel(telemetry.LevelInfo)
-	a := logrAdapter{
-		scope: logger,
-	}
-	require.False(t, a.Enabled(int(telemetry.LevelDebug)))
-	require.True(t, a.Enabled(int(telemetry.LevelInfo)))
-	require.True(t, a.Enabled(int(telemetry.LevelError)))
+	require.False(t, a.Enabled(debugLevelThreshold))
+	require.True(t, a.Enabled(debugLevelThreshold-1))
+
+	logger.SetLevel(telemetry.LevelDebug)
+	require.True(t, a.Enabled(debugLevelThreshold))
+	require.True(t, a.Enabled(debugLevelThreshold-1))
 }
 
 func TestInfo(t *testing.T) {
@@ -83,7 +85,7 @@ func TestDebug(t *testing.T) {
 		scope: logger,
 	}
 
-	a.Info(0, "test", "one", 1, "two", 2)
+	a.Info(debugLevelThreshold, "test", "one", 1, "two", 2)
 
 	_ = w.Close()
 	out, _ := io.ReadAll(r)