Skip to content

Commit

Permalink
WIP: Fix coverage and make sloghandler slog compliant
Browse files Browse the repository at this point in the history
This is not yet concurrent-safe nor optimized.
  • Loading branch information
thockin committed Dec 6, 2023
1 parent 80c9c67 commit e4f3211
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 351 deletions.
1 change: 1 addition & 0 deletions funcr/slogsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (l fnlogger) WithAttrs(attrs []slog.Attr) logr.SlogSink {
}

func (l fnlogger) WithGroup(name string) logr.SlogSink {
//FIXME: if name == "" it's just inline
l.startGroup(name)
return &l
}
Expand Down
23 changes: 0 additions & 23 deletions logr_noslog_test.go

This file was deleted.

218 changes: 0 additions & 218 deletions logr_slog_test.go

This file was deleted.

79 changes: 0 additions & 79 deletions logr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,85 +24,6 @@ import (
"testing"
)

// testLogSink is a Logger just for testing that calls optional hooks on each method.
type testLogSink struct {
fnInit func(ri RuntimeInfo)
fnEnabled func(lvl int) bool
fnInfo func(lvl int, msg string, kv ...any)
fnError func(err error, msg string, kv ...any)
fnWithValues func(kv ...any)
fnWithName func(name string)

withValues []any

// testSlogSink contains some additional fields if (and only if) slog is supported by Go.
// See logr_slog_test.go.
//nolint:unused // Only unused with Go < 1.21.
testSlogSink
}

var _ LogSink = &testLogSink{}

func (l *testLogSink) Init(ri RuntimeInfo) {
if l.fnInit != nil {
l.fnInit(ri)
}
}

func (l *testLogSink) Enabled(lvl int) bool {
if l.fnEnabled != nil {
return l.fnEnabled(lvl)
}
return false
}

func (l *testLogSink) Info(lvl int, msg string, kv ...any) {
if l.fnInfo != nil {
l.fnInfo(lvl, msg, kv...)
}
}

func (l *testLogSink) Error(err error, msg string, kv ...any) {
if l.fnError != nil {
l.fnError(err, msg, kv...)
}
}

func (l *testLogSink) WithValues(kv ...any) LogSink {
if l.fnWithValues != nil {
l.fnWithValues(kv...)
}
out := *l
n := len(out.withValues)
out.withValues = append(out.withValues[:n:n], kv...)
return &out
}

func (l *testLogSink) WithName(name string) LogSink {
if l.fnWithName != nil {
l.fnWithName(name)
}
out := *l
return &out
}

type testCallDepthLogSink struct {
testLogSink
callDepth int
fnWithCallDepth func(depth int)
}

var _ CallDepthLogSink = &testCallDepthLogSink{}

func (l *testCallDepthLogSink) WithCallDepth(depth int) LogSink {
if l.fnWithCallDepth != nil {
l.fnWithCallDepth(depth)
}
out := *l
out.callDepth += depth
return &out
}

func TestNew(t *testing.T) {
calledInit := 0

Expand Down
Loading

0 comments on commit e4f3211

Please sign in to comment.