github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/logging/level.go (about) 1 package logging 2 3 import ( 4 "strings" 5 ) 6 7 // Level represents a log level. Its value hierarchy is designed to be ordered 8 // and comparable by value. 9 type Level uint 10 11 const ( 12 // LevelDisabled indicates that logging is completely disabled. 13 LevelDisabled Level = iota 14 // LevelError indicates that only fatal errors are logged. 15 LevelError 16 // LevelWarn indicates that both fatal and non-fatal errors are logged. 17 LevelWarn 18 // LevelInfo indicates that basic execution information is logged (in 19 // addition to all errors). 20 LevelInfo 21 // LevelDebug indicates that advanced execution information is logged (in 22 // addition to basic information and all errors). 23 LevelDebug 24 // LevelTrace indicates that low-level execution information is logged (in 25 // addition to all other execution information and all errors). 26 LevelTrace 27 ) 28 29 // levelNames are the human-readable representations of log levels. 30 var levelNames = [6]string{ 31 "disabled", 32 "error", 33 "warn", 34 "info", 35 "debug", 36 "trace", 37 } 38 39 // String provides a human-readable representation of a log level. 40 func (l Level) String() string { 41 if l <= LevelTrace { 42 return levelNames[l] 43 } 44 return "unknown" 45 } 46 47 // NameToLevel converts a string-based representation of a log level to the 48 // appropriate Level value. It returns a boolean indicating whether or not the 49 // conversion was valid. If the name is invalid, LevelDisabled is returned. 50 func NameToLevel(name string) (Level, bool) { 51 switch name { 52 case "disabled": 53 return LevelDisabled, true 54 case "error": 55 return LevelError, true 56 case "warn": 57 return LevelWarn, true 58 case "info": 59 return LevelInfo, true 60 case "debug": 61 return LevelDebug, true 62 case "trace": 63 return LevelTrace, true 64 default: 65 return LevelDisabled, false 66 } 67 } 68 69 // abbreviations is the range of abbreviations to use for log levels. 70 const abbreviations = "_EWIDT" 71 72 // abbreviation returns a one-byte prefix to use for the level in log lines. 73 func (l Level) abbreviation() byte { 74 if l <= LevelTrace { 75 return abbreviations[l] 76 } 77 return '?' 78 } 79 80 // abbreviationToLevel converts a one-byte prefix representation of a log level 81 // to the appropriate Level value. It returns a boolean indicating whether or 82 // not the conversion was valid. If the abbreviation is invalid, LevelDisabled 83 // is returned. 84 func abbreviationToLevel(abbreviation byte) (Level, bool) { 85 if index := strings.IndexByte(abbreviations, abbreviation); index != -1 { 86 return Level(index), true 87 } 88 return LevelDisabled, false 89 }