Skip to content

Commit

Permalink
tfsdklog: Consolidated multiple invalid log level messages and added …
Browse files Browse the repository at this point in the history
…missing newline (#35)

Reference: #32

This is related to SDK (or special implementations of testing framework) code which needs to execute `tfsdklog.RegisterTestSink()`, generally via the `tf5server.WithLoggingSink()`/`tf6server.WithLoggingSink()` functions.

Previously in terraform-provider-kubernetes:

```console
$ TF_LOG=trac go test -v -tags acceptance -run '^TestKubernetesManifest_ConfigMap$' ./manifest/test/acceptance
2022/03/04 16:51:21 Testing against Kubernetes API version: v1.22.3
=== RUN   TestKubernetesManifest_ConfigMap
[WARN] Invalid log level: "TRAC". Defaulting to level: OFF. Valid levels are: [TRACE DEBUG INFO WARN ERROR OFF][WARN] Invalid log level: "TRAC". Defaulting to level: OFF. Valid levels are: [TRACE DEBUG INFO WARN ERROR OFF][WARN] Invalid log level: "TRAC". Defaulting to level: OFF. Valid levels are: [TRACE DEBUG INFO WARN ERROR OFF][WARN] Invalid log level: "TRAC". Defaulting to level: OFF. Valid levels are: [TRACE DEBUG INFO WARN ERROR OFF][WARN] Invalid log level: "TRAC". Defaulting to level: OFF. Valid levels are: [TRACE DEBUG INFO WARN ERROR OFF][WARN] Invalid log level: "TRAC". Defaulting to level: OFF. Valid levels are: [TRACE DEBUG INFO WARN ERROR OFF]...
```

After updating dependency:

```console
$ go mod edit -replace=github.com/hashicorp/terraform-plugin-log=/Users/bflad/src/github.com/hashicorp/terraform-plugin-log
$ go mod tidy
$ go mod vendor
$ TF_LOG=trac go test -v -tags acceptance -run '^TestKubernetesManifest_ConfigMap$' ./manifest/test/acceptance
2022/03/04 16:52:49 Testing against Kubernetes API version: v1.22.3
=== RUN   TestKubernetesManifest_ConfigMap
[WARN] Invalid log level: "TRAC". Defaulting to level: OFF. Valid levels are: [TRACE DEBUG INFO WARN ERROR OFF]
... other test output ...
```
  • Loading branch information
bflad committed Mar 8, 2022
1 parent d50d729 commit cca4f05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/35.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
tfsdklog: Consolidated multiple invalid log level messages and added missing newline
```
14 changes: 12 additions & 2 deletions tfsdklog/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"strings"
"sync"
"syscall"

"github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -53,6 +54,9 @@ const (
// loggers.
var ValidLevels = []string{"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"}

// Only show invalid log level message once across any number of level lookups.
var invalidLogLevelMessage sync.Once

func getSink(ctx context.Context) hclog.Logger {
logger := ctx.Value(logging.SinkKey)
if logger == nil {
Expand Down Expand Up @@ -120,8 +124,14 @@ func newSink(t testing.T) hclog.Logger {
} else if isValidLogLevel(envLevel) {
logLevel = hclog.LevelFromString(envLevel)
} else {
fmt.Fprintf(os.Stderr, "[WARN] Invalid log level: %q. Defaulting to level: OFF. Valid levels are: %+v",
envLevel, ValidLevels)
invalidLogLevelMessage.Do(func() {
fmt.Fprintf(
os.Stderr,
"[WARN] Invalid log level: %q. Defaulting to level: OFF. Valid levels are: %+v\n",
envLevel,
ValidLevels,
)
})
}

return hclog.New(&hclog.LoggerOptions{
Expand Down

0 comments on commit cca4f05

Please sign in to comment.