Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds logging sink that uses the standard logger #162

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 30 additions & 2 deletions tfsdklog/sink.go
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"io"
"log"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -70,15 +71,15 @@ var invalidLogLevelMessage sync.Once
// RegisterTestSink must be called prior to any loggers being setup or
// instantiated.
func RegisterTestSink(ctx context.Context, t testing.T) context.Context {
logger, loggerOptions := newSink(t)
logger, loggerOptions := newTestSink(t)

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

return ctx
}

func newSink(t testing.T) (hclog.Logger, *hclog.LoggerOptions) {
func newTestSink(t testing.T) (hclog.Logger, *hclog.LoggerOptions) {
logOutput := io.Writer(os.Stderr)
var json bool
var logLevel hclog.Level
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
}