Skip to content

Commit

Permalink
✨ Add configurable log-level and log-format
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed May 11, 2022
1 parent bf2bca6 commit 131ae0d
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 3 deletions.
72 changes: 72 additions & 0 deletions cmd/flag_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
logLevel string
logFormat string
)

func init() {
var err error

Command.Flags().StringVarP(&logLevel, "log-level", "l", "info", "Log level (trace, debug, info, warning, error, fatal, panic)")
err = Command.RegisterFlagCompletionFunc(
"log-level",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"trace", "debug", "info", "warning", "error", "fatal", "panic"}, cobra.ShellCompDirectiveNoFileComp
},
)
if err != nil {
panic(err)
}

Command.Flags().StringVar(&logFormat, "log-format", "color", "Log format (color, plain, json)")
err = Command.RegisterFlagCompletionFunc(
"log-format",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"text", "json"}, cobra.ShellCompDirectiveNoFileComp
},
)
if err != nil {
panic(err)
}

cobra.OnInitialize(initLog)
}

func initLogLevel(level string) log.Level {
parsed, err := log.ParseLevel(level)
if err != nil {
log.WithField("level", logLevel).Warn("invalid log level. defaulting to info.")
logLevel = "info"
parsed = log.InfoLevel
}
log.SetLevel(parsed)

return parsed
}

func initLogFormat(format string) log.Formatter {
var formatter log.Formatter = &log.TextFormatter{}
switch format {
case "color", "c":
break
case "plain", "p":
formatter.(*log.TextFormatter).DisableColors = true
case "json", "j":
formatter = &log.JSONFormatter{}
default:
log.WithField("format", logFormat).Warn("invalid log formatter. defaulting to color.")
}
log.SetFormatter(formatter)
return formatter
}

func initLog() {
initLogLevel(logLevel)
initLogFormat(logFormat)
}
70 changes: 70 additions & 0 deletions cmd/flag_log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
log "github.com/sirupsen/logrus"
"reflect"
"testing"
)

func Test_initLog(t *testing.T) {
tests := []struct {
name string
}{
{"default"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
initLog()
})
}
}

func Test_initLogFormat(t *testing.T) {
type args struct {
format string
}
tests := []struct {
name string
args args
want log.Formatter
}{
{"default", args{"color"}, &log.TextFormatter{}},
{"plain", args{"plain"}, &log.TextFormatter{DisableColors: true}},
{"json", args{"json"}, &log.JSONFormatter{}},
{"unknown", args{""}, &log.TextFormatter{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := initLogFormat(tt.args.format); !reflect.DeepEqual(got, tt.want) {
t.Errorf("initLogFormat() = %v, want %v", got, tt.want)
}
})
}
}

func Test_initLogLevel(t *testing.T) {
type args struct {
level string
}
tests := []struct {
name string
args args
want log.Level
}{
{"trace", args{"trace"}, log.TraceLevel},
{"debug", args{"debug"}, log.DebugLevel},
{"info", args{"info"}, log.InfoLevel},
{"warning", args{"warning"}, log.WarnLevel},
{"error", args{"error"}, log.ErrorLevel},
{"fatal", args{"fatal"}, log.FatalLevel},
{"panic", args{"panic"}, log.PanicLevel},
{"unknown", args{""}, log.InfoLevel},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := initLogLevel(tt.args.level); got != tt.want {
t.Errorf("initLogLevel() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions docs/yampl.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ yampl [-i] [-p prefix] [-v key=value ...] [file ...]
-I, --indent int Override output indentation (default 2)
-i, --inplace Update files inplace
--left-delim string Override the left delimiter (default "{{")
--log-format string Log format (color, plain, json) (default "color")
-l, --log-level string Log level (trace, debug, info, warning, error, fatal, panic) (default "info")
-p, --prefix string Line-comments are ignored unless this prefix is found. Prefix must begin with '#' (default "#yampl")
--right-delim string Override the right delimiter (default "}}")
-s, --strict Trigger an error if a template variable is missing
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.18

require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

Expand All @@ -22,6 +22,8 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904 // indirect
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
Expand All @@ -43,6 +45,10 @@ golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
4 changes: 2 additions & 2 deletions internal/template/line_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package template
import (
"github.com/Masterminds/sprig/v3"
"github.com/clevyr/go-yampl/internal/config"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"log"
"strings"
"text/template"
)
Expand Down Expand Up @@ -34,7 +34,7 @@ func LineComment(conf config.Config, node *yaml.Node) error {
var buf strings.Builder
if err = tmpl.Execute(&buf, conf.Values); err != nil {
if !conf.Strict {
log.Printf("WARN: %v", err)
log.WithError(err).Warn("skipping value due to template error")
return nil
}
return err
Expand Down

0 comments on commit 131ae0d

Please sign in to comment.