@@ -69,16 +69,39 @@ var invalidLogLevelMessage sync.Once
69
69
//
70
70
// RegisterTestSink must be called prior to any loggers being setup or
71
71
// 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.
72
77
func 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 )
74
97
75
98
ctx = logging .SetSink (ctx , logger )
76
99
ctx = logging .SetSinkOptions (ctx , loggerOptions )
77
100
78
101
return ctx
79
102
}
80
103
81
- func newSink (t testing. T ) (hclog.Logger , * hclog.LoggerOptions ) {
104
+ func newSink (testName string ) (hclog.Logger , * hclog.LoggerOptions ) {
82
105
logOutput := io .Writer (os .Stderr )
83
106
var json bool
84
107
var logLevel hclog.Level
@@ -102,7 +125,7 @@ func newSink(t testing.T) (hclog.Logger, *hclog.LoggerOptions) {
102
125
// if TF_LOG_PATH_MASK is set, use a test-name specific logging file,
103
126
// instead
104
127
if logPathMask := os .Getenv (envLogPathMask ); logPathMask != "" {
105
- testName := strings .Replace (t . Name () , "/" , "__" , - 1 )
128
+ testName := strings .Replace (testName , "/" , "__" , - 1 )
106
129
logFile = fmt .Sprintf (logPathMask , testName )
107
130
}
108
131
0 commit comments