Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
k8s/logger: reimplement k8s logger as LogSink
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorris committed Jun 7, 2022
1 parent 49269bf commit 313218d
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions internal/k8s/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,38 @@ import (
)

func fromHCLogger(log hclog.Logger) logr.Logger {
return &logger{log}
return logr.New(&logger{log})
}

// logger is a LogSink that wraps hclog
type logger struct {
hclog.Logger
}

func (l *logger) Enabled() bool {
// Verify that it actually implements the interface
var _ logr.LogSink = logger{}

func (l logger) Init(logr.RuntimeInfo) {
}

func (l logger) Enabled(_ int) bool {
return true
}

func (l *logger) Error(err error, msg string, keysAndValues ...interface{}) {
keysAndValues = append([]interface{}{"error", err}, keysAndValues...)
l.Logger.Error(msg, keysAndValues...)
func (l logger) Info(_ int, msg string, keysAndValues ...interface{}) {
keysAndValues = append([]interface{}{"info", msg}, keysAndValues...)
l.Logger.Info(msg, keysAndValues...)
}

func (l *logger) V(_ int) logr.Logger {
return l
func (l logger) Error(err error, msg string, keysAndValues ...interface{}) {
keysAndValues = append([]interface{}{"error", err}, keysAndValues...)
l.Logger.Error(msg, keysAndValues...)
}

func (l *logger) WithValues(keysAndValues ...interface{}) logr.Logger {
func (l logger) WithValues(keysAndValues ...interface{}) logr.LogSink {
return &logger{l.With(keysAndValues...)}
}

func (l *logger) WithName(name string) logr.Logger {
func (l logger) WithName(name string) logr.LogSink {
return &logger{l.Named(name)}
}

0 comments on commit 313218d

Please sign in to comment.