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

slogtest: Allow passing in logging config options. #12

Merged
merged 1 commit into from
Jun 21, 2024
Merged
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
18 changes: 16 additions & 2 deletions slogtest/slogtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,23 @@ type Logger interface {
Logf(format string, args ...any)
}

// TestLogger gets a logger to use in unit and end to end tests
// 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 clog.New(slog.NewTextHandler(&logAdapter{l: t}, nil))
return TestLoggerWithOptions(t, &slog.HandlerOptions{
Level: slog.LevelDebug,
})
}

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

// 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)
}

// TestContextWithLogger returns a context with a logger to be used in tests
Expand Down
Loading