Skip to content

Commit

Permalink
Adds log sink that uses the standard logger
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Jun 21, 2023
1 parent f84dbd9 commit d10a80d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tfsdklog/sink.go
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"io"
"log"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -153,3 +154,30 @@ func isValidLogLevel(level string) bool {

return false
}

// RegisterStdlogSink sets up a logging sink for use with test sweepers and
// other cases where plugin logs don't get routed through Terraform and the
// built-in Go `log` package is also used.
//
// RegisterStdlogSink should only ever be called by test sweepers, providers
// should never call it.
//
// RegisterStdlogSink must be called prior to any loggers being setup or
// instantiated.
func RegisterStdlogSink(ctx context.Context) context.Context {
logger, loggerOptions := newStdlogSink()

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

return ctx
}

func newStdlogSink() (hclog.Logger, *hclog.LoggerOptions) {
loggerOptions := &hclog.LoggerOptions{
IndependentLevels: true,
JSONFormat: false,
}

return hclog.FromStandardLogger(log.Default(), loggerOptions), loggerOptions
}

0 comments on commit d10a80d

Please sign in to comment.