Skip to content

Commit

Permalink
fix: Support multiple --logger flags (sourcenetwork#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
orpheuslummis authored Aug 2, 2022
1 parent ad9ab70 commit 0bffb47
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
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)
}
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

0 comments on commit 0bffb47

Please sign in to comment.