Просмотр исходного кода

Merge pull request #1203 from kubecost/ramich-podlogs

Remove ANSI colors from podLogs endpoint and update logging init
Dan Ramich 4 лет назад
Родитель
Сommit
5937b2736b
4 измененных файлов с 22 добавлено и 3 удалено
  1. 1 0
      .gitignore
  2. 8 2
      pkg/cmd/commands.go
  3. 11 0
      pkg/costmodel/router.go
  4. 2 1
      pkg/log/log.go

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@
 ui/.cache
 ui/dist
 ui/node_modules/
+cmd/costmodel/costmodel

+ 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)
 		},
 	}

+ 11 - 0
pkg/costmodel/router.go

@@ -7,6 +7,7 @@ import (
 	"io/ioutil"
 	"net/http"
 	"reflect"
+	"regexp"
 	"strconv"
 	"strings"
 	"sync"
@@ -19,6 +20,7 @@ import (
 	"github.com/kubecost/cost-model/pkg/util/timeutil"
 	"github.com/kubecost/cost-model/pkg/util/watcher"
 	"github.com/microcosm-cc/bluemonday"
+	"github.com/spf13/viper"
 
 	v1 "k8s.io/api/core/v1"
 
@@ -65,6 +67,9 @@ const (
 var (
 	// gitCommit is set by the build system
 	gitCommit string
+
+	// ANSIRegex matches ANSI escape and colors https://en.wikipedia.org/wiki/ANSI_escape_code
+	ANSIRegex = regexp.MustCompile("\x1b\\[[0-9;]*m")
 )
 
 // Accesses defines a singleton application instance, providing access to
@@ -1181,6 +1186,12 @@ func logsFor(c kubernetes.Interface, namespace string, pod string, container str
 		return "", err
 	}
 
+	// If color is already disabled then we don't need to process the logs
+	// to drop ANSI colors
+	if !viper.GetBool("disable-log-color") {
+		podLogs = ANSIRegex.ReplaceAll(podLogs, []byte{})
+	}
+
 	return string(podLogs), nil
 }
 

+ 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, NoColor: true})
+		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"))