github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/log/level.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package log 7 8 import ( 9 "bytes" 10 "fmt" 11 "os" 12 "strings" 13 14 "github.com/gitbundle/modules/json" 15 ) 16 17 // Level is the level of the logger 18 type Level int 19 20 const ( 21 // TRACE represents the lowest log level 22 TRACE Level = iota 23 // DEBUG is for debug logging 24 DEBUG 25 // INFO is for information 26 INFO 27 // WARN is for warning information 28 WARN 29 // ERROR is for error reporting 30 ERROR 31 // CRITICAL is for critical errors 32 CRITICAL 33 // FATAL is for fatal errors 34 FATAL 35 // NONE is for no logging 36 NONE 37 ) 38 39 var toString = map[Level]string{ 40 TRACE: "trace", 41 DEBUG: "debug", 42 INFO: "info", 43 WARN: "warn", 44 ERROR: "error", 45 CRITICAL: "critical", 46 FATAL: "fatal", 47 NONE: "none", 48 } 49 50 var toLevel = map[string]Level{ 51 "trace": TRACE, 52 "debug": DEBUG, 53 "info": INFO, 54 "warn": WARN, 55 "error": ERROR, 56 "critical": CRITICAL, 57 "fatal": FATAL, 58 "none": NONE, 59 } 60 61 // Levels returns all the possible logging levels 62 func Levels() []string { 63 keys := make([]string, 0) 64 for key := range toLevel { 65 keys = append(keys, key) 66 } 67 return keys 68 } 69 70 func (l Level) String() string { 71 s, ok := toString[l] 72 if ok { 73 return s 74 } 75 return "info" 76 } 77 78 func (l Level) Icon() []byte { 79 switch l { 80 case TRACE: 81 return []byte("👣") 82 case DEBUG: 83 return []byte("🐵") 84 case INFO: 85 return []byte("👉") 86 case WARN: 87 return []byte("🚸") 88 case ERROR: 89 return []byte("🔴") 90 case CRITICAL: 91 return []byte("🚨") 92 case FATAL: 93 return []byte("💣") 94 case NONE: 95 return []byte("✋") 96 default: 97 return []byte("🤔") 98 } 99 } 100 101 // Color returns the color string for this Level 102 func (l Level) Color() *[]byte { 103 color, ok := levelToColor[l] 104 if ok { 105 return &(color) 106 } 107 none := levelToColor[NONE] 108 return &none 109 } 110 111 // MarshalJSON takes a Level and turns it into text 112 func (l Level) MarshalJSON() ([]byte, error) { 113 buffer := bytes.NewBufferString(`"`) 114 buffer.WriteString(toString[l]) 115 buffer.WriteString(`"`) 116 return buffer.Bytes(), nil 117 } 118 119 // FromString takes a level string and returns a Level 120 func FromString(level string) Level { 121 temp, ok := toLevel[strings.ToLower(level)] 122 if !ok { 123 return INFO 124 } 125 return temp 126 } 127 128 // UnmarshalJSON takes text and turns it into a Level 129 func (l *Level) UnmarshalJSON(b []byte) error { 130 var tmp interface{} 131 err := json.Unmarshal(b, &tmp) 132 if err != nil { 133 fmt.Fprintf(os.Stderr, "Err: %v", err) 134 return err 135 } 136 137 switch v := tmp.(type) { 138 case string: 139 *l = FromString(v) 140 case int: 141 *l = FromString(Level(v).String()) 142 default: 143 *l = INFO 144 } 145 return nil 146 }