Merge pull request #1040 from cvgw/u/cvgw/more-idiomatic-logging-config
More idiomatic logging config
This commit is contained in:
commit
7c7d899633
|
|
@ -39,13 +39,18 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
opts = &config.KanikoOptions{}
|
opts = &config.KanikoOptions{}
|
||||||
force bool
|
force bool
|
||||||
|
logLevel string
|
||||||
|
logFormat string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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")
|
RootCmd.PersistentFlags().BoolVarP(&force, "force", "", false, "Force building outside of a container")
|
||||||
|
|
||||||
addKanikoOptionsFlags()
|
addKanikoOptionsFlags()
|
||||||
addHiddenFlags(RootCmd)
|
addHiddenFlags(RootCmd)
|
||||||
}
|
}
|
||||||
|
|
@ -56,9 +61,11 @@ var RootCmd = &cobra.Command{
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if cmd.Use == "executor" {
|
if cmd.Use == "executor" {
|
||||||
resolveEnvironmentBuildArgs(opts.BuildArgs, os.Getenv)
|
resolveEnvironmentBuildArgs(opts.BuildArgs, os.Getenv)
|
||||||
if err := logging.Configure(); err != nil {
|
|
||||||
|
if err := logging.Configure(logLevel, logFormat); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !opts.NoPush && len(opts.Destinations) == 0 {
|
if !opts.NoPush && len(opts.Destinations) == 0 {
|
||||||
return errors.New("You must provide --destination, or use --no-push")
|
return errors.New("You must provide --destination, or use --no-push")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,11 +29,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
opts = &config.WarmerOptions{}
|
opts = &config.WarmerOptions{}
|
||||||
|
logLevel string
|
||||||
|
logFormat string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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()
|
addKanikoOptionsFlags()
|
||||||
addHiddenFlags()
|
addHiddenFlags()
|
||||||
}
|
}
|
||||||
|
|
@ -41,9 +45,10 @@ func init() {
|
||||||
var RootCmd = &cobra.Command{
|
var RootCmd = &cobra.Command{
|
||||||
Use: "cache warmer",
|
Use: "cache warmer",
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if err := logging.Configure(); err != nil {
|
if err := logging.Configure(logLevel, logFormat); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(opts.Images) == 0 {
|
if len(opts.Images) == 0 {
|
||||||
return errors.New("You must select at least one image to cache")
|
return errors.New("You must select at least one image to cache")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ import (
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/pflag"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -36,27 +35,16 @@ const (
|
||||||
FormatJSON = "json"
|
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
|
// Configure sets the logrus logging level and formatter
|
||||||
func Configure() error {
|
func Configure(level, format string) error {
|
||||||
lvl, err := logrus.ParseLevel(logLevel)
|
lvl, err := logrus.ParseLevel(level)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "parsing log level")
|
return errors.Wrap(err, "parsing log level")
|
||||||
}
|
}
|
||||||
logrus.SetLevel(lvl)
|
logrus.SetLevel(lvl)
|
||||||
|
|
||||||
var formatter logrus.Formatter
|
var formatter logrus.Formatter
|
||||||
switch logFormat {
|
switch format {
|
||||||
case FormatText:
|
case FormatText:
|
||||||
formatter = &logrus.TextFormatter{
|
formatter = &logrus.TextFormatter{
|
||||||
DisableColors: true,
|
DisableColors: true,
|
||||||
|
|
@ -68,7 +56,7 @@ func Configure() error {
|
||||||
case FormatJSON:
|
case FormatJSON:
|
||||||
formatter = &logrus.JSONFormatter{}
|
formatter = &logrus.JSONFormatter{}
|
||||||
default:
|
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)
|
logrus.SetFormatter(formatter)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue