Skip to content

Commit 3052202

Browse files
committed
Adds log sink that uses the standard logger
1 parent 7418300 commit 3052202

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tfsdklog/sink.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"io"
10+
"log"
1011
"os"
1112
"strings"
1213
"sync"
@@ -153,3 +154,30 @@ func isValidLogLevel(level string) bool {
153154

154155
return false
155156
}
157+
158+
// RegisterStdlogSink sets up a logging sink for use with test sweepers and
159+
// other cases where plugin logs don't get routed through Terraform and the
160+
// built-in Go `log` package is also used.
161+
//
162+
// RegisterStdlogSink should only ever be called by test sweepers, providers
163+
// should never call it.
164+
//
165+
// RegisterStdlogSink must be called prior to any loggers being setup or
166+
// instantiated.
167+
func RegisterStdlogSink(ctx context.Context) context.Context {
168+
logger, loggerOptions := newStdlogSink()
169+
170+
ctx = logging.SetSink(ctx, logger)
171+
ctx = logging.SetSinkOptions(ctx, loggerOptions)
172+
173+
return ctx
174+
}
175+
176+
func newStdlogSink() (hclog.Logger, *hclog.LoggerOptions) {
177+
loggerOptions := &hclog.LoggerOptions{
178+
IndependentLevels: true,
179+
JSONFormat: false,
180+
}
181+
182+
return hclog.FromStandardLogger(log.Default(), loggerOptions), loggerOptions
183+
}

0 commit comments

Comments
 (0)