77 "context"
88 "fmt"
99 "io"
10+ "log"
1011 "os"
1112 "strings"
1213 "sync"
@@ -75,7 +76,7 @@ var invalidLogLevelMessage sync.Once
7576// no longer maintained. Use ContextWithTestLogging instead of
7677// RegisterTestSink.
7778func RegisterTestSink (ctx context.Context , t testing.T ) context.Context {
78- logger , loggerOptions := newSink (t .Name ())
79+ logger , loggerOptions := newTestSink (t .Name ())
7980
8081 ctx = logging .SetSink (ctx , logger )
8182 ctx = logging .SetSinkOptions (ctx , loggerOptions )
@@ -93,15 +94,15 @@ func RegisterTestSink(ctx context.Context, t testing.T) context.Context {
9394// ContextWithTestLogging must be called prior to any loggers being setup or
9495// instantiated.
9596func ContextWithTestLogging (ctx context.Context , testName string ) context.Context {
96- logger , loggerOptions := newSink (testName )
97+ logger , loggerOptions := newTestSink (testName )
9798
9899 ctx = logging .SetSink (ctx , logger )
99100 ctx = logging .SetSinkOptions (ctx , loggerOptions )
100101
101102 return ctx
102103}
103104
104- func newSink (testName string ) (hclog.Logger , * hclog.LoggerOptions ) {
105+ func newTestSink (testName string ) (hclog.Logger , * hclog.LoggerOptions ) {
105106 logOutput := io .Writer (os .Stderr )
106107 var json bool
107108 var logLevel hclog.Level
@@ -176,3 +177,30 @@ func isValidLogLevel(level string) bool {
176177
177178 return false
178179}
180+
181+ // RegisterStdlogSink sets up a logging sink for use with test sweepers and
182+ // other cases where plugin logs don't get routed through Terraform and the
183+ // built-in Go `log` package is also used.
184+ //
185+ // RegisterStdlogSink should only ever be called by test sweepers, providers
186+ // should never call it.
187+ //
188+ // RegisterStdlogSink must be called prior to any loggers being setup or
189+ // instantiated.
190+ func RegisterStdlogSink (ctx context.Context ) context.Context {
191+ logger , loggerOptions := newStdlogSink ()
192+
193+ ctx = logging .SetSink (ctx , logger )
194+ ctx = logging .SetSinkOptions (ctx , loggerOptions )
195+
196+ return ctx
197+ }
198+
199+ func newStdlogSink () (hclog.Logger , * hclog.LoggerOptions ) {
200+ loggerOptions := & hclog.LoggerOptions {
201+ IndependentLevels : true ,
202+ JSONFormat : false ,
203+ }
204+
205+ return hclog .FromStandardLogger (log .Default (), loggerOptions ), loggerOptions
206+ }
0 commit comments