Add controller-runtime logging adapter using commons/logger with level shifting#26
Add controller-runtime logging adapter using commons/logger with level shifting#26
Conversation
…t warn level - Add logging.go with levelShiftHandler that adapts controller-runtime logs to flanksource/commons/logger, shifting levels down (info→debug, debug→trace, error→warn) via slog.Handler wrapper - Add NewControllerRuntimeLogger() using commons/logger with default warn level - Update manager.go to use the new adapter instead of logr.Discard() - Add comprehensive logging_test.go verifying level shifting, suppression, WithAttrs/WithGroup propagation, and default warn level behavior Co-authored-by: moshloop <1489660+moshloop@users.noreply.github.com>
logging.go
Outdated
| } | ||
|
|
||
| func (h *levelShiftHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
| return h.handler.Enabled(ctx, level+slogLevelShift) |
There was a problem hiding this comment.
add unit tests for slog level shift
There was a problem hiding this comment.
Added TestShiftLevel in 24d78ea — table-driven tests covering all slog level mappings including Error→Warn, Warn→Info, Info→Debug, Debug→Trace, V(1-3)→Trace, and below-Debug→below-Trace.
logging.go
Outdated
| // - controller-runtime Error (slog 8) → commons Warn (slog 4) | ||
| // - controller-runtime Info/V(0) (slog 0) → commons Debug (slog -4) | ||
| // - controller-runtime V(1) (slog -1) → commons Trace (slog -5) | ||
| const slogLevelShift = slog.Level(-4) |
There was a problem hiding this comment.
commons log level increase the more verbose they are,
slog Debug should map the Logger.Trace
There was a problem hiding this comment.
@copilot commons log level increase the more verbose they are,
slog Debug should map the Logger.Trace
There was a problem hiding this comment.
Fixed in 24d78ea. Replaced the constant -4 slog shift with a non-linear shiftLevel() function that maps each slog level down one semantic step: Error→Warn, Warn→Info, Info→Debug, Debug→Trace. slog.LevelDebug (-4) now correctly maps to logger.SlogTraceLevel (-5) / commons Trace.
slog Debug now correctly maps to commons Trace (slog -5) instead of being shifted by a constant -4 to slog -8. Added TestShiftLevel unit tests for slog level shift, and additional Enabled tests for Debug→Trace mapping. Co-authored-by: moshloop <1489660+moshloop@users.noreply.github.com>
| func shiftLevel(level slog.Level) slog.Level { | ||
| switch { | ||
| case level >= slog.LevelError: | ||
| return slog.LevelWarn |
There was a problem hiding this comment.
I would keep errors as errors
Replace
logr.Discard()with aflanksource/commons/loggeradapter that shifts controller-runtime log levels down one semantic level (info→debug, debug→trace, error→warn), defaulting to warn to suppress controller-runtime noise.logging.go: Non-linearshiftLevel()function maps each slog level band down one semantic step to commons/logger levels.levelShiftHandlerwrapsslog.Handlerwith this mapping.NewControllerRuntimeLogger()creates a named"controller-runtime"logger at warn level and converts vialogr.FromSlogHandler()manager.go: Replace bothlogr.Discard()calls withNewControllerRuntimeLogger()logging_test.go: Unit tests forshiftLevel()slog level mapping, level shifting via logr,Enabled()gating,WithAttrs/WithGrouppropagation, and default warn-level suppression🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.