Przeglądaj źródła

Add parameter to log init: hide log level message

This is useful for package users who want to keep logs to a minimum,
like CLI tools.
Michael Dresser 3 lat temu
rodzic
commit
aaa3895b13
2 zmienionych plików z 11 dodań i 4 usunięć
  1. 2 2
      pkg/cmd/commands.go
  2. 9 2
      pkg/log/log.go

+ 2 - 2
pkg/cmd/commands.go

@@ -98,7 +98,7 @@ func newCostModelCommand() *cobra.Command {
 		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()
+			log.InitLogging(true)
 			return costmodel.Execute(opts)
 		},
 	}
@@ -119,7 +119,7 @@ func newAgentCommand() *cobra.Command {
 		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()
+			log.InitLogging(true)
 			return agent.Execute(opts)
 		},
 	}

+ 9 - 2
pkg/log/log.go

@@ -23,7 +23,11 @@ const (
 	flagDisableColor = "disable-log-color"
 )
 
-func InitLogging() {
+// InitLogging sets standard configuration values for logging.
+//
+// If showLogLevelSetMessage is true, will log an unleveled message
+// specifying the log level post-init.
+func InitLogging(showLogLevelSetMessage bool) {
 	zerolog.TimeFieldFormat = time.RFC3339Nano
 	// Default to using pretty formatting
 	if strings.ToLower(viper.GetString(flagFormat)) != "json" {
@@ -38,7 +42,10 @@ func InitLogging() {
 		return
 	}
 	zerolog.SetGlobalLevel(level)
-	log.Log().Msgf("Log level set to %v", level)
+
+	if showLogLevelSetMessage {
+		log.Log().Msgf("Log level set to %v", level)
+	}
 
 }