Skip to content

Commit

Permalink
Merge pull request #1031 from osela/log-format
Browse files Browse the repository at this point in the history
set log format using a flag
  • Loading branch information
tejal29 authored Feb 6, 2020
2 parents 343f418 + 9dd050b commit 03f2ac4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 29 deletions.
10 changes: 5 additions & 5 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
10 changes: 4 additions & 6 deletions cmd/warmer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,25 @@ 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()
}

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 {
Expand Down
3 changes: 0 additions & 3 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/"

Expand Down
76 changes: 76 additions & 0 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 0 additions & 15 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 03f2ac4

Please sign in to comment.