github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/autoprofile/internal/logger/logger.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package logger
     5  
     6  import (
     7  	instalogger "github.com/mier85/go-sensor/logger"
     8  )
     9  
    10  // Level is the log level
    11  type Level uint8
    12  
    13  // Valid log levels compatible with github.com/mier85/go-sensor log level values
    14  const (
    15  	ErrorLevel Level = iota
    16  	WarnLevel
    17  	InfoLevel
    18  	DebugLevel
    19  )
    20  
    21  var defaultLogger LeveledLogger = instalogger.New(nil)
    22  
    23  // LeveledLogger is an interface of a generic logger that support different message levels.
    24  // By default instana.Sensor uses logger.Logger with log.Logger as an output, however this
    25  // interface is also compatible with such popular loggers as github.com/sirupsen/logrus.Logger
    26  // and go.uber.org/zap.SugaredLogger
    27  type LeveledLogger interface {
    28  	Debug(v ...interface{})
    29  	Info(v ...interface{})
    30  	Warn(v ...interface{})
    31  	Error(v ...interface{})
    32  }
    33  
    34  // SetLogLevel configures the min log level for autoprofile defaultLogger
    35  func SetLogLevel(level instalogger.Level) {
    36  	l, ok := defaultLogger.(*instalogger.Logger)
    37  	if !ok {
    38  		return
    39  	}
    40  
    41  	l.SetLevel(level)
    42  }
    43  
    44  // SetLogger sets the leveled logger to use to output the diagnostic messages and errors
    45  func SetLogger(l LeveledLogger) {
    46  	defaultLogger = l
    47  }
    48  
    49  // Debug writes log message with defaultLogger.Debug level
    50  func Debug(v ...interface{}) {
    51  	defaultLogger.Debug(v...)
    52  }
    53  
    54  // Info writes log message with defaultLogger.Info level
    55  func Info(v ...interface{}) {
    56  	defaultLogger.Info(v...)
    57  }
    58  
    59  // Warn writes log message with defaultLogger.Warn level
    60  func Warn(v ...interface{}) {
    61  	defaultLogger.Warn(v...)
    62  }
    63  
    64  // Error writes log message with defaultLogger.Error level
    65  func Error(v ...interface{}) {
    66  	defaultLogger.Error(v...)
    67  }