Skip to content

Commit b228ba8

Browse files
committed
Break examples to new file
1 parent 6432877 commit b228ba8

File tree

2 files changed

+91
-55
lines changed

2 files changed

+91
-55
lines changed

example_slogr_test.go

Lines changed: 79 additions & 0 deletions
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+
}

slogr_test.go

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,18 @@ See the License for the specific language governing permissions and
1717
limitations under the License.
1818
*/
1919

20-
package logr_test
20+
package logr
2121

2222
import (
2323
"bytes"
24-
"errors"
2524
"fmt"
2625
"io"
2726
"log/slog"
28-
"os"
2927
"path"
3028
"runtime"
3129
"strings"
3230
"testing"
3331

34-
"github.com/go-logr/logr"
35-
"github.com/go-logr/logr/funcr"
3632
"github.com/go-logr/logr/internal/testhelp"
3733
)
3834

@@ -46,52 +42,13 @@ var debugWithoutTime = &slog.HandlerOptions{
4642
Level: slog.LevelDebug,
4743
}
4844

49-
func ExampleFromSlogHandler() {
50-
logrLogger := logr.FromSlogHandler(slog.NewTextHandler(os.Stdout, debugWithoutTime))
51-
52-
logrLogger.Info("hello world")
53-
logrLogger.Error(errors.New("fake error"), "ignore me")
54-
logrLogger.WithValues("x", 1, "y", 2).WithValues("str", "abc").WithName("foo").WithName("bar").V(4).Info("with values, verbosity and name")
55-
56-
// Output:
57-
// level=INFO msg="hello world"
58-
// level=ERROR msg="ignore me" err="fake error"
59-
// level=DEBUG msg="with values, verbosity and name" x=1 y=2 str=abc logger=foo/bar
60-
}
61-
62-
func ExampleToSlogHandler() {
63-
funcrLogger := funcr.New(func(prefix, args string) {
64-
if prefix != "" {
65-
fmt.Fprintln(os.Stdout, prefix, args)
66-
} else {
67-
fmt.Fprintln(os.Stdout, args)
68-
}
69-
}, funcr.Options{
70-
Verbosity: 10,
71-
})
72-
73-
slogLogger := slog.New(logr.ToSlogHandler(funcrLogger))
74-
slogLogger.Info("hello world")
75-
slogLogger.Error("ignore me", "err", errors.New("fake error"))
76-
slogLogger.With("x", 1, "y", 2).WithGroup("group").With("str", "abc").Warn("with values and group")
77-
78-
slogLogger = slog.New(logr.ToSlogHandler(funcrLogger.V(int(-slog.LevelDebug))))
79-
slogLogger.Info("info message reduced to debug level")
80-
81-
// Output:
82-
// "level"=0 "msg"="hello world"
83-
// "msg"="ignore me" "error"=null "err"="fake error"
84-
// "level"=0 "msg"="with values and group" "x"=1 "y"=2 "group"={"str"="abc"}
85-
// "level"=4 "msg"="info message reduced to debug level"
86-
}
87-
8845
func TestWithCallDepth(t *testing.T) {
8946
debugWithCaller := *debugWithoutTime
9047
debugWithCaller.AddSource = true
9148
var buffer bytes.Buffer
92-
logger := logr.FromSlogHandler(slog.NewTextHandler(&buffer, &debugWithCaller))
49+
logger := FromSlogHandler(slog.NewTextHandler(&buffer, &debugWithCaller))
9350

94-
logHelper := func(logger logr.Logger) {
51+
logHelper := func(logger Logger) {
9552
logger.WithCallDepth(1).Info("hello")
9653
}
9754

@@ -108,31 +65,31 @@ func TestRunSlogTestsOnSlogSink(t *testing.T) {
10865
// This proves that slogSink passes slog's own tests.
10966
testhelp.RunSlogTests(t, func(buffer *bytes.Buffer) slog.Handler {
11067
handler := slog.NewJSONHandler(buffer, nil)
111-
logger := logr.FromSlogHandler(handler)
112-
return logr.ToSlogHandler(logger)
68+
logger := FromSlogHandler(handler)
69+
return ToSlogHandler(logger)
11370
})
11471
}
11572

11673
func TestSlogSinkOnDiscard(_ *testing.T) {
11774
// Compile-test
118-
logger := slog.New(logr.ToSlogHandler(logr.Discard()))
75+
logger := slog.New(ToSlogHandler(Discard()))
11976
logger.WithGroup("foo").With("x", 1).Info("hello")
12077
}
12178

12279
func TestConversion(t *testing.T) {
123-
d := logr.Discard()
124-
d2 := logr.FromSlogHandler(logr.ToSlogHandler(d))
80+
d := Discard()
81+
d2 := FromSlogHandler(ToSlogHandler(d))
12582
expectEqual(t, d, d2)
12683

127-
e := logr.Logger{}
128-
e2 := logr.FromSlogHandler(logr.ToSlogHandler(e))
84+
e := Logger{}
85+
e2 := FromSlogHandler(ToSlogHandler(e))
12986
expectEqual(t, e, e2)
13087

13188
text := slog.NewTextHandler(io.Discard, nil)
132-
text2 := logr.ToSlogHandler(logr.FromSlogHandler(text))
89+
text2 := ToSlogHandler(FromSlogHandler(text))
13390
expectEqual(t, text, text2)
13491

135-
text3 := logr.ToSlogHandler(logr.FromSlogHandler(text).V(1))
92+
text3 := ToSlogHandler(FromSlogHandler(text).V(1))
13693
if handler, ok := text3.(interface {
13794
GetLevel() slog.Level
13895
}); ok {

0 commit comments

Comments
 (0)