github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/utils/gadgettracermanagerloglevel/gadgettracermanagerloglevel.go (about)

     1  package gadgettracermanagerloglevel
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"sync"
     7  
     8  	log "github.com/sirupsen/logrus"
     9  )
    10  
    11  var (
    12  	loglevel log.Level
    13  	once     sync.Once
    14  )
    15  
    16  const EnvName = "GADGET_TRACER_MANAGER_LOG_LEVEL"
    17  
    18  func LogLevel() log.Level {
    19  	once.Do(func() {
    20  		strLevels := make([]string, len(log.AllLevels))
    21  		for i, level := range log.AllLevels {
    22  			strLevels[i] = level.String()
    23  		}
    24  		if val, ok := os.LookupEnv(EnvName); ok {
    25  			if level, parseErr := log.ParseLevel(val); parseErr == nil {
    26  				loglevel = level
    27  				return
    28  			} else {
    29  				log.WithError(parseErr).Errorf("Invalid log level, valid levels are: %v, defaulting to %s", strings.Join(strLevels, ", "), log.InfoLevel)
    30  			}
    31  		}
    32  		loglevel = log.InfoLevel
    33  	})
    34  
    35  	return loglevel
    36  }