Skip to content

Commit

Permalink
root: add --quiet option for disabling logging
Browse files Browse the repository at this point in the history
With the leveled logger in place, it's also interesting to the user to
disable any sort of logging, thus only the usual output from the commands
are printed, no other additional information. It's useful for scripting
any wrappers around lab.

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed May 14, 2021
1 parent 35abd0c commit 6da5d52
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 9 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,23 @@ func init() {
RootCmd.Flags().Bool("version", false, "Show the lab version")
RootCmd.PersistentFlags().Bool("no-pager", false, "Do not pipe output into a pager")
RootCmd.PersistentFlags().Bool("debug", false, "Enable debug logging level")
RootCmd.PersistentFlags().Bool("quiet", false, "Turn off any sort of logging. Only command output is printed")

// We need to set the logger level before any other piece of code is
// called, thus we make sure we don't lose any debug message, but for
// that we need to parse the args from command input.
err := RootCmd.ParseFlags(os.Args[1:])
// Handle the err != nil case later
if err == nil {
debugLogLevel, _ := RootCmd.Flags().GetBool("debug")
if debugLogLevel {
debugLogger, _ := RootCmd.Flags().GetBool("debug")
quietLogger, _ := RootCmd.Flags().GetBool("quiet")
if debugLogger && quietLogger {
log.Fatal("option --debug cannot be combined with --quiet")
}
if debugLogger {
log.SetLogLevel(logger.LOG_DEBUG)
} else if quietLogger {
log.SetLogLevel(logger.LOG_NONE)
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (

// Logger levels available
const (
LOG_ERROR = iota
LOG_NONE = iota
LOG_ERROR
LOG_INFO
LOG_DEBUG
)
Expand Down Expand Up @@ -53,9 +54,9 @@ func GetInstance() *logger {
}

// SetLogLevel set the level of the internal logger.
// Allowed values are LOG_{ERROR,INFO,DEBUG}.
// Allowed values are LOG_{ERROR,INFO,DEBUG,NONE}.
func (l *logger) SetLogLevel(level int) error {
if !(level >= LOG_ERROR && level <= LOG_DEBUG) {
if !(level >= LOG_NONE && level <= LOG_DEBUG) {
return errors.New("invalid log level")
}
l.level = level
Expand Down

0 comments on commit 6da5d52

Please sign in to comment.