Skip to content

Commit

Permalink
chore: Convert to log/slog (#519)
Browse files Browse the repository at this point in the history
The small amount of verbose logging in the exec command has been
converted to debug logging under log/slog.
  • Loading branch information
bhavanki authored Nov 18, 2024
1 parent 4a778ea commit a9981cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 5 additions & 8 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"log/slog"
"os"
"strings"

Expand Down Expand Up @@ -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...)
Expand All @@ -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)
}
11 changes: 10 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a9981cc

Please sign in to comment.