Skip to content

Commit

Permalink
slog: use main logr package instead of logr/slogr
Browse files Browse the repository at this point in the history
logr v1.4.0 moved the slog support functions in the main package and deprecated
slogr.
  • Loading branch information
pohly committed Jan 9, 2024
1 parent 5d1d2d5 commit e4deee8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
10 changes: 5 additions & 5 deletions klogr_slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"strconv"
"time"

"github.com/go-logr/logr/slogr"
"github.com/go-logr/logr"

"k8s.io/klog/v2/internal/buffer"
"k8s.io/klog/v2/internal/serialize"
Expand All @@ -35,7 +35,7 @@ import (

func (l *klogger) Handle(ctx context.Context, record slog.Record) error {
if logging.logger != nil {
if slogSink, ok := logging.logger.GetSink().(slogr.SlogSink); ok {
if slogSink, ok := logging.logger.GetSink().(logr.SlogSink); ok {
// Let that logger do the work.
return slogSink.Handle(ctx, record)
}
Expand Down Expand Up @@ -77,13 +77,13 @@ func slogOutput(file string, line int, now time.Time, err error, s severity.Seve
buffer.PutBuffer(b)
}

func (l *klogger) WithAttrs(attrs []slog.Attr) slogr.SlogSink {
func (l *klogger) WithAttrs(attrs []slog.Attr) logr.SlogSink {
clone := *l
clone.values = serialize.WithValues(l.values, sloghandler.Attrs2KVList(l.groups, attrs))
return &clone
}

func (l *klogger) WithGroup(name string) slogr.SlogSink {
func (l *klogger) WithGroup(name string) logr.SlogSink {
clone := *l
if clone.groups != "" {
clone.groups += "." + name
Expand All @@ -93,4 +93,4 @@ func (l *klogger) WithGroup(name string) slogr.SlogSink {
return &clone
}

var _ slogr.SlogSink = &klogger{}
var _ logr.SlogSink = &klogger{}
17 changes: 9 additions & 8 deletions klogr_slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import (
"os"
"time"

"github.com/go-logr/logr/slogr"
"github.com/go-logr/logr"

"k8s.io/klog/v2"
internal "k8s.io/klog/v2/internal/buffer"
)
Expand Down Expand Up @@ -71,7 +72,7 @@ func ExampleBackground_Slog() {
internal.Pid = 123

logrLogger := klog.Background()
slogHandler := slogr.NewSlogHandler(logrLogger)
slogHandler := logr.ToSlogHandler(logrLogger)
slogLogger := slog.New(slogHandler)

// Note that -vmodule does not work when using the slog API because
Expand Down Expand Up @@ -107,10 +108,10 @@ func ExampleBackground_Slog() {
)

// Output:
// I1224 12:30:40.000000 123 klogr_slog_test.go:80] "A debug message"
// I1224 12:30:40.000000 123 klogr_slog_test.go:82] "An info message"
// W1224 12:30:40.000000 123 klogr_slog_test.go:83] "A warning"
// E1224 12:30:40.000000 123 klogr_slog_test.go:84] "An error" err="fake error"
// I1224 12:30:40.000000 123 klogr_slog_test.go:87] "Grouping" top.sub={"str":"abc","bool":true,"bottom":{"coordinates":{"X":-1,"Y":-2}}} top.duration="1s" top.pi=3.12 top.e=2.71 top.moreCoordinates={"X":100,"Y":200}
// I1224 12:30:40.000000 123 klogr_slog_test.go:103] "slog values" variables={"a":1,"b":2} duration="1s" coordinates={"X":100,"Y":200}
// I1224 12:30:40.000000 123 klogr_slog_test.go:81] "A debug message"
// I1224 12:30:40.000000 123 klogr_slog_test.go:83] "An info message"
// W1224 12:30:40.000000 123 klogr_slog_test.go:84] "A warning"
// E1224 12:30:40.000000 123 klogr_slog_test.go:85] "An error" err="fake error"
// I1224 12:30:40.000000 123 klogr_slog_test.go:88] "Grouping" top.sub={"str":"abc","bool":true,"bottom":{"coordinates":{"X":-1,"Y":-2}}} top.duration="1s" top.pi=3.12 top.e=2.71 top.moreCoordinates={"X":100,"Y":200}
// I1224 12:30:40.000000 123 klogr_slog_test.go:104] "slog values" variables={"a":1,"b":2} duration="1s" coordinates={"X":100,"Y":200}
}
8 changes: 4 additions & 4 deletions textlogger/textlogger_slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"context"
"log/slog"

"github.com/go-logr/logr/slogr"
"github.com/go-logr/logr"

"k8s.io/klog/v2/internal/serialize"
"k8s.io/klog/v2/internal/sloghandler"
Expand All @@ -33,13 +33,13 @@ func (l *tlogger) Handle(ctx context.Context, record slog.Record) error {
return sloghandler.Handle(ctx, record, l.groups, l.printWithInfos)
}

func (l *tlogger) WithAttrs(attrs []slog.Attr) slogr.SlogSink {
func (l *tlogger) WithAttrs(attrs []slog.Attr) logr.SlogSink {
clone := *l
clone.values = serialize.WithValues(l.values, sloghandler.Attrs2KVList(l.groups, attrs))
return &clone
}

func (l *tlogger) WithGroup(name string) slogr.SlogSink {
func (l *tlogger) WithGroup(name string) logr.SlogSink {
clone := *l
if clone.groups != "" {
clone.groups += "." + name
Expand All @@ -49,4 +49,4 @@ func (l *tlogger) WithGroup(name string) slogr.SlogSink {
return &clone
}

var _ slogr.SlogSink = &tlogger{}
var _ logr.SlogSink = &tlogger{}

0 comments on commit e4deee8

Please sign in to comment.