Skip to content

Commit

Permalink
More idiomatic logging config
Browse files Browse the repository at this point in the history
Update logging configuration to be more idiomatic for a Cobra
application
  • Loading branch information
cvgw committed Feb 8, 2020
1 parent 8d9e6b8 commit 7e2009b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
15 changes: 11 additions & 4 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ import (
)

var (
opts = &config.KanikoOptions{}
force bool
opts = &config.KanikoOptions{}
force bool
logLevel string
logFormat string
)

func init() {
logging.AddFlags(RootCmd.PersistentFlags())
RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", logging.DefaultLevel, "Log level (debug, info, warn, error, fatal, panic")
RootCmd.PersistentFlags().StringVar(&logFormat, "log-format", logging.FormatColor, "Log format (text, color, json)")

RootCmd.PersistentFlags().BoolVarP(&force, "force", "", false, "Force building outside of a container")

addKanikoOptionsFlags()
addHiddenFlags(RootCmd)
}
Expand All @@ -56,9 +61,11 @@ var RootCmd = &cobra.Command{
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cmd.Use == "executor" {
resolveEnvironmentBuildArgs(opts.BuildArgs, os.Getenv)
if err := logging.Configure(); err != nil {

if err := logging.Configure(logLevel, logFormat); err != nil {
return err
}

if !opts.NoPush && len(opts.Destinations) == 0 {
return errors.New("You must provide --destination, or use --no-push")
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/warmer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,26 @@ import (
)

var (
opts = &config.WarmerOptions{}
opts = &config.WarmerOptions{}
logLevel string
logFormat string
)

func init() {
logging.AddFlags(RootCmd.PersistentFlags())
RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", logging.DefaultLevel, "Log level (debug, info, warn, error, fatal, panic")
RootCmd.PersistentFlags().StringVar(&logFormat, "log-format", logging.FormatColor, "Log format (text, color, json)")

addKanikoOptionsFlags()
addHiddenFlags()
}

var RootCmd = &cobra.Command{
Use: "cache warmer",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := logging.Configure(); err != nil {
if err := logging.Configure(logLevel, logFormat); err != nil {
return err
}

if len(opts.Images) == 0 {
return errors.New("You must select at least one image to cache")
}
Expand Down
20 changes: 4 additions & 16 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)

const (
Expand All @@ -36,27 +35,16 @@ const (
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)
func Configure(level, format string) error {
lvl, err := logrus.ParseLevel(level)
if err != nil {
return errors.Wrap(err, "parsing log level")
}
logrus.SetLevel(lvl)

var formatter logrus.Formatter
switch logFormat {
switch format {
case FormatText:
formatter = &logrus.TextFormatter{
DisableColors: true,
Expand All @@ -68,7 +56,7 @@ func Configure() error {
case FormatJSON:
formatter = &logrus.JSONFormatter{}
default:
return fmt.Errorf("not a valid log format: %q. Please specify one of (text, color, json)", logFormat)
return fmt.Errorf("not a valid log format: %q. Please specify one of (text, color, json)", format)
}
logrus.SetFormatter(formatter)

Expand Down

0 comments on commit 7e2009b

Please sign in to comment.