Skip to content

Commit

Permalink
Expose TANZU_CLI_LOG_LEVEL environment variable to control the log level
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Jul 31, 2023
1 parent 9248596 commit 9f52db6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
24 changes: 21 additions & 3 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
)

const (
EnvTanzuCLILogLevel = "TANZU_CLI_LOG_LEVEL"
)

var defaultLogThreshold int32 = 3

// logEntry defines the information that can be used for composing a log line.
Expand All @@ -28,14 +33,27 @@ type logEntry struct {
Values []interface{}
}

// NewLogger returns a new instance of the clusterctl.
// NewLogger returns a new instance of the logger.
func NewLogger() LoggerImpl {
logThreshold := getLogThreshold()
return &logger{
threshold: &defaultLogThreshold,
threshold: &logThreshold,
}
}

func getLogThreshold() int32 {
reqLogLevelStr := os.Getenv(EnvTanzuCLILogLevel)
if reqLogLevelStr != "" {
requestedLogLevel, err := strconv.ParseUint(reqLogLevelStr, 10, 32)
if err == nil {
return int32(requestedLogLevel)
}
fmt.Fprintf(os.Stderr, "invalid value %q for %s\n", reqLogLevelStr, EnvTanzuCLILogLevel)
}
return defaultLogThreshold
}

// logger defines a clusterctl friendly logr.Logger
// logger defines a logr.Logger
type logger struct {
threshold *int32
level int32
Expand Down
77 changes: 77 additions & 0 deletions log/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package log

import (
"bytes"
"os"
"testing"

"github.com/tj/assert"
)

func TestLogger(t *testing.T) {
assert := assert.New(t)

tests := []struct {
test string
logLevel string
containStrings []string
doesNotContainStrings []string
}{
{
test: "when TANZU_CLI_LOG_LEVEL is not configured",
logLevel: "",
containStrings: []string{"log-default", "log-0", "log-1", "log-3"},
doesNotContainStrings: []string{"log-5", "log-8"},
},
{
test: "when TANZU_CLI_LOG_LEVEL is set to 3",
logLevel: "",
containStrings: []string{"log-default", "log-0", "log-1", "log-3"},
doesNotContainStrings: []string{"log-5", "log-8"},
},
{
test: "when TANZU_CLI_LOG_LEVEL is set to 6",
logLevel: "6",
containStrings: []string{"log-default", "log-0", "log-1", "log-3", "log-5"},
doesNotContainStrings: []string{"log-8"},
},
{
test: "when TANZU_CLI_LOG_LEVEL is set to 9",
logLevel: "9",
containStrings: []string{"log-default", "log-0", "log-1", "log-3", "log-5", "log-8"},
doesNotContainStrings: []string{},
},
{
test: "when TANZU_CLI_LOG_LEVEL is configured with incorrect value",
logLevel: "a",
containStrings: []string{"log-default", "log-0", "log-1", "log-3"},
doesNotContainStrings: []string{"log-5", "log-8"},
},
}

for _, spec := range tests {
defer os.Setenv(EnvTanzuCLILogLevel, "")
os.Setenv(EnvTanzuCLILogLevel, spec.logLevel)

l = NewLogger()
stderr := &bytes.Buffer{}
SetStderr(stderr)

Info("log-default")
V(0).Info("log-0")
V(1).Info("log-1")
V(3).Info("log-3")
V(5).Info("log-5")
V(8).Info("log-8")

for _, logStr := range spec.containStrings {
assert.Contains(stderr.String(), logStr, spec.test)
}
for _, logStr := range spec.doesNotContainStrings {
assert.NotContains(stderr.String(), logStr, spec.test)
}
}
}

0 comments on commit 9f52db6

Please sign in to comment.