From 9dd050b8929fbca26e5e2ace065e5b2ba154f9d0 Mon Sep 17 00:00:00 2001 From: Or Sela Date: Thu, 6 Feb 2020 13:15:16 +0200 Subject: [PATCH] set log format using a flag --- cmd/executor/cmd/root.go | 10 ++--- cmd/warmer/cmd/root.go | 10 ++--- pkg/constants/constants.go | 3 -- pkg/logging/logging.go | 76 ++++++++++++++++++++++++++++++++++++++ pkg/util/util.go | 15 -------- 5 files changed, 85 insertions(+), 29 deletions(-) create mode 100644 pkg/logging/logging.go diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index f39e011ede..c5cbd57fe8 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -28,6 +28,7 @@ import ( "github.com/GoogleContainerTools/kaniko/pkg/config" "github.com/GoogleContainerTools/kaniko/pkg/constants" "github.com/GoogleContainerTools/kaniko/pkg/executor" + "github.com/GoogleContainerTools/kaniko/pkg/logging" "github.com/GoogleContainerTools/kaniko/pkg/timing" "github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/genuinetools/amicontained/container" @@ -38,13 +39,12 @@ import ( ) var ( - opts = &config.KanikoOptions{} - logLevel string - force bool + opts = &config.KanikoOptions{} + force bool ) func init() { - RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", constants.DefaultLogLevel, "Log level (debug, info, warn, error, fatal, panic") + logging.AddFlags(RootCmd.PersistentFlags()) RootCmd.PersistentFlags().BoolVarP(&force, "force", "", false, "Force building outside of a container") addKanikoOptionsFlags() addHiddenFlags(RootCmd) @@ -56,7 +56,7 @@ var RootCmd = &cobra.Command{ PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if cmd.Use == "executor" { resolveEnvironmentBuildArgs(opts.BuildArgs, os.Getenv) - if err := util.ConfigureLogging(logLevel); err != nil { + if err := logging.Configure(); err != nil { return err } if !opts.NoPush && len(opts.Destinations) == 0 { diff --git a/cmd/warmer/cmd/root.go b/cmd/warmer/cmd/root.go index b0e3c9076e..8ced40ac0f 100644 --- a/cmd/warmer/cmd/root.go +++ b/cmd/warmer/cmd/root.go @@ -23,19 +23,17 @@ import ( "github.com/GoogleContainerTools/kaniko/pkg/cache" "github.com/GoogleContainerTools/kaniko/pkg/config" - "github.com/GoogleContainerTools/kaniko/pkg/constants" - "github.com/GoogleContainerTools/kaniko/pkg/util" + "github.com/GoogleContainerTools/kaniko/pkg/logging" "github.com/pkg/errors" "github.com/spf13/cobra" ) var ( - opts = &config.WarmerOptions{} - logLevel string + opts = &config.WarmerOptions{} ) func init() { - RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", constants.DefaultLogLevel, "Log level (debug, info, warn, error, fatal, panic") + logging.AddFlags(RootCmd.PersistentFlags()) addKanikoOptionsFlags() addHiddenFlags() } @@ -43,7 +41,7 @@ func init() { var RootCmd = &cobra.Command{ Use: "cache warmer", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := util.ConfigureLogging(logLevel); err != nil { + if err := logging.Configure(); err != nil { return err } if len(opts.Images) == 0 { diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 9de493eb21..8dd5893ea7 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -17,9 +17,6 @@ limitations under the License. package constants const ( - // DefaultLogLevel is the default log level - DefaultLogLevel = "info" - // RootDir is the path to the root directory RootDir = "/" diff --git a/pkg/logging/logging.go b/pkg/logging/logging.go new file mode 100644 index 0000000000..5ed79df986 --- /dev/null +++ b/pkg/logging/logging.go @@ -0,0 +1,76 @@ +/* +Copyright 2020 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package logging + +import ( + "fmt" + + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "github.com/spf13/pflag" +) + +const ( + // Default log level + DefaultLevel = "info" + + // Text format + FormatText = "text" + // Colored text format + FormatColor = "color" + // JSON format + FormatJSON = "json" +) + +var ( + logLevel string + logFormat string +) + +// AddFlags injects logging-related flags into the given FlagSet +func AddFlags(fs *pflag.FlagSet) { + fs.StringVarP(&logLevel, "verbosity", "v", DefaultLevel, "Log level (debug, info, warn, error, fatal, panic") + fs.StringVar(&logFormat, "log-format", FormatColor, "Log format (text, color, json)") +} + +// Configure sets the logrus logging level and formatter +func Configure() error { + lvl, err := logrus.ParseLevel(logLevel) + if err != nil { + return errors.Wrap(err, "parsing log level") + } + logrus.SetLevel(lvl) + + var formatter logrus.Formatter + switch logFormat { + case FormatText: + formatter = &logrus.TextFormatter{ + DisableColors: true, + } + case FormatColor: + formatter = &logrus.TextFormatter{ + ForceColors: true, + } + case FormatJSON: + formatter = &logrus.JSONFormatter{} + default: + return fmt.Errorf("not a valid log format: %q. Please specify one of (text, color, json)", logFormat) + } + logrus.SetFormatter(formatter) + + return nil +} diff --git a/pkg/util/util.go b/pkg/util/util.go index 3d3755ae8d..83f709801e 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -28,25 +28,10 @@ import ( "syscall" "github.com/minio/highwayhash" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" v1 "github.com/google/go-containerregistry/pkg/v1" ) -// ConfigureLogging sets the logrus logging level and forces logs to be colorful (!) -func ConfigureLogging(logLevel string) error { - lvl, err := logrus.ParseLevel(logLevel) - if err != nil { - return errors.Wrap(err, "parsing log level") - } - logrus.SetLevel(lvl) - logrus.SetFormatter(&logrus.TextFormatter{ - ForceColors: true, - }) - return nil -} - // Hasher returns a hash function, used in snapshotting to determine if a file has changed func Hasher() func(string) (string, error) { pool := sync.Pool{