Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve slogtest #13

Merged
merged 6 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions slogtest/slogtest.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
// Package slogtest provides utilities for emitting test logs using clog.
//
// func TestExample(t *testing.T) {
// ctx := slogtest.Context(t)
// clog.FromContext(ctx).With("foo", "bar").Info("hello world")
// }
//
// This produces the following test output:
//
// === RUN TestExample
// slogtest.go:24: level=INFO source=/path/to/example_test.go:13 msg="hello world" foo=bar
//
// This package is intended to be used in tests only.
package slogtest

import (
"context"
"io"
"log/slog"
"strings"
"testing"

"github.com/chainguard-dev/clog"
)

var (
_ io.Writer = &logAdapter{}
)
var _ io.Writer = &logAdapter{}

type logAdapter struct {
l Logger
}
type logAdapter struct{ l Logger }

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

type Logger interface {
Log(args ...any)
Logf(format string, args ...any)
}
var _ Logger = (*testing.T)(nil)
var _ Logger = (*testing.B)(nil)
var _ Logger = (*testing.F)(nil)

type Logger interface{ Log(args ...any) }

// TestLogger gets a logger to use in unit and end to end tests.
// This logger is configured to log at debug level.
func TestLogger(t Logger) *clog.Logger {
return TestLoggerWithOptions(t, &slog.HandlerOptions{
Level: slog.LevelDebug,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep this? IIRC this lets debug logs surface in test output (which follows the regular go test pattern where they only show if the test fails).

})
return clog.New(slog.NewTextHandler(&logAdapter{l: t}, &slog.HandlerOptions{
AddSource: true,
ReplaceAttr: RemoveTime,
}))
}

// TestLoggerWithOptions gets a logger to use in unit and end to end tests.
Expand All @@ -40,15 +53,14 @@ func TestLoggerWithOptions(t Logger, opts *slog.HandlerOptions) *clog.Logger {
}

// Context returns a context with a logger to be used in tests.
// This is equivalent to TestContextWithLogger.
func Context(t Logger) context.Context {
return TestContextWithLogger(t)
return clog.WithLogger(context.Background(), TestLogger(t))
}

// TestContextWithLogger returns a context with a logger to be used in tests
func TestContextWithLogger(t Logger) context.Context {
return clog.WithLogger(context.Background(), TestLogger(t))
}
//
// Deprecated: Use Context instead.
func TestContextWithLogger(t Logger) context.Context { return Context(t) }

// RemoveTime removes the top-level time attribute.
// It is intended to be used as a ReplaceAttr function,
Expand Down
16 changes: 16 additions & 0 deletions slogtest/slogtest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package slogtest_test

import (
"testing"

"github.com/chainguard-dev/clog"
"github.com/chainguard-dev/clog/slogtest"
)

func TestSlogTest(t *testing.T) {
ctx := slogtest.Context(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")
}
Loading