Add `--debug` as shortcut for `--log-level debug` (#964)

## Changes
This PR exposes simpler interfaces to end users.

## Tests
<img width="724" alt="image"
src="https://github.com/databricks/cli/assets/259697/8bd25110-33f0-4197-8f00-2b8198c4aba6">
This commit is contained in:
Serge Smertin 2023-11-09 11:40:47 +01:00 committed by GitHub
parent d4d4b7480f
commit d4c0027556
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 3 deletions

View File

@ -73,6 +73,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) {
@ -95,6 +96,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
@ -137,9 +142,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)