3
0

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
This commit is contained in:
Denis Arh
2020-03-27 21:48:04 +01:00
parent d90d6ecdc6
commit c0713893b6
2 changed files with 28 additions and 3 deletions
+23 -1
View File
@@ -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()
}
+5 -2
View File
@@ -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