From b228ba8636d02f868be6802747693a8489b182e2 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Tue, 5 Dec 2023 10:22:26 -0800 Subject: [PATCH] Break examples to new file --- example_slogr_test.go | 79 +++++++++++++++++++++++++++++++++++++++++++ slogr_test.go | 67 +++++++----------------------------- 2 files changed, 91 insertions(+), 55 deletions(-) create mode 100644 example_slogr_test.go diff --git a/example_slogr_test.go b/example_slogr_test.go new file mode 100644 index 0000000..6f568f9 --- /dev/null +++ b/example_slogr_test.go @@ -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" +} diff --git a/slogr_test.go b/slogr_test.go index 83634d5..332c7e9 100644 --- a/slogr_test.go +++ b/slogr_test.go @@ -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" ) @@ -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") } @@ -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 {