github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/logr/log_level.go (about)

     1  package logr
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // Level type
     9  type Level uint32
    10  
    11  const (
    12  	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
    13  	// Commonly used for hooks to send errors to an error tracking service.
    14  	ErrorLevel Level = iota
    15  	// WarnLevel level. Non-critical entries that deserve eyes.
    16  	WarnLevel
    17  	// InfoLevel level. General operational entries about what's going on inside the
    18  	// application.
    19  	InfoLevel
    20  	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
    21  	DebugLevel
    22  )
    23  
    24  // ParseLevel takes a string level and returns the Logrus log level constant.
    25  func ParseLevel(lvl string) (Level, error) {
    26  	switch strings.ToLower(lvl) {
    27  	case "error":
    28  		return ErrorLevel, nil
    29  	case "warn", "warning":
    30  		return WarnLevel, nil
    31  	case "info":
    32  		return InfoLevel, nil
    33  	case "debug":
    34  		return DebugLevel, nil
    35  	}
    36  
    37  	var l Level
    38  	return l, fmt.Errorf("not a valid level: %q", lvl)
    39  }
    40  
    41  func (lvl Level) String() string {
    42  	if b, err := lvl.MarshalText(); err == nil {
    43  		return string(b)
    44  	} else {
    45  		return "unknown"
    46  	}
    47  }
    48  
    49  func (lvl *Level) UnmarshalText(text []byte) error {
    50  	l, err := ParseLevel(string(text))
    51  	if err != nil {
    52  		return err
    53  	}
    54  	*lvl = l
    55  	return nil
    56  }
    57  
    58  func (lvl Level) MarshalText() ([]byte, error) {
    59  	switch lvl {
    60  	case DebugLevel:
    61  		return []byte("debug"), nil
    62  	case InfoLevel:
    63  		return []byte("info"), nil
    64  	case WarnLevel:
    65  		return []byte("warning"), nil
    66  	case ErrorLevel:
    67  		return []byte("error"), nil
    68  	}
    69  
    70  	return nil, fmt.Errorf("not a valid level %d", lvl)
    71  }