Skip to content

Commit b5707f5

Browse files
committed
refactor(upload): add logging formats
1 parent fdccf34 commit b5707f5

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

tools/upload/cmd/root.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ func initializeConfig(cmd *cobra.Command) error {
5252

5353
// Set global log level
5454
zerolog.SetGlobalLevel(zerolog.Level(finalConfig.LogLevel))
55-
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
55+
// Set log format, defaults to json if format isn't "text"
56+
if finalConfig.LogFormat == "text" {
57+
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true})
58+
}
5659

5760
return nil
5861
}
@@ -87,7 +90,7 @@ func executeRootCommand(cmd *cobra.Command, args []string) error {
8790
log.Debug().Str("InputPath", conf.InputPath).Msg("Walking input path")
8891
err = filepath.WalkDir(conf.InputPath, walker)
8992
if err != nil {
90-
fmt.Errorf("Error walking input path: %w", err)
93+
return fmt.Errorf("walking input path: %w", err)
9194
}
9295

9396
// Putting the objects in order so all Dirs come before all Files

tools/upload/config/global.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import (
1010
// Define all global flag names
1111
const (
1212
LogLevelFlag = "log-level"
13+
LogFormatFlag = "log-format"
1314
InputPathFlag = "input"
1415
OutputPathFlag = "output"
1516
)
1617

1718
type GlobalConfig struct {
1819
LogLevel int `yaml:"log-level"`
20+
LogFormat string `yaml:"log-format"`
1921
InputPath string `yaml:"input-path"`
2022
OutputPath string `yaml:"output-path"`
2123
}
@@ -27,13 +29,15 @@ func ParseGlobalConfig(cmd *cobra.Command) (*GlobalConfig, error) {
2729
}
2830
return &GlobalConfig{
2931
LogLevel: number,
32+
LogFormat: cmd.Flag(LogFormatFlag).Value.String(),
3033
InputPath: filepath.Clean(cmd.Flag(InputPathFlag).Value.String()),
3134
OutputPath: filepath.Clean(cmd.Flag(OutputPathFlag).Value.String()),
3235
}, nil
3336
}
3437

3538
func AddGlobalFlags(cmd *cobra.Command) *cobra.Command {
3639
cmd.PersistentFlags().IntP(LogLevelFlag, "l", 1, "Logging level (-1= trace, 0=debug, 1=info, 2=warning, 3=error, ...")
40+
cmd.PersistentFlags().StringP(LogFormatFlag, "", "text", "Logging format (json, text)")
3741
cmd.PersistentFlags().StringP(InputPathFlag, "i", "/inputs", "Input path")
3842
cmd.PersistentFlags().StringP(OutputPathFlag, "o", "/outputs", "Output path")
3943
return cmd

0 commit comments

Comments
 (0)