From b253e6dc875adcf0fdd9c7a7ee8868adfdc9a3f7 Mon Sep 17 00:00:00 2001 From: Fangbao Li <94163234+lfbdev@users.noreply.github.com> Date: Tue, 8 Feb 2022 11:39:35 +0800 Subject: [PATCH] add debug level switch (#175) * add opt for debug swith * add opt for debug swith * update for pr --- cmd/devstream/apply.go | 1 - cmd/devstream/main.go | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/devstream/apply.go b/cmd/devstream/apply.go index 8d43cfbfb..037702ea9 100644 --- a/cmd/devstream/apply.go +++ b/cmd/devstream/apply.go @@ -20,7 +20,6 @@ DevStream will generate and execute a new plan based on the config file and the func applyCMDFunc(cmd *cobra.Command, args []string) { log.Info("Apply started.") - if err := pluginengine.Apply(configFile, continueDirectly); err != nil { log.Errorf("Apply error: %s.", err) os.Exit(1) diff --git a/cmd/devstream/main.go b/cmd/devstream/main.go index 3adeac876..7cbdc9881 100644 --- a/cmd/devstream/main.go +++ b/cmd/devstream/main.go @@ -3,11 +3,11 @@ package main import ( "strings" - "github.com/merico-dev/stream/internal/pkg/log" - + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" + "github.com/merico-dev/stream/internal/pkg/log" "github.com/merico-dev/stream/internal/pkg/pluginengine" ) @@ -15,6 +15,7 @@ var ( configFile string pluginDir string continueDirectly bool + isDebug bool rootCMD = &cobra.Command{ Use: "dtm", @@ -29,6 +30,9 @@ var ( # # # # # # # # # # # # # # # ###### ###### ## ##### # # # ###### # # # # `, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + initLog() + }, } ) @@ -38,7 +42,7 @@ func init() { rootCMD.PersistentFlags().StringVarP(&configFile, "config-file", "f", "config.yaml", "config file") rootCMD.PersistentFlags().StringVarP(&pluginDir, "plugin-dir", "p", pluginengine.DefaultPluginDir, "plugins directory") rootCMD.PersistentFlags().BoolVarP(&continueDirectly, "yes", "y", false, "apply/delete directly without confirmation") - + rootCMD.PersistentFlags().BoolVarP(&isDebug, "debug", "d", false, "debug level log") rootCMD.AddCommand(versionCMD) rootCMD.AddCommand(initCMD) rootCMD.AddCommand(applyCMD) @@ -68,6 +72,15 @@ func initConfig() { } } +func initLog() { + if isDebug { + logrus.SetLevel(logrus.DebugLevel) + } else { + logrus.SetLevel(logrus.InfoLevel) + } + log.Info("Log level is: ", logrus.GetLevel()) +} + func main() { err := rootCMD.Execute() if err != nil {