Skip to content

Commit a9981cc

Browse files
authored
chore: Convert to log/slog (#519)
The small amount of verbose logging in the exec command has been converted to debug logging under log/slog.
1 parent 4a778ea commit a9981cc

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

cmd/exec.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6+
"log/slog"
67
"os"
78
"strings"
89

@@ -97,15 +98,13 @@ func execRun(cmd *cobra.Command, args []string) error {
9798
return fmt.Errorf("Failed to get secret store: %w", err)
9899
}
99100

100-
if pristine && verbose {
101-
fmt.Fprintf(os.Stderr, "chamber: pristine mode engaged\n")
101+
if pristine {
102+
slog.Debug("chamber: pristine mode engaged")
102103
}
103104

104105
var env environ.Environ
105106
if strict {
106-
if verbose {
107-
fmt.Fprintf(os.Stderr, "chamber: strict mode engaged\n")
108-
}
107+
slog.Debug("chamber: strict mode engaged")
109108
var err error
110109
env = environ.Environ(os.Environ())
111110
err = env.LoadStrict(cmd.Context(), secretStore, strictValue, pristine, services...)
@@ -130,9 +129,7 @@ func execRun(cmd *cobra.Command, args []string) error {
130129
}
131130
}
132131

133-
if verbose {
134-
fmt.Fprintf(os.Stdout, "info: With environment %s\n", strings.Join(env, ","))
135-
}
132+
slog.Debug(fmt.Sprintf("info: With environment %s\n", strings.Join(env, ",")))
136133

137134
return exec(command, commandArgs, env)
138135
}

cmd/root.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"log/slog"
78
"os"
89
"regexp"
910
"strconv"
@@ -79,12 +80,13 @@ var RootCmd = &cobra.Command{
7980
func init() {
8081
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")
8182
RootCmd.PersistentFlags().DurationVarP(&minThrottleDelay, "min-throttle-delay", "", 0, "DEPRECATED and no longer has any effect. Use retry-mode instead")
83+
_ = RootCmd.PersistentFlags().MarkDeprecated("min-throttle-delay", "use --retry-mode instead")
8284
RootCmd.PersistentFlags().StringVarP(&retryMode, "retry-mode", "", store.DefaultRetryMode.String(),
8385
`For SSM, the model used to retry requests
8486
`+aws.RetryModeStandard.String()+`
8587
`+aws.RetryModeAdaptive.String(),
8688
)
87-
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "Print more information to STDOUT")
89+
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "Print more information to STDERR")
8890
RootCmd.PersistentFlags().StringVarP(&backendFlag, "backend", "b", "ssm",
8991
`Backend to use; AKA $CHAMBER_SECRET_BACKEND
9092
null: no-op
@@ -250,6 +252,13 @@ func prerun(cmd *cobra.Command, args []string) {
250252
Set("chamber-version", chamberVersion),
251253
})
252254
}
255+
256+
if verbose {
257+
levelVar := &slog.LevelVar{}
258+
levelVar.Set(slog.LevelDebug)
259+
handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: levelVar})
260+
slog.SetDefault(slog.New(handler))
261+
}
253262
}
254263

255264
func postrun(cmd *cobra.Command, args []string) {

0 commit comments

Comments
 (0)