Log level is configurable via verbosity flag
Fixes
@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/signal"
+ "strconv"
"time"
"github.com/heptio/developer-dash/internal/dash"
@@ -13,6 +14,7 @@ import (
"github.com/heptio/go-telemetry/pkg/telemetry"
"github.com/spf13/cobra"
"go.uber.org/zap"
+ "go.uber.org/zap/zapcore"
"k8s.io/client-go/tools/clientcmd"
)
@@ -20,6 +22,7 @@ func newDashCmd() *cobra.Command {
var namespace string
var uiURL string
var kubeconfig string
+ var verboseLevel int
dashCmd := &cobra.Command{
Use: "dash",
@@ -29,11 +32,10 @@ func newDashCmd() *cobra.Command {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
- // Set to 9 for verbose.
- // TODO control via cobra verbose flag along with zap.
- flag.CommandLine.Parse([]string{"-logtostderr", "-v", "0"}) // Set glog to verbose
+ // Configure glog verbosity (in client-go)
+ flag.CommandLine.Parse([]string{"-logtostderr", "-v", strconv.Itoa(verboseLevel)}) // Set glog to verbose
- z, err := zap.NewDevelopment()
+ z, err := newZapLogger(verboseLevel)
if err != nil {
fmt.Printf("failed to initialize logger: %v\n", err)
os.Exit(1)
@@ -79,8 +81,9 @@ func newDashCmd() *cobra.Command {
},
}
- dashCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "Initial namespace")
- dashCmd.Flags().StringVar(&uiURL, "ui-url", "", "UI URL")
+ dashCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "initial namespace")
+ dashCmd.Flags().StringVar(&uiURL, "ui-url", "", "dashboard url")
+ dashCmd.Flags().CountVarP(&verboseLevel, "verbose", "v", "verbosity level")
kubeconfig = clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
@@ -107,3 +110,25 @@ func newTelemetry(logger log.Logger) telemetry.Interface {
return telemetryClient
}
+
+// Returns a new zap logger, setting level according to the provided
+// verbosity level as an offset of the base level, Info.
+// i.e. verboseLevel==0, level==Info
+// verboseLevel==1, level==Debug
+func newZapLogger(verboseLevel int) (*zap.Logger, error) {
+ level := zapcore.InfoLevel - zapcore.Level(verboseLevel)
+ if level < zapcore.DebugLevel || level > zapcore.FatalLevel {
+ level = zapcore.DebugLevel
+ }
+
+ cfg := zap.Config{
+ Level: zap.NewAtomicLevelAt(level),
+ Development: true,
+ Encoding: "console",
+ EncoderConfig: zap.NewDevelopmentEncoderConfig(),
+ OutputPaths: []string{"stderr"},
+ ErrorOutputPaths: []string{"stderr"},
+ }
+
+ return cfg.Build()
+}
