Unverified Commit c3ebe416 authored by bryanl's avatar bryanl
Browse files

Add multiple levels of logging verbosity



Adds multiple levels of logging verbosity. Squelches jsonnet vm output
unless `-vv` is passed to `ks`.
Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent 25395ed0
......@@ -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")
......
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() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment