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

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2016
     3  
     4  package instana
     5  
     6  import (
     7  	"github.com/mier85/go-sensor/logger"
     8  )
     9  
    10  // Valid log levels
    11  const (
    12  	Error = 0
    13  	Warn  = 1
    14  	Info  = 2
    15  	Debug = 3
    16  )
    17  
    18  // LeveledLogger is an interface of a generic logger that support different message levels.
    19  // By default instana.Sensor uses logger.Logger with log.Logger as an output, however this
    20  // interface is also compatible with such popular loggers as github.com/sirupsen/logrus.Logger
    21  // and go.uber.org/zap.SugaredLogger
    22  type LeveledLogger interface {
    23  	Debug(v ...interface{})
    24  	Info(v ...interface{})
    25  	Warn(v ...interface{})
    26  	Error(v ...interface{})
    27  }
    28  
    29  var defaultLogger LeveledLogger = logger.New(nil)
    30  
    31  // SetLogger configures the default logger to be used by Instana go-sensor. Note that changing the logger
    32  // will not affect already initialized instana.Sensor instances. To make them use the new logger please call
    33  // (*instana.Sensor).SetLogger() explicitly.
    34  func SetLogger(l LeveledLogger) {
    35  	defaultLogger = l
    36  
    37  	// if the sensor has already been initialized, we need to update its logger too
    38  	if sensor != nil {
    39  		sensor.setLogger(l)
    40  	}
    41  }
    42  
    43  // setLogLevel translates legacy Instana log levels into logger.Logger levels.
    44  // Any level that is greater than instana.Debug is interpreted as logger.DebugLevel.
    45  func setLogLevel(l *logger.Logger, level int) {
    46  	switch level {
    47  	case Error:
    48  		l.SetLevel(logger.ErrorLevel)
    49  	case Warn:
    50  		l.SetLevel(logger.WarnLevel)
    51  	case Info:
    52  		l.SetLevel(logger.InfoLevel)
    53  	default:
    54  		l.SetLevel(logger.DebugLevel)
    55  	}
    56  }