Skip to content

Commit

Permalink
Break examples to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Dec 8, 2023
1 parent 6432877 commit b228ba8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 55 deletions.
79 changes: 79 additions & 0 deletions example_slogr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//go:build go1.21
// +build go1.21

/*
Copyright 2023 The logr Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package logr_test

import (
"errors"
"fmt"
"log/slog"
"os"

"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
)

var debugWithoutTime = &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == "time" {
return slog.Attr{}
}
return a
},
Level: slog.LevelDebug,
}

func ExampleFromSlogHandler() {
logrLogger := logr.FromSlogHandler(slog.NewTextHandler(os.Stdout, debugWithoutTime))

logrLogger.Info("hello world")
logrLogger.Error(errors.New("fake error"), "ignore me")
logrLogger.WithValues("x", 1, "y", 2).WithValues("str", "abc").WithName("foo").WithName("bar").V(4).Info("with values, verbosity and name")

// Output:
// level=INFO msg="hello world"
// level=ERROR msg="ignore me" err="fake error"
// level=DEBUG msg="with values, verbosity and name" x=1 y=2 str=abc logger=foo/bar
}

func ExampleToSlogHandler() {
funcrLogger := funcr.New(func(prefix, args string) {
if prefix != "" {
fmt.Fprintln(os.Stdout, prefix, args)
} else {
fmt.Fprintln(os.Stdout, args)
}
}, funcr.Options{
Verbosity: 10,
})

slogLogger := slog.New(logr.ToSlogHandler(funcrLogger))
slogLogger.Info("hello world")
slogLogger.Error("ignore me", "err", errors.New("fake error"))
slogLogger.With("x", 1, "y", 2).WithGroup("group").With("str", "abc").Warn("with values and group")

slogLogger = slog.New(logr.ToSlogHandler(funcrLogger.V(int(-slog.LevelDebug))))
slogLogger.Info("info message reduced to debug level")

// Output:
// "level"=0 "msg"="hello world"
// "msg"="ignore me" "error"=null "err"="fake error"
// "level"=0 "msg"="with values and group" "x"=1 "y"=2 "group"={"str"="abc"}
// "level"=4 "msg"="info message reduced to debug level"
}
67 changes: 12 additions & 55 deletions slogr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package logr_test
package logr

import (
"bytes"
"errors"
"fmt"
"io"
"log/slog"
"os"
"path"
"runtime"
"strings"
"testing"

"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
"github.com/go-logr/logr/internal/testhelp"
)

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

func ExampleFromSlogHandler() {
logrLogger := logr.FromSlogHandler(slog.NewTextHandler(os.Stdout, debugWithoutTime))

logrLogger.Info("hello world")
logrLogger.Error(errors.New("fake error"), "ignore me")
logrLogger.WithValues("x", 1, "y", 2).WithValues("str", "abc").WithName("foo").WithName("bar").V(4).Info("with values, verbosity and name")

// Output:
// level=INFO msg="hello world"
// level=ERROR msg="ignore me" err="fake error"
// level=DEBUG msg="with values, verbosity and name" x=1 y=2 str=abc logger=foo/bar
}

func ExampleToSlogHandler() {
funcrLogger := funcr.New(func(prefix, args string) {
if prefix != "" {
fmt.Fprintln(os.Stdout, prefix, args)
} else {
fmt.Fprintln(os.Stdout, args)
}
}, funcr.Options{
Verbosity: 10,
})

slogLogger := slog.New(logr.ToSlogHandler(funcrLogger))
slogLogger.Info("hello world")
slogLogger.Error("ignore me", "err", errors.New("fake error"))
slogLogger.With("x", 1, "y", 2).WithGroup("group").With("str", "abc").Warn("with values and group")

slogLogger = slog.New(logr.ToSlogHandler(funcrLogger.V(int(-slog.LevelDebug))))
slogLogger.Info("info message reduced to debug level")

// Output:
// "level"=0 "msg"="hello world"
// "msg"="ignore me" "error"=null "err"="fake error"
// "level"=0 "msg"="with values and group" "x"=1 "y"=2 "group"={"str"="abc"}
// "level"=4 "msg"="info message reduced to debug level"
}

func TestWithCallDepth(t *testing.T) {
debugWithCaller := *debugWithoutTime
debugWithCaller.AddSource = true
var buffer bytes.Buffer
logger := logr.FromSlogHandler(slog.NewTextHandler(&buffer, &debugWithCaller))
logger := FromSlogHandler(slog.NewTextHandler(&buffer, &debugWithCaller))

logHelper := func(logger logr.Logger) {
logHelper := func(logger Logger) {
logger.WithCallDepth(1).Info("hello")
}

Expand All @@ -108,31 +65,31 @@ func TestRunSlogTestsOnSlogSink(t *testing.T) {
// This proves that slogSink passes slog's own tests.
testhelp.RunSlogTests(t, func(buffer *bytes.Buffer) slog.Handler {
handler := slog.NewJSONHandler(buffer, nil)
logger := logr.FromSlogHandler(handler)
return logr.ToSlogHandler(logger)
logger := FromSlogHandler(handler)
return ToSlogHandler(logger)
})
}

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

func TestConversion(t *testing.T) {
d := logr.Discard()
d2 := logr.FromSlogHandler(logr.ToSlogHandler(d))
d := Discard()
d2 := FromSlogHandler(ToSlogHandler(d))
expectEqual(t, d, d2)

e := logr.Logger{}
e2 := logr.FromSlogHandler(logr.ToSlogHandler(e))
e := Logger{}
e2 := FromSlogHandler(ToSlogHandler(e))
expectEqual(t, e, e2)

text := slog.NewTextHandler(io.Discard, nil)
text2 := logr.ToSlogHandler(logr.FromSlogHandler(text))
text2 := ToSlogHandler(FromSlogHandler(text))
expectEqual(t, text, text2)

text3 := logr.ToSlogHandler(logr.FromSlogHandler(text).V(1))
text3 := ToSlogHandler(FromSlogHandler(text).V(1))
if handler, ok := text3.(interface {
GetLevel() slog.Level
}); ok {
Expand Down

0 comments on commit b228ba8

Please sign in to comment.