Permalink
Browse files

Merge pull request #523 from bryanl/log-verbosity-handling

Add multiple levels of logging verbosity
  • Loading branch information...
bryanl committed May 8, 2018
2 parents 25395ed + c3ebe41 commit 6f2791e07bf8a71d6bdb6ede4b4977d434f69bd9
Showing with 65 additions and 36 deletions.
  1. +3 −25 pkg/clicmd/root.go
  2. +39 −0 pkg/log/log.go
  3. +23 −11 pkg/util/jsonnet/vm.go
@@ -30,12 +30,12 @@ import (
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/app"
"github.com/ksonnet/ksonnet/pkg/env"
"github.com/ksonnet/ksonnet/pkg/log"
"github.com/ksonnet/ksonnet/pkg/pipeline"
"github.com/ksonnet/ksonnet/pkg/plugin"
str "github.com/ksonnet/ksonnet/pkg/util/strings"
"github.com/ksonnet/ksonnet/template"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
@@ -68,21 +68,13 @@ application configuration to remote clusters.
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
goflag.CommandLine.Parse([]string{})
flags := cmd.Flags()
out := cmd.OutOrStderr()
log.SetOutput(out)
logFmt := &log.TextFormatter{
DisableTimestamp: true,
DisableLevelTruncation: true,
QuoteEmptyFields: true,
}
log.SetFormatter(logFmt)
verbosity, err := flags.GetCount(flagVerbose)
if err != nil {
return err
}
log.SetLevel(logLevel(verbosity))
log.Init(verbosity, cmd.OutOrStderr())
wd, err := os.Getwd()
if err != nil {
@@ -155,15 +147,6 @@ func runPlugin(p plugin.Plugin, args []string) error {
return cmd.Run()
}
func logLevel(verbosity int) log.Level {
switch verbosity {
case 0:
return log.InfoLevel
default:
return log.DebugLevel
}
}
func newExpander(fs afero.Fs, cmd *cobra.Command) (*template.Expander, error) {
flags := cmd.Flags()
spec := template.NewExpander(fs)
@@ -256,11 +239,6 @@ func newCmdObjExpander(c cmdObjExpanderConfig) *cmdObjExpander {
// Expands expands the templates.
func (te *cmdObjExpander) Expand() ([]*unstructured.Unstructured, error) {
// expander, err := te.templateExpanderFn(te.config.fs, te.config.cmd)
// if err != nil {
// return nil, errors.Wrap(err, "template expander")
// }
manager, err := metadata.Find(te.config.cwd)
if err != nil {
return nil, errors.Wrap(err, "find metadata")
@@ -0,0 +1,39 @@
package log
import (
"io"
"github.com/sirupsen/logrus"
)
var (
// VerbosityLevel is the current verbosity level.
VerbosityLevel = 0
)
// Init initializes ksonnet's logger.
func Init(verbosity int, w io.Writer) {
logrus.SetOutput(w)
logrus.SetFormatter(defaultLogFmt())
logrus.SetLevel(logLevel(verbosity))
VerbosityLevel = verbosity
logrus.WithField("verbosity-level", verbosity).Debug("setting log verbosity")
}
func defaultLogFmt() logrus.Formatter {
return &logrus.TextFormatter{
DisableTimestamp: true,
DisableLevelTruncation: true,
QuoteEmptyFields: true,
}
}
func logLevel(verbosity int) logrus.Level {
switch verbosity {
case 0:
return logrus.InfoLevel
default:
return logrus.DebugLevel
}
}
@@ -27,6 +27,7 @@ import (
"github.com/google/go-jsonnet"
"github.com/google/go-jsonnet/ast"
"github.com/ksonnet/ksonnet/pkg/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
@@ -110,9 +111,12 @@ func (vm *VM) EvaluateSnippet(name, snippet string) (string, error) {
now := time.Now()
fields := logrus.Fields{
"jPaths": strings.Join(vm.jPaths, ", "),
"name": name,
"snippet": snippet,
"jPaths": strings.Join(vm.jPaths, ", "),
"name": name,
}
if log.VerbosityLevel >= 2 {
fields["snippet"] = snippet
}
jvm := jsonnet.MakeVM()
@@ -126,26 +130,34 @@ func (vm *VM) EvaluateSnippet(name, snippet string) (string, error) {
for k, v := range vm.extCodes {
jvm.ExtCode(k, v)
key := fmt.Sprintf("extCode#%s", k)
fields[key] = v
if log.VerbosityLevel >= 2 {
key := fmt.Sprintf("extCode#%s", k)
fields[key] = v
}
}
for k, v := range vm.extVars {
jvm.ExtVar(k, v)
key := fmt.Sprintf("extVar#%s", k)
fields[key] = v
if log.VerbosityLevel >= 2 {
key := fmt.Sprintf("extVar#%s", k)
fields[key] = v
}
}
for k, v := range vm.tlaCodes {
jvm.TLACode(k, v)
key := fmt.Sprintf("tlaCode#%s", k)
fields[key] = v
if log.VerbosityLevel >= 2 {
key := fmt.Sprintf("tlaCode#%s", k)
fields[key] = v
}
}
for k, v := range vm.tlaVars {
jvm.TLAVar(k, v)
key := fmt.Sprintf("tlaVar#%s", k)
fields[key] = v
if log.VerbosityLevel >= 2 {
key := fmt.Sprintf("tlaVar#%s", k)
fields[key] = v
}
}
defer func() {

0 comments on commit 6f2791e

Please sign in to comment.