Skip to content

Commit

Permalink
slogr test: fix and enhance names
Browse files Browse the repository at this point in the history
ExampleNewSlogLogger was wrong, the function is called NewSlogHandler.

Distinguishing what kind of logger a variable holds makes the examples a bit
easier to read. Normally one probably wouldn't do that.
  • Loading branch information
pohly committed Nov 29, 2023
1 parent 43857bc commit 2e4188a
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions slogr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ var debugWithoutTime = &slog.HandlerOptions{
}

func ExampleNew() {
logger := logr.NewLogr(slog.NewTextHandler(os.Stdout, debugWithoutTime))
logrLogger := logr.NewLogr(slog.NewTextHandler(os.Stdout, debugWithoutTime))

logger.Info("hello world")
logger.Error(errors.New("fake error"), "ignore me")
logger.WithValues("x", 1, "y", 2).WithValues("str", "abc").WithName("foo").WithName("bar").V(4).Info("with values, verbosity and name")
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 ExampleNewSlogLogger() {
func ExampleNewSlogHandler() {
funcrLogger := funcr.New(func(prefix, args string) {
if prefix != "" {
fmt.Fprintln(os.Stdout, prefix, args)
Expand All @@ -72,13 +72,13 @@ func ExampleNewSlogLogger() {
Verbosity: 10,
})

logger := slog.New(logr.NewSlogHandler(funcrLogger))
logger.Info("hello world")
logger.Error("ignore me", "err", errors.New("fake error"))
logger.With("x", 1, "y", 2).WithGroup("group").With("str", "abc").Warn("with values and group")
slogLogger := slog.New(logr.NewSlogHandler(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")

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

// Output:
// "level"=0 "msg"="hello world"
Expand Down

0 comments on commit 2e4188a

Please sign in to comment.