-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.go
113 lines (95 loc) · 1.85 KB
/
level.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package log
import (
"encoding/json"
"github.com/fatih/color"
"strings"
)
var (
red = color.New(color.FgRed).SprintfFunc()
yellow = color.New(color.FgYellow).SprintfFunc()
cyan = color.New(color.FgCyan).SprintfFunc()
blue = color.New(color.FgBlue).SprintfFunc()
//green = color.New(color.FgGreen).SprintfFunc()
//magenta = color.New(color.FgMagenta).SprintfFunc()
//white = color.New(color.FgWhite).SprintfFunc()
)
const (
// DEBUG debug level
DEBUG Level = 40
// INFO debug level
INFO Level = 30
// WARN warn level
WARN Level = 20
// ERROR error level
ERROR Level = 10
// OTHER empty value
OTHER Level = 0
)
// Level Logging Level
type Level int
func (l Level) String() string {
return LevelToStr(l)
}
// StringColor terminal colored string
func (l Level) StringColor() string {
var str string
switch l {
case DEBUG:
str = blue("DEBUG")
case INFO:
str = cyan("INFO")
case WARN:
str = yellow("WARN")
case ERROR:
str = red("ERROR")
default:
str = "OTHER"
}
return str
}
// MarshalJSON json serializer
func (l *Level) MarshalJSON() ([]byte, error) {
return json.Marshal(LevelToStr(*l))
}
// UnmarshalJSON json unserializer
func (l *Level) UnmarshalJSON(b []byte) error {
var str string
err := json.Unmarshal(b, &str)
*l = StrToLevel(str)
return err
}
// StrToLevel converts a string to a Level type
func StrToLevel(str string) Level {
str = strings.ToUpper(str)
var level Level
switch str {
case "DEBUG":
level = DEBUG
case "INFO":
level = INFO
case "WARN":
level = WARN
case "ERROR":
level = ERROR
default:
level = OTHER
}
return level
}
// LevelToStr Level type to a string
func LevelToStr(level Level) string {
var str string
switch level {
case DEBUG:
str = "DEBUG"
case INFO:
str = "INFO"
case WARN:
str = "WARN"
case ERROR:
str = "ERROR"
default:
str = "OTHER"
}
return str
}