Pārlūkot izejas kodu

Update logging color and init

Move logging init to happen after cobra has processed the flags/args
and add a new flag to disable color output of the logs
Daniel Ramich 4 gadi atpakaļ
vecāks
revīzija
58b17ce927
2 mainītis faili ar 10 papildinājumiem un 3 dzēšanām
  1. 8 2
      pkg/cmd/commands.go
  2. 2 1
      pkg/log/log.go

+ 8 - 2
pkg/cmd/commands.go

@@ -68,9 +68,11 @@ func newRootCommand(costModelCmd *cobra.Command) *cobra.Command {
 	// Add our persistent flags, these are global and available anywhere
 	cmd.PersistentFlags().String("log-level", "info", "Set the log level")
 	cmd.PersistentFlags().String("log-format", "pretty", "Set the log format - Can be either 'JSON' or 'pretty'")
+	cmd.PersistentFlags().Bool("disable-log-color", false, "Disable coloring of log output")
 
 	viper.BindPFlag("log-level", cmd.PersistentFlags().Lookup("log-level"))
 	viper.BindPFlag("log-format", cmd.PersistentFlags().Lookup("log-format"))
+	viper.BindPFlag("disable-log-color", cmd.PersistentFlags().Lookup("disable-log-color"))
 
 	// Setup viper to read from the env, this allows reading flags from the command line or the env
 	// using the format 'LOG_LEVEL'
@@ -83,8 +85,6 @@ func newRootCommand(costModelCmd *cobra.Command) *cobra.Command {
 		newAgentCommand(),
 	)
 
-	log.InitLogging()
-
 	return cmd
 }
 
@@ -96,6 +96,9 @@ func newCostModelCommand() *cobra.Command {
 		Use:   CommandCostModel,
 		Short: "Cost-Model metric exporter and data aggregator.",
 		RunE: func(cmd *cobra.Command, args []string) error {
+			// Init logging here so cobra/viper has processed the command line args and flags
+			// otherwise only envvars are available during init
+			log.InitLogging()
 			return costmodel.Execute(opts)
 		},
 	}
@@ -114,6 +117,9 @@ func newAgentCommand() *cobra.Command {
 		Use:   CommandAgent,
 		Short: "Agent mode operates as a metric exporter only.",
 		RunE: func(cmd *cobra.Command, args []string) error {
+			// Init logging here so cobra/viper has processed the command line args and flags
+			// otherwise only envvars are available during init
+			log.InitLogging()
 			return agent.Execute(opts)
 		},
 	}

+ 2 - 1
pkg/log/log.go

@@ -21,7 +21,8 @@ func InitLogging() {
 	zerolog.TimeFieldFormat = time.RFC3339Nano
 	// Default to using pretty formatting
 	if strings.ToLower(viper.GetString("log-format")) != "json" {
-		log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339Nano})
+		disableColor := viper.GetBool("disable-log-color")
+		log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339Nano, NoColor: disableColor})
 	}
 
 	level, err := zerolog.ParseLevel(viper.GetString("log-level"))