github.com/mongodb/grip@v0.0.0-20240213223901-f906268d82b9/level/level.go (about)

     1  /*
     2  Package level defines a Priority type and some conversion methods for a 7-tiered
     3  logging level schema, which mirror syslog and system's logging levels.
     4  
     5  Levels range from Emergency (0) to Debug (7), and the special type
     6  Priority and associated constants provide access to these values.
     7  
     8  */
     9  package level
    10  
    11  import "strings"
    12  
    13  // Priority is an integer that tracks log levels. Use with one of the
    14  // defined constants.
    15  type Priority int16
    16  
    17  // Constants defined for easy access to
    18  const (
    19  	Emergency Priority = 100
    20  	Alert     Priority = 90
    21  	Critical  Priority = 80
    22  	Error     Priority = 70
    23  	Warning   Priority = 60
    24  	Notice    Priority = 50
    25  	Info      Priority = 40
    26  	Debug     Priority = 30
    27  	Trace     Priority = 20
    28  	Invalid   Priority = 0
    29  )
    30  
    31  // String implements the Stringer interface and makes it possible to
    32  // print human-readable string identifier for a log level.
    33  func (p Priority) String() string {
    34  	switch p {
    35  	case 100:
    36  		return "emergency"
    37  	case 90:
    38  		return "alert"
    39  	case 80:
    40  		return "critical"
    41  	case 70:
    42  		return "error"
    43  	case 60:
    44  		return "warning"
    45  	case 50:
    46  		return "notice"
    47  	case 40:
    48  		return "info"
    49  	case 30:
    50  		return "debug"
    51  	case 20:
    52  		return "trace"
    53  	default:
    54  		return "invalid"
    55  	}
    56  }
    57  
    58  // IsValid returns false when the priority valid is not a valid
    59  // priority value.
    60  func (p Priority) IsValid() bool {
    61  	return p > 1 && p <= 100
    62  }
    63  
    64  // FromString takes a string, (case insensitive, leading and trailing space removed, )
    65  func FromString(l string) Priority {
    66  	switch strings.TrimSpace(strings.ToLower(l)) {
    67  	case "emergency":
    68  		return Emergency
    69  	case "alert":
    70  		return Alert
    71  	case "critical":
    72  		return Critical
    73  	case "error":
    74  		return Error
    75  	case "warning":
    76  		return Warning
    77  	case "notice":
    78  		return Notice
    79  	case "info":
    80  		return Info
    81  	case "debug":
    82  		return Debug
    83  	case "trace":
    84  		return Trace
    85  	default:
    86  		return Invalid
    87  	}
    88  }