@@ -69,16 +69,39 @@ var invalidLogLevelMessage sync.Once
6969//
7070// RegisterTestSink must be called prior to any loggers being setup or
7171// instantiated.
72+ //
73+ // Deprecated: RegisterTestSink will be removed in a future release in order to
74+ // drop the dependency on github.com/mitchellh/go-testing-interface, which is
75+ // no longer maintained. Use ContextWithTestLogging instead of
76+ // RegisterTestSink.
7277func RegisterTestSink (ctx context.Context , t testing.T ) context.Context {
73- logger , loggerOptions := newSink (t )
78+ logger , loggerOptions := newSink (t .Name ())
79+
80+ ctx = logging .SetSink (ctx , logger )
81+ ctx = logging .SetSinkOptions (ctx , loggerOptions )
82+
83+ return ctx
84+ }
85+
86+ // ContextWithTestLogging sets up a logging sink, for use with test frameworks
87+ // and other cases where plugin logs don't get routed through Terraform. This
88+ // applies the same filtering and file output behaviors that Terraform does.
89+ //
90+ // ContextWithTestLogging should only ever be called by test frameworks,
91+ // providers should never call it.
92+ //
93+ // ContextWithTestLogging must be called prior to any loggers being setup or
94+ // instantiated.
95+ func ContextWithTestLogging (ctx context.Context , testName string ) context.Context {
96+ logger , loggerOptions := newSink (testName )
7497
7598 ctx = logging .SetSink (ctx , logger )
7699 ctx = logging .SetSinkOptions (ctx , loggerOptions )
77100
78101 return ctx
79102}
80103
81- func newSink (t testing. T ) (hclog.Logger , * hclog.LoggerOptions ) {
104+ func newSink (testName string ) (hclog.Logger , * hclog.LoggerOptions ) {
82105 logOutput := io .Writer (os .Stderr )
83106 var json bool
84107 var logLevel hclog.Level
@@ -102,7 +125,7 @@ func newSink(t testing.T) (hclog.Logger, *hclog.LoggerOptions) {
102125 // if TF_LOG_PATH_MASK is set, use a test-name specific logging file,
103126 // instead
104127 if logPathMask := os .Getenv (envLogPathMask ); logPathMask != "" {
105- testName := strings .Replace (t . Name () , "/" , "__" , - 1 )
128+ testName := strings .Replace (testName , "/" , "__" , - 1 )
106129 logFile = fmt .Sprintf (logPathMask , testName )
107130 }
108131
0 commit comments