diff --git a/cmd/root.go b/cmd/root.go index 7d56e1a115a..4cbeccc6710 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -655,20 +655,6 @@ func watchChanges(ctx context.Context, watchDirs map[string]bool, watchedFiles m } } -// updateLogLevel checks if Toolbox have to update the existing log level set by users. -// stdio doesn't support "debug" and "info" logs. -func updateLogLevel(stdio bool, logLevel string) bool { - if stdio { - switch strings.ToUpper(logLevel) { - case log.Debug, log.Info: - return true - default: - return false - } - } - return false -} - func resolveWatcherInputs(toolsFile string, toolsFiles []string, toolsFolder string) (map[string]bool, map[string]bool) { var relevantFiles []string @@ -697,10 +683,6 @@ func resolveWatcherInputs(toolsFile string, toolsFiles []string, toolsFolder str } func run(cmd *Command) error { - if updateLogLevel(cmd.cfg.Stdio, cmd.cfg.LogLevel.String()) { - cmd.cfg.LogLevel = server.StringLevel(log.Warn) - } - ctx, cancel := context.WithCancel(cmd.Context()) defer cancel() @@ -724,16 +706,22 @@ func run(cmd *Command) error { cancel() }(ctx) + // If stdio, set logger's out stream (usually DEBUG and INFO logs) to errStream + loggerOut := cmd.outStream + if cmd.cfg.Stdio { + loggerOut = cmd.errStream + } + // Handle logger separately from config switch strings.ToLower(cmd.cfg.LoggingFormat.String()) { case "json": - logger, err := log.NewStructuredLogger(cmd.outStream, cmd.errStream, cmd.cfg.LogLevel.String()) + logger, err := log.NewStructuredLogger(loggerOut, cmd.errStream, cmd.cfg.LogLevel.String()) if err != nil { return fmt.Errorf("unable to initialize logger: %w", err) } cmd.logger = logger case "standard": - logger, err := log.NewStdLogger(cmd.outStream, cmd.errStream, cmd.cfg.LogLevel.String()) + logger, err := log.NewStdLogger(loggerOut, cmd.errStream, cmd.cfg.LogLevel.String()) if err != nil { return fmt.Errorf("unable to initialize logger: %w", err) } diff --git a/cmd/root_test.go b/cmd/root_test.go index 0ea5cf547a1..03a5b2f19e6 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1597,51 +1597,3 @@ func TestPrebuiltTools(t *testing.T) { }) } } - -func TestUpdateLogLevel(t *testing.T) { - tcs := []struct { - desc string - stdio bool - logLevel string - want bool - }{ - { - desc: "no stdio", - stdio: false, - logLevel: "info", - want: false, - }, - { - desc: "stdio with info log", - stdio: true, - logLevel: "info", - want: true, - }, - { - desc: "stdio with debug log", - stdio: true, - logLevel: "debug", - want: true, - }, - { - desc: "stdio with warn log", - stdio: true, - logLevel: "warn", - want: false, - }, - { - desc: "stdio with error log", - stdio: true, - logLevel: "error", - want: false, - }, - } - for _, tc := range tcs { - t.Run(tc.desc, func(t *testing.T) { - got := updateLogLevel(tc.stdio, tc.logLevel) - if got != tc.want { - t.Fatalf("incorrect indication to update log level: got %t, want %t", got, tc.want) - } - }) - } -}