Skip to content
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
5 changes: 5 additions & 0 deletions .changes/unreleased/NOTES-20251008-161009.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: NOTES
body: tfsdklog.RegisterTestSink is deprecated in favor or tfsdklog.ContextWithTestLogging
time: 2025-10-08T16:10:09.714154-04:00
custom:
Issue: "264"
29 changes: 26 additions & 3 deletions tfsdklog/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,39 @@ var invalidLogLevelMessage sync.Once
//
// RegisterTestSink must be called prior to any loggers being setup or
// instantiated.
//
// Deprecated: RegisterTestSink will be removed in a future release in order to
// drop the dependency on github.com/mitchellh/go-testing-interface, which is
// no longer maintained. Use ContextWithTestLogging instead of
// RegisterTestSink.
func RegisterTestSink(ctx context.Context, t testing.T) context.Context {
logger, loggerOptions := newSink(t)
logger, loggerOptions := newSink(t.Name())

ctx = logging.SetSink(ctx, logger)
ctx = logging.SetSinkOptions(ctx, loggerOptions)

return ctx
}

// ContextWithTestLogging sets up a logging sink, for use with test frameworks
// and other cases where plugin logs don't get routed through Terraform. This
// applies the same filtering and file output behaviors that Terraform does.
//
// ContextWithTestLogging should only ever be called by test frameworks,
// providers should never call it.
//
// ContextWithTestLogging must be called prior to any loggers being setup or
// instantiated.
func ContextWithTestLogging(ctx context.Context, testName string) context.Context {
Comment thread
austinvalle marked this conversation as resolved.
logger, loggerOptions := newSink(testName)

ctx = logging.SetSink(ctx, logger)
ctx = logging.SetSinkOptions(ctx, loggerOptions)

return ctx
}

func newSink(t testing.T) (hclog.Logger, *hclog.LoggerOptions) {
func newSink(testName string) (hclog.Logger, *hclog.LoggerOptions) {
logOutput := io.Writer(os.Stderr)
var json bool
var logLevel hclog.Level
Expand All @@ -102,7 +125,7 @@ func newSink(t testing.T) (hclog.Logger, *hclog.LoggerOptions) {
// if TF_LOG_PATH_MASK is set, use a test-name specific logging file,
// instead
if logPathMask := os.Getenv(envLogPathMask); logPathMask != "" {
testName := strings.Replace(t.Name(), "/", "__", -1)
testName := strings.Replace(testName, "/", "__", -1)
logFile = fmt.Sprintf(logPathMask, testName)
}

Expand Down