From c0713893b6294d11c76d40276f199f128c4cb1a2 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Fri, 27 Mar 2020 21:48:04 +0100 Subject: [PATCH] Improve initial logger configuration Default logging level is now WARN If serve-api command is executed and log level is not explicitly specified with LOG_LEVEL, log level is set to INFO Root command now accepts -s/--silent and -d/--debug flags for log level control --- pkg/cli/commands.go | 24 +++++++++++++++++++++++- pkg/logger/logger.go | 7 +++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/pkg/cli/commands.go b/pkg/cli/commands.go index 90e5448ad..9f96eb0c6 100644 --- a/pkg/cli/commands.go +++ b/pkg/cli/commands.go @@ -1,8 +1,11 @@ package cli import ( + "github.com/cortezaproject/corteza-server/pkg/logger" "github.com/cortezaproject/corteza-server/pkg/version" "github.com/spf13/cobra" + "go.uber.org/zap" + "os" ) var ( @@ -40,7 +43,11 @@ var ( // Callback is called when not executed with help subcommand func RootCommand(ppRunEfn func() error) *cobra.Command { // Make a copy - var cmd = rootCommand + var ( + cmd = rootCommand + + silent, debug bool + ) cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) (err error) { if cmd.Name() == "help" { @@ -48,9 +55,18 @@ func RootCommand(ppRunEfn func() error) *cobra.Command { return nil } + if debug { + logger.DefaultLevel.SetLevel(zap.DebugLevel) + } else if silent { + logger.DefaultLevel.SetLevel(zap.FatalLevel) + } + return ppRunEfn() } + cmd.Flags().BoolVarP(&silent, "silent", "s", false, "No output") + cmd.Flags().BoolVarP(&debug, "debug", "d", false, "Debug") + return &cmd } @@ -59,6 +75,12 @@ func ServeCommand(runEfn func() error) *cobra.Command { var cmd = serveApiCommand cmd.RunE = func(cmd *cobra.Command, args []string) (err error) { + if _, set := os.LookupEnv("LOG_LEVEL"); !set { + // If LOG_LEVEL is not explicitly set, let's + // set it to INFO so that it + logger.DefaultLevel.SetLevel(zap.InfoLevel) + } + return runEfn() } diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index e6427bf29..0d922936f 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -36,6 +36,8 @@ func MakeDebugLogger() *zap.Logger { panic(err) } + logger.Debug("full debug mode enabled (LOG_DEBUG=true), enjoy all the colors and verbosity") + return logger } @@ -43,8 +45,9 @@ func Init() { var ( err error - // Set INFO as defaut log level - logLevel = zapcore.InfoLevel + // Set WARN as default log level -- running serve-api + // will override this if LOG_LEVEL is not set + logLevel = zapcore.WarnLevel // Do we want to enable debug logger // with a bit more dev-friendly output