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

tfsdklog: Consolidated multiple invalid log level messages and added missing newline #35

Merged
merged 2 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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