Skip to content

Commit

Permalink
also trim trailing newlines
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hall <[email protected]>
  • Loading branch information
imjasonh committed Feb 12, 2024
1 parent adb77fc commit 66902bc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 3 additions & 4 deletions slogtest/slogtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"io"
"log/slog"
"strings"

"github.com/chainguard-dev/clog"
)
Expand All @@ -17,20 +18,18 @@ type logAdapter struct {
}

func (l *logAdapter) Write(b []byte) (int, error) {
l.l.Helper()
l.l.Log(string(b))
l.l.Log(strings.TrimSuffix(string(b), "\n"))
return len(b), nil
}

type Logger interface {
Helper()
Log(args ...any)
Logf(format string, args ...any)
}

// TestLogger gets a logger to use in unit and end to end tests
func TestLogger(t Logger) *clog.Logger {
return clog.New(slog.NewTextHandler(&logAdapter{l: t}, &slog.HandlerOptions{}))
return clog.New(slog.NewTextHandler(&logAdapter{l: t}, nil))
}

// TestContextWithLogger returns a context with a logger to be used in tests
Expand Down
2 changes: 2 additions & 0 deletions slogtest/slogtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ func TestSlogTest(t *testing.T) {
ctx := TestContextWithLogger(t)

clog.FromContext(ctx).With("foo", "bar").Infof("hello world")
clog.FromContext(ctx).With("bar", "baz").Infof("me again")
clog.FromContext(ctx).With("baz", true).Infof("okay last one")
}

0 comments on commit 66902bc

Please sign in to comment.