From 7d226ca5207c68d62da32ae51462bc25087fde5c Mon Sep 17 00:00:00 2001 From: "Jip J. Dekker" Date: Sat, 19 Mar 2016 21:35:06 +0100 Subject: [PATCH] Adds flags for log levels --- cmd/root.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 23a887f..2256ef5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,9 +17,15 @@ package cmd import ( "os" + log "github.com/Sirupsen/logrus" "github.com/spf13/cobra" ) +var ( + veryVerbose bool + verbose bool +) + // This represents the base command when called without any subcommands var RootCmd = &cobra.Command{ Use: "ponder", @@ -42,12 +48,18 @@ func Execute() { } func init() { - // Here you will define your flags and configuration settings. - // Cobra supports Persistent Flags, which, if defined here, - // will be global for your application. + RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output of application events") + RootCmd.PersistentFlags().BoolVar(&veryVerbose, "vv", false, "Debug output of application events") - // RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ponder.yaml)") - // Cobra also supports local flags, which will only run - // when this action is called directly. - // RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + setLogLevel() +} + +func setLogLevel() { + if veryVerbose { + log.SetLevel(log.DebugLevel) + } else if verbose { + log.SetLevel(log.InfoLevel) + } else { + log.SetLevel(log.WarnLevel) + } }