From edec2209a900999b332106277a6b370e4186651b Mon Sep 17 00:00:00 2001 From: Bill Havanki Date: Mon, 17 Jun 2024 12:20:07 -0400 Subject: [PATCH] chore: Convert to log/slog The small amount of verbose logging in the exec command has been converted to debug logging under log/slog. --- cmd/exec.go | 13 +++++-------- cmd/root.go | 11 ++++++++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/exec.go b/cmd/exec.go index 4d54bd9..55ac5fc 100644 --- a/cmd/exec.go +++ b/cmd/exec.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "log/slog" "os" "strings" @@ -97,15 +98,13 @@ func execRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("Failed to get secret store: %w", err) } - if pristine && verbose { - fmt.Fprintf(os.Stderr, "chamber: pristine mode engaged\n") + if pristine { + slog.Debug("chamber: pristine mode engaged") } var env environ.Environ if strict { - if verbose { - fmt.Fprintf(os.Stderr, "chamber: strict mode engaged\n") - } + slog.Debug("chamber: strict mode engaged") var err error env = environ.Environ(os.Environ()) err = env.LoadStrict(cmd.Context(), secretStore, strictValue, pristine, services...) @@ -130,9 +129,7 @@ func execRun(cmd *cobra.Command, args []string) error { } } - if verbose { - fmt.Fprintf(os.Stdout, "info: With environment %s\n", strings.Join(env, ",")) - } + slog.Debug(fmt.Sprintf("info: With environment %s\n", strings.Join(env, ","))) return exec(command, commandArgs, env) } diff --git a/cmd/root.go b/cmd/root.go index 3934552..37bdab0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "log/slog" "os" "regexp" "strconv" @@ -79,12 +80,13 @@ var RootCmd = &cobra.Command{ func init() { RootCmd.PersistentFlags().IntVarP(&numRetries, "retries", "r", DefaultNumRetries, "For SSM or Secrets Manager, the number of retries we'll make before giving up; AKA $CHAMBER_RETRIES") RootCmd.PersistentFlags().DurationVarP(&minThrottleDelay, "min-throttle-delay", "", 0, "DEPRECATED and no longer has any effect. Use retry-mode instead") + _ = RootCmd.PersistentFlags().MarkDeprecated("min-throttle-delay", "use --retry-mode instead") RootCmd.PersistentFlags().StringVarP(&retryMode, "retry-mode", "", store.DefaultRetryMode.String(), `For SSM, the model used to retry requests `+aws.RetryModeStandard.String()+` `+aws.RetryModeAdaptive.String(), ) - RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "Print more information to STDOUT") + RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "Print more information to STDERR") RootCmd.PersistentFlags().StringVarP(&backendFlag, "backend", "b", "ssm", `Backend to use; AKA $CHAMBER_SECRET_BACKEND null: no-op @@ -250,6 +252,13 @@ func prerun(cmd *cobra.Command, args []string) { Set("chamber-version", chamberVersion), }) } + + if verbose { + levelVar := &slog.LevelVar{} + levelVar.Set(slog.LevelDebug) + handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: levelVar}) + slog.SetDefault(slog.New(handler)) + } } func postrun(cmd *cobra.Command, args []string) {