Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --debug as shortcut for --log-level debug #964

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions cmd/root/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type logFlags struct {
file flags.LogFileFlag
level flags.LogLevelFlag
output flags.Output
debug bool
}

func (f *logFlags) makeLogHandler(opts slog.HandlerOptions) (slog.Handler, error) {
Expand All @@ -94,6 +95,10 @@ func (f *logFlags) makeLogHandler(opts slog.HandlerOptions) (slog.Handler, error
}

func (f *logFlags) initializeContext(ctx context.Context) (context.Context, error) {
if f.debug {
f.level.Set("debug")
}

opts := slog.HandlerOptions{}
opts.Level = f.level.Level()
opts.AddSource = true
Expand Down Expand Up @@ -136,9 +141,17 @@ func initLogFlags(cmd *cobra.Command) *logFlags {
f.output.Set(v)
}

cmd.PersistentFlags().Var(&f.file, "log-file", "file to write logs to")
cmd.PersistentFlags().Var(&f.level, "log-level", "log level")
cmd.PersistentFlags().Var(&f.output, "log-format", "log output format (text or json)")
flags := cmd.PersistentFlags()
flags.BoolVar(&f.debug, "debug", false, "enable debug logging")
flags.Var(&f.file, "log-file", "file to write logs to")
flags.Var(&f.level, "log-level", "log level")
flags.Var(&f.output, "log-format", "log output format (text or json)")

// mark fine-grained flags hidden from global --help
flags.MarkHidden("log-file")
flags.MarkHidden("log-level")
flags.MarkHidden("log-format")

cmd.RegisterFlagCompletionFunc("log-file", f.file.Complete)
cmd.RegisterFlagCompletionFunc("log-level", f.level.Complete)
cmd.RegisterFlagCompletionFunc("log-format", f.output.Complete)
Expand Down