Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More idiomatic logging config #1040

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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