Skip to content

Commit

Permalink
Revert "Implement sync threshold values set using env variable TANZU_…
Browse files Browse the repository at this point in the history
…CLI_LOG_…" (#158)

This reverts commit d0ea232.

(cherry picked from commit 055fd83)
  • Loading branch information
mpanchajanya committed Feb 7, 2024
1 parent 566f018 commit bd354a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 63 deletions.
25 changes: 4 additions & 21 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,22 @@ type logEntry struct {

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

func getLogThreshold(defaultThreshold int32) int32 {
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 defaultThreshold
return defaultLogThreshold
}

// logger defines a logr.Logger
Expand All @@ -67,22 +65,8 @@ type logger struct {

var _ LoggerImpl = &logger{}

// SyncThreshold syncs the threshold value for a logger based on what is set on logger and TANZU_CLI_LOG_LEVEL.
// TANZU_CLI_LOG_LEVEL value takes precedence over SetThreshold
func (l *logger) syncThreshold() {
var logThreshold int32

// If threshold is already set then use that value as default fallback if TANZU_CLI_LOG_LEVEL is not set
if l.threshold != nil {
logThreshold = getLogThreshold(*l.threshold)
} else {
logThreshold = getLogThreshold(defaultLogThreshold)
}
l.threshold = &logThreshold
}

// SetThreshold implements a New Option that allows to set the threshold level for a logger.
// The logger will write only log messages with a level/V(x) equal or lower to the threshold.
// The logger will write only log messages with a level/V(x) equal or higher to the threshold.
func (l *logger) SetThreshold(threshold *int32) {
l.threshold = threshold
}
Expand Down Expand Up @@ -209,7 +193,6 @@ func (l *logger) CloneWithLevel(level int) LoggerImpl {
}

func (l *logger) Print(msg string, err error, logType string, kvs ...interface{}) {
l.syncThreshold()
msg = fmt.Sprintf("%s%s", l.getLogTypeIndicator(logType), msg)
values := copySlice(l.values)
values = append(values, kvs...)
Expand Down
72 changes: 30 additions & 42 deletions log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,84 +12,72 @@ import (
)

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

tests := []struct {
title string
initialLogLevel string
test string
logLevel string
containStrings []string
doesNotContainStrings []string
}{

{
title: "when TANZU_CLI_LOG_LEVEL is not configured and then set to 3",
initialLogLevel: "",
logLevel: "3",
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"},
},
{
title: "when TANZU_CLI_LOG_LEVEL is set to 1 and 5",
initialLogLevel: "1",
logLevel: "5",
containStrings: []string{"log-default", "log-0", "log-1", "log-5"},
doesNotContainStrings: []string{"log-3", "log-8"},
test: "when TANZU_CLI_LOG_LEVEL is set to 3",
logLevel: "3",
containStrings: []string{"log-default", "log-0", "log-1", "log-3"},
doesNotContainStrings: []string{"log-5", "log-8"},
},
{
title: "when TANZU_CLI_LOG_LEVEL is set to 3 and 6",
initialLogLevel: "3",
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"},
},
{
title: "when TANZU_CLI_LOG_LEVEL is set to 3 and 8",
initialLogLevel: "3",
test: "when TANZU_CLI_LOG_LEVEL is set to 8",
logLevel: "8",
containStrings: []string{"log-default", "log-0", "log-1", "log-3", "log-5", "log-8"},
doesNotContainStrings: []string{},
},
{
title: "when TANZU_CLI_LOG_LEVEL is set to 5 and 9",
initialLogLevel: "5",
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{},
},
{
title: "when TANZU_CLI_LOG_LEVEL is configured with incorrect value",
initialLogLevel: "a",
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 {
t.Run(spec.title, func(t *testing.T) {
defer os.Setenv(EnvTanzuCLILogLevel, "")

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

os.Setenv(EnvTanzuCLILogLevel, spec.initialLogLevel)

Info("log-default")
V(0).Info("log-0")
V(1).Info("log-1")
V(3).Info("log-3")
defer os.Setenv(EnvTanzuCLILogLevel, "")
os.Setenv(EnvTanzuCLILogLevel, spec.logLevel)

os.Setenv(EnvTanzuCLILogLevel, spec.logLevel)
l = NewLogger()
stderr := &bytes.Buffer{}
SetStderr(stderr)

V(5).Info("log-5")
V(8).Info("log-8")
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(t, stderr.String(), logStr, spec.title)
}
for _, logStr := range spec.doesNotContainStrings {
assert.NotContains(t, stderr.String(), logStr, spec.title)
}
})
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 bd354a3

Please sign in to comment.