github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/lib/log/debuglogger/api.go (about)

     1  package debuglogger
     2  
     3  import (
     4  	"github.com/Cloud-Foundations/Dominator/lib/log"
     5  )
     6  
     7  type Logger struct {
     8  	level int16
     9  	log.Logger
    10  }
    11  
    12  // New will create a Logger from an existing log.Logger, adding methods for
    13  // debug logs. Debug messages will be logged or ignored depending on their debug
    14  // level. By default, the max debug level is -1, meaning all debug logs are
    15  // dropped (ignored).
    16  func New(logger log.Logger) *Logger {
    17  	return &Logger{-1, logger}
    18  }
    19  
    20  // Upgrade will upgrade a log.Logger, adding methods for debug logs. If the
    21  // provided logger is a log.DebugLogger, it is simply returned, otherwise the
    22  // logger is wrapped in a new Logger.
    23  func Upgrade(logger log.Logger) log.DebugLogger {
    24  	if logger, ok := logger.(log.DebugLogger); ok {
    25  		return logger
    26  	}
    27  	return New(logger)
    28  }
    29  
    30  // Debug will call the Print method if level is less than or equal to the max
    31  // debug level for the Logger.
    32  func (l *Logger) Debug(level uint8, v ...interface{}) {
    33  	if l.level >= int16(level) {
    34  		l.Print(v...)
    35  	}
    36  }
    37  
    38  // Debugf will call the Printf method if level is less than or equal to the max
    39  // debug level for the Logger.
    40  func (l *Logger) Debugf(level uint8, format string, v ...interface{}) {
    41  	if l.level >= int16(level) {
    42  		l.Printf(format, v...)
    43  	}
    44  }
    45  
    46  // Debugln will call the Println method if level is less than or equal to the
    47  // max debug level for the Logger.
    48  func (l *Logger) Debugln(level uint8, v ...interface{}) {
    49  	if l.level >= int16(level) {
    50  		l.Println(v...)
    51  	}
    52  }
    53  
    54  // GetLevel gets the current maximum debug level.
    55  func (l *Logger) GetLevel() int16 {
    56  	return l.level
    57  }
    58  
    59  // SetLevel sets the maximum debug level. A negative level will cause all debug
    60  // messages to be dropped.
    61  func (l *Logger) SetLevel(maxLevel int16) {
    62  	l.level = maxLevel
    63  }