Skip to content

Commit d55b4e2

Browse files
authored
Merge pull request #241 from thockin/master
Add SlogSink support to funcr
2 parents c589653 + 98ee9d9 commit d55b4e2

22 files changed

+1194
-556
lines changed

.github/workflows/lint.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ jobs:
1111
steps:
1212
- name: Checkout code
1313
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
14+
- name: Update Go
15+
uses: actions/setup-go@v4
16+
with:
17+
go-version: '>=1.21.0'
18+
cache: false
1419
- name: Lint
1520
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3.7.0
1621
with:

benchmark/benchmark_slog_test.go

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//go:build go1.21
2+
// +build go1.21
3+
4+
/*
5+
Copyright 2021 The logr 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 logr
21+
22+
import (
23+
"log/slog"
24+
"os"
25+
"testing"
26+
27+
"github.com/go-logr/logr"
28+
)
29+
30+
//
31+
// slogSink wrapper of discard
32+
//
33+
34+
func BenchmarkSlogSinkLogInfoOneArg(b *testing.B) {
35+
var log logr.Logger = logr.FromSlogHandler(logr.ToSlogHandler(logr.Discard()))
36+
doInfoOneArg(b, log)
37+
}
38+
39+
func BenchmarkSlogSinkLogInfoSeveralArgs(b *testing.B) {
40+
var log logr.Logger = logr.FromSlogHandler(logr.ToSlogHandler(logr.Discard()))
41+
doInfoSeveralArgs(b, log)
42+
}
43+
44+
func BenchmarkSlogSinkLogInfoWithValues(b *testing.B) {
45+
var log logr.Logger = logr.FromSlogHandler(logr.ToSlogHandler(logr.Discard()))
46+
doInfoWithValues(b, log)
47+
}
48+
49+
func BenchmarkSlogSinkLogV0Info(b *testing.B) {
50+
var log logr.Logger = logr.FromSlogHandler(logr.ToSlogHandler(logr.Discard()))
51+
doV0Info(b, log)
52+
}
53+
54+
func BenchmarkSlogSinkLogV9Info(b *testing.B) {
55+
var log logr.Logger = logr.FromSlogHandler(logr.ToSlogHandler(logr.Discard()))
56+
doV9Info(b, log)
57+
}
58+
59+
func BenchmarkSlogSinkLogError(b *testing.B) {
60+
var log logr.Logger = logr.FromSlogHandler(logr.ToSlogHandler(logr.Discard()))
61+
doError(b, log)
62+
}
63+
64+
func BenchmarkSlogSinkWithValues(b *testing.B) {
65+
var log logr.Logger = logr.FromSlogHandler(logr.ToSlogHandler(logr.Discard()))
66+
doWithValues(b, log)
67+
}
68+
69+
func BenchmarkSlogSinkWithName(b *testing.B) {
70+
var log logr.Logger = logr.FromSlogHandler(logr.ToSlogHandler(logr.Discard()))
71+
doWithName(b, log)
72+
}
73+
74+
//
75+
// slogSink wrapper of slog's JSONHandler, for comparison
76+
//
77+
78+
func makeSlogJSONLogger() logr.Logger {
79+
devnull, _ := os.Open("/dev/null")
80+
handler := slog.NewJSONHandler(devnull, nil)
81+
return logr.FromSlogHandler(handler)
82+
}
83+
84+
func BenchmarkSlogJSONLogInfoOneArg(b *testing.B) {
85+
var log logr.Logger = makeSlogJSONLogger()
86+
doInfoOneArg(b, log)
87+
}
88+
89+
func BenchmarkSlogJSONLogInfoSeveralArgs(b *testing.B) {
90+
var log logr.Logger = makeSlogJSONLogger()
91+
doInfoSeveralArgs(b, log)
92+
}
93+
94+
func BenchmarkSlogJSONLogInfoWithValues(b *testing.B) {
95+
var log logr.Logger = makeSlogJSONLogger()
96+
doInfoWithValues(b, log)
97+
}
98+
99+
func BenchmarkSlogJSONLogV0Info(b *testing.B) {
100+
var log logr.Logger = makeSlogJSONLogger()
101+
doV0Info(b, log)
102+
}
103+
104+
func BenchmarkSlogJSONLogV9Info(b *testing.B) {
105+
var log logr.Logger = makeSlogJSONLogger()
106+
doV9Info(b, log)
107+
}
108+
109+
func BenchmarkSlogJSONLogError(b *testing.B) {
110+
var log logr.Logger = makeSlogJSONLogger()
111+
doError(b, log)
112+
}
113+
114+
func BenchmarkSlogJSONLogWithValues(b *testing.B) {
115+
var log logr.Logger = makeSlogJSONLogger()
116+
doWithValues(b, log)
117+
}
118+
119+
func BenchmarkSlogJSONWithName(b *testing.B) {
120+
var log logr.Logger = makeSlogJSONLogger()
121+
doWithName(b, log)
122+
}
123+
124+
func BenchmarkSlogJSONWithCallDepth(b *testing.B) {
125+
var log logr.Logger = makeSlogJSONLogger()
126+
doWithCallDepth(b, log)
127+
}
128+
129+
func BenchmarkSlogJSONLogInfoStringerValue(b *testing.B) {
130+
var log logr.Logger = makeSlogJSONLogger()
131+
doStringerValue(b, log)
132+
}
133+
134+
func BenchmarkSlogJSONLogInfoErrorValue(b *testing.B) {
135+
var log logr.Logger = makeSlogJSONLogger()
136+
doErrorValue(b, log)
137+
}
138+
139+
func BenchmarkSlogJSONLogInfoMarshalerValue(b *testing.B) {
140+
var log logr.Logger = makeSlogJSONLogger()
141+
doMarshalerValue(b, log)
142+
}

benchmark/benchmark_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ func doMarshalerValue(b *testing.B, log logr.Logger) {
141141
}
142142
}
143143

144+
//
145+
// discard
146+
//
147+
144148
func BenchmarkDiscardLogInfoOneArg(b *testing.B) {
145149
var log logr.Logger = logr.Discard()
146150
doInfoOneArg(b, log)
@@ -181,6 +185,10 @@ func BenchmarkDiscardWithName(b *testing.B) {
181185
doWithName(b, log)
182186
}
183187

188+
//
189+
// funcr
190+
//
191+
184192
func noopKV(_, _ string) {}
185193
func noopJSON(_ string) {}
186194

example_marshaler_secret_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ func ExampleMarshaler_secret() {
4343
secret := ComplexObjectRef{Namespace: "kube-system", Name: "some-secret", Secret: "do-not-log-me"}
4444
l.Info("simplified", "secret", secret)
4545
// Output:
46-
// "level"=0 "msg"="simplified" "secret"={"Name":"some-secret","Namespace":"kube-system"}
46+
// "level"=0 "msg"="simplified" "secret"={"Name"="some-secret" "Namespace"="kube-system"}
4747
}

example_marshaler_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ func ExampleMarshaler() {
5151
l.Info("as struct", "pod", pod)
5252
// Output:
5353
// "level"=0 "msg"="as string" "pod"="kube-system/some-pod"
54-
// "level"=0 "msg"="as struct" "pod"={"name":"some-pod","namespace":"kube-system"}
54+
// "level"=0 "msg"="as struct" "pod"={"name"="some-pod" "namespace"="kube-system"}
5555
}

example_slogr_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//go:build go1.21
2+
// +build go1.21
3+
4+
/*
5+
Copyright 2023 The logr 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 logr_test
21+
22+
import (
23+
"errors"
24+
"fmt"
25+
"log/slog"
26+
"os"
27+
28+
"github.com/go-logr/logr"
29+
"github.com/go-logr/logr/funcr"
30+
)
31+
32+
var debugWithoutTime = &slog.HandlerOptions{
33+
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
34+
if a.Key == "time" {
35+
return slog.Attr{}
36+
}
37+
return a
38+
},
39+
Level: slog.LevelDebug,
40+
}
41+
42+
func ExampleFromSlogHandler() {
43+
logrLogger := logr.FromSlogHandler(slog.NewTextHandler(os.Stdout, debugWithoutTime))
44+
45+
logrLogger.Info("hello world")
46+
logrLogger.Error(errors.New("fake error"), "ignore me")
47+
logrLogger.WithValues("x", 1, "y", 2).WithValues("str", "abc").WithName("foo").WithName("bar").V(4).Info("with values, verbosity and name")
48+
49+
// Output:
50+
// level=INFO msg="hello world"
51+
// level=ERROR msg="ignore me" err="fake error"
52+
// level=DEBUG msg="with values, verbosity and name" x=1 y=2 str=abc logger=foo/bar
53+
}
54+
55+
func ExampleToSlogHandler() {
56+
funcrLogger := funcr.New(func(prefix, args string) {
57+
if prefix != "" {
58+
fmt.Fprintln(os.Stdout, prefix, args)
59+
} else {
60+
fmt.Fprintln(os.Stdout, args)
61+
}
62+
}, funcr.Options{
63+
Verbosity: 10,
64+
})
65+
66+
slogLogger := slog.New(logr.ToSlogHandler(funcrLogger))
67+
slogLogger.Info("hello world")
68+
slogLogger.Error("ignore me", "err", errors.New("fake error"))
69+
slogLogger.With("x", 1, "y", 2).WithGroup("group").With("str", "abc").Warn("with values and group")
70+
71+
slogLogger = slog.New(logr.ToSlogHandler(funcrLogger.V(int(-slog.LevelDebug))))
72+
slogLogger.Info("info message reduced to debug level")
73+
74+
// Output:
75+
// "level"=0 "msg"="hello world"
76+
// "msg"="ignore me" "error"=null "err"="fake error"
77+
// "level"=0 "msg"="with values and group" "x"=1 "y"=2 "group"={"str"="abc"}
78+
// "level"=4 "msg"="info message reduced to debug level"
79+
}

funcr/example/main.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,21 @@ func helper2(log logr.Logger, msg string) {
4141
}
4242

4343
func main() {
44-
log := funcr.New(
45-
func(pfx, args string) { fmt.Println(pfx, args) },
44+
// logr
45+
log := funcr.NewJSON(
46+
func(arg string) { fmt.Println(arg) },
4647
funcr.Options{
4748
LogCaller: funcr.All,
4849
LogTimestamp: true,
4950
Verbosity: 1,
5051
})
51-
example(log.WithValues("module", "example"))
52+
logrExample(log.WithName("logr").WithValues("mode", "funcr"))
53+
54+
// slog (if possible)
55+
doSlog(log)
5256
}
5357

54-
func example(log logr.Logger) {
58+
func logrExample(log logr.Logger) {
5559
log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1})
5660
log.V(1).Info("you should see this")
5761
log.V(1).V(1).Info("you should NOT see this")

logr_noslog_test.go renamed to funcr/example/main_noslog.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ See the License for the specific language governing permissions and
1717
limitations under the License.
1818
*/
1919

20-
package logr
20+
// Package main is an example of using funcr.
21+
package main
2122

22-
//nolint:unused
23-
type testSlogSink struct{}
23+
import (
24+
"github.com/go-logr/logr"
25+
)
26+
27+
func doSlog(log logr.Logger) {
28+
log.Error(nil, "Sorry, slog is not supported on this version of Go")
29+
}

funcr/example/main_slog.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//go:build go1.21
2+
// +build go1.21
3+
4+
/*
5+
Copyright 2023 The logr 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 main is an example of using funcr.
21+
package main
22+
23+
import (
24+
"log/slog"
25+
26+
"github.com/go-logr/logr"
27+
)
28+
29+
func doSlog(log logr.Logger) {
30+
slogger := slog.New(logr.ToSlogHandler(log.WithName("slog").WithValues("mode", "slog")))
31+
slogExample(slogger)
32+
}
33+
34+
func slogExample(log *slog.Logger) {
35+
log.Warn("hello", "val1", 1, "val2", map[string]int{"k": 1})
36+
log.Info("you should see this")
37+
log.Debug("you should NOT see this")
38+
log.Error("uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14})
39+
log.With("attr1", 1, "attr2", 2).Info("with attrs")
40+
log.WithGroup("groupname").Info("with group", "slog2", false)
41+
log.WithGroup("group1").With("attr1", 1).WithGroup("group2").With("attr2", 2).Info("msg", "arg", "val")
42+
}

0 commit comments

Comments
 (0)