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

fix: Support multiple --logger flags #704

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,18 @@ func parseAndConfigLog(ctx context.Context, cfg *config.LoggingConfig, cmd *cobr
return err
}

// handle --logger <name>,<field>=<value>,...
loggerKVs, err := cmd.Flags().GetString("logger")
// handle --logger <name>,<field>=<value>,... --logger <name2>,<field>=<value>,..
loggerKVs, err := cmd.Flags().GetStringArray("logger")
if err != nil {
return fmt.Errorf("can't get logger flag: %w", err)
}

if loggerKVs != "" {
if err := parseAndConfigLogAllParams(ctx, cfg, loggerKVs); err != nil {
for _, kvs := range loggerKVs {
if err := parseAndConfigLogAllParams(ctx, cfg, kvs); err != nil {
return err
}
}

loggingConfig, err := cfg.ToLoggerConfig()
if err != nil {
return fmt.Errorf("could not get logging config: %w", err)
Expand All @@ -119,13 +120,12 @@ func parseAndConfigLog(ctx context.Context, cfg *config.LoggingConfig, cmd *cobr

func parseAndConfigLogAllParams(ctx context.Context, cfg *config.LoggingConfig, kvs string) error {
if kvs == "" {
return nil //nothing todo
return nil
}

// check if a CSV is provided
parsed := strings.Split(kvs, ",")
if len(parsed) <= 1 {
log.Fatal(ctx, "invalid --logger format, must be a csv")
return fmt.Errorf("logger was not provided as comma-separated pairs of <name>=<value>: %s", kvs)
orpheuslummis marked this conversation as resolved.
Show resolved Hide resolved
}
name := parsed[0]

Expand Down
6 changes: 3 additions & 3 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func init() {
log.FatalE(context.Background(), "Could not bind logging.loglevel", err)
}

rootCmd.PersistentFlags().String(
"logger", "",
"Named logger parameter override. usage: --logger <name>,level=<level>,output=<output>,...",
rootCmd.PersistentFlags().StringArray(
"logger", []string{},
"Override logger parameters. Usage: --logger <name>,level=<level>,output=<output>,...",
)

rootCmd.PersistentFlags().String(
Expand Down