-
Notifications
You must be signed in to change notification settings - Fork 7
/
level.go
86 lines (79 loc) · 1.38 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
package log
import (
"fmt"
"strings"
)
// Level type
type Level uint32
// These are the different logging levels
const (
OFF Level = iota
FATAL
PANIC
ERROR
WARN
INFO
DEBUG
)
// String converts the Level to a string
func (level Level) String() string {
switch level {
case OFF:
return "OFF"
case FATAL:
return "FATAL"
case PANIC:
return "PANIC"
case ERROR:
return "ERROR"
case WARN:
return "WARN"
case INFO:
return "INFO"
case DEBUG:
return "DEBUG"
default:
return "UNKNOWN"
}
}
// ColorString converts the Level to a string with term colorful
func (level Level) ColorString() string {
switch level {
case OFF:
return "OFF"
case FATAL:
return "\033[35mFATAL\033[0m"
case PANIC:
return "\033[35mPANIC\033[0m"
case ERROR:
return "\033[31mERROR\033[0m"
case WARN:
return "\033[33mWARN\033[0m"
case INFO:
return "\033[32mINFO\033[0m"
case DEBUG:
return "\033[34mDEBUG\033[0m"
default:
return "UNKNOWN"
}
}
// ParseLevel takes a string level and returns the log level constant.
func ParseLevel(name string) (Level, error) {
switch strings.ToUpper(name) {
case "OFF":
return OFF, nil
case "FATAL":
return FATAL, nil
case "PANIC":
return PANIC, nil
case "ERROR":
return ERROR, nil
case "WARN":
return WARN, nil
case "INFO":
return INFO, nil
case "DEBUG":
return DEBUG, nil
}
return 0, fmt.Errorf("invalid log.Level: %q", name)
}