Skip to content

Commit 2d08296

Browse files
authored
Merge pull request #396 from pohly/slog-helper
logr v1.4.1 + SetSlogLogger
2 parents 2086216 + e4deee8 commit 2d08296

File tree

10 files changed

+106
-26
lines changed

10 files changed

+106
-26
lines changed

.github/workflows/test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.18, 1.19, 1.20, 1.21]
7+
go-version: ["1.18", "1.19", "1.20", "1.21"]
88
platform: [ubuntu-latest, macos-latest, windows-latest]
99
runs-on: ${{ matrix.platform }}
1010
steps:
@@ -27,7 +27,7 @@ jobs:
2727
- name: Install Go
2828
uses: actions/setup-go@v1
2929
with:
30-
go-version: 1.18
30+
go-version: 1.21
3131
- name: Add GOBIN to PATH
3232
run: echo "PATH=$(go env GOPATH)/bin:$PATH" >>$GITHUB_ENV
3333
- name: Install dependencies

contextual_slog.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build go1.21
2+
// +build go1.21
3+
4+
/*
5+
Copyright 2021 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package klog
21+
22+
import (
23+
"log/slog"
24+
25+
"github.com/go-logr/logr"
26+
)
27+
28+
// SetSlogLogger reconfigures klog to log through the slog logger. The logger must not be nil.
29+
func SetSlogLogger(logger *slog.Logger) {
30+
SetLoggerWithOptions(logr.FromSlogHandler(logger.Handler()), ContextualLogger(true))
31+
}

contextual_slog_example_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//go:build go1.21
2+
// +build go1.21
3+
4+
/*
5+
Copyright 2021 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package klog_test
21+
22+
import (
23+
"log/slog"
24+
"os"
25+
26+
"k8s.io/klog/v2"
27+
)
28+
29+
func ExampleSetSlogLogger() {
30+
state := klog.CaptureState()
31+
defer state.Restore()
32+
33+
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
34+
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
35+
if a.Key == slog.TimeKey {
36+
// Avoid non-deterministic output.
37+
return slog.Attr{}
38+
}
39+
return a
40+
},
41+
})
42+
logger := slog.New(handler)
43+
klog.SetSlogLogger(logger)
44+
klog.Info("hello world")
45+
46+
// Output:
47+
// level=INFO msg="hello world"
48+
}

examples/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module k8s.io/klog/examples
33
go 1.13
44

55
require (
6-
github.com/go-logr/logr v1.3.0
6+
github.com/go-logr/logr v1.4.1
77
github.com/go-logr/zapr v1.2.3
88
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
99
go.uber.org/goleak v1.1.12

examples/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
7-
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
8-
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
7+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
8+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
99
github.com/go-logr/zapr v1.2.3 h1:a9vnzlIBPQBBkeaR9IuMUfmVOrQlkoC4YfPoFkX3T7A=
1010
github.com/go-logr/zapr v1.2.3/go.mod h1:eIauM6P8qSvTw5o2ez6UEAfGjQKrxQTl5EoK+Qa2oG4=
1111
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module k8s.io/klog/v2
22

3-
go 1.13
3+
go 1.18
44

5-
require github.com/go-logr/logr v1.3.0
5+
require github.com/go-logr/logr v1.4.1

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
2-
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
1+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
2+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=

klogr_slog.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"strconv"
2626
"time"
2727

28-
"github.com/go-logr/logr/slogr"
28+
"github.com/go-logr/logr"
2929

3030
"k8s.io/klog/v2/internal/buffer"
3131
"k8s.io/klog/v2/internal/serialize"
@@ -35,7 +35,7 @@ import (
3535

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

80-
func (l *klogger) WithAttrs(attrs []slog.Attr) slogr.SlogSink {
80+
func (l *klogger) WithAttrs(attrs []slog.Attr) logr.SlogSink {
8181
clone := *l
8282
clone.values = serialize.WithValues(l.values, sloghandler.Attrs2KVList(l.groups, attrs))
8383
return &clone
8484
}
8585

86-
func (l *klogger) WithGroup(name string) slogr.SlogSink {
86+
func (l *klogger) WithGroup(name string) logr.SlogSink {
8787
clone := *l
8888
if clone.groups != "" {
8989
clone.groups += "." + name
@@ -93,4 +93,4 @@ func (l *klogger) WithGroup(name string) slogr.SlogSink {
9393
return &clone
9494
}
9595

96-
var _ slogr.SlogSink = &klogger{}
96+
var _ logr.SlogSink = &klogger{}

klogr_slog_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import (
2727
"os"
2828
"time"
2929

30-
"github.com/go-logr/logr/slogr"
30+
"github.com/go-logr/logr"
31+
3132
"k8s.io/klog/v2"
3233
internal "k8s.io/klog/v2/internal/buffer"
3334
)
@@ -71,7 +72,7 @@ func ExampleBackground_Slog() {
7172
internal.Pid = 123
7273

7374
logrLogger := klog.Background()
74-
slogHandler := slogr.NewSlogHandler(logrLogger)
75+
slogHandler := logr.ToSlogHandler(logrLogger)
7576
slogLogger := slog.New(slogHandler)
7677

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

109110
// Output:
110-
// I1224 12:30:40.000000 123 klogr_slog_test.go:80] "A debug message"
111-
// I1224 12:30:40.000000 123 klogr_slog_test.go:82] "An info message"
112-
// W1224 12:30:40.000000 123 klogr_slog_test.go:83] "A warning"
113-
// E1224 12:30:40.000000 123 klogr_slog_test.go:84] "An error" err="fake error"
114-
// 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}
115-
// 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}
111+
// I1224 12:30:40.000000 123 klogr_slog_test.go:81] "A debug message"
112+
// I1224 12:30:40.000000 123 klogr_slog_test.go:83] "An info message"
113+
// W1224 12:30:40.000000 123 klogr_slog_test.go:84] "A warning"
114+
// E1224 12:30:40.000000 123 klogr_slog_test.go:85] "An error" err="fake error"
115+
// 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}
116+
// 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}
116117
}

textlogger/textlogger_slog.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"context"
2424
"log/slog"
2525

26-
"github.com/go-logr/logr/slogr"
26+
"github.com/go-logr/logr"
2727

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

36-
func (l *tlogger) WithAttrs(attrs []slog.Attr) slogr.SlogSink {
36+
func (l *tlogger) WithAttrs(attrs []slog.Attr) logr.SlogSink {
3737
clone := *l
3838
clone.values = serialize.WithValues(l.values, sloghandler.Attrs2KVList(l.groups, attrs))
3939
return &clone
4040
}
4141

42-
func (l *tlogger) WithGroup(name string) slogr.SlogSink {
42+
func (l *tlogger) WithGroup(name string) logr.SlogSink {
4343
clone := *l
4444
if clone.groups != "" {
4545
clone.groups += "." + name
@@ -49,4 +49,4 @@ func (l *tlogger) WithGroup(name string) slogr.SlogSink {
4949
return &clone
5050
}
5151

52-
var _ slogr.SlogSink = &tlogger{}
52+
var _ logr.SlogSink = &tlogger{}

0 commit comments

Comments
 (0)