-
Notifications
You must be signed in to change notification settings - Fork 1
/
level.go
129 lines (117 loc) · 3.48 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package logger
import (
"strings"
)
// Level represents the log Level
// The higher the value the more chances to write in the Stream
type Level byte
const (
// UNSET level means the level is not yet set
UNSET Level = iota * 10
// TRACE level should be used for entries that should be used by the developer of the app/package only
TRACE
// DEBUG level should be used for detailed logging as they tend to be noisy
DEBUG
// INFO level should be used as the standard level. Entries that really mean something to most people should go there
INFO
// WARN level should be used when the code considers a situation as not optimal but it can live with it
WARN
// ERROR level should be used when the code encounters an issue and normal flow is disrupted
ERROR
// FATAL level should be used when the code eoncounters an issue it cannot recover from
FATAL
// ALWAYS level should be used for entries that should always be logged, like app version, etc.
ALWAYS Level = 255
// NEVER level should be used for entries that should never be logged
NEVER Level = 1
)
// FilterSetter describes objects that can set their Filter Level
//
// The Filter Level is the level that will be used to filter what gets written to the Stream:
// Records with a level lower than the filter level will not be written
type FilterSetter interface {
// SetFilterLevel sets the filter level
//
// If present, the first parameter is the topic.
//
// If present, the second parameter is the scope.
SetFilterLevel(level Level, parameters ...string)
}
// FilterModifier describes objects that can modify their Filter Level
type FilterModifier interface {
// FilterMore tells the stream to filter more
FilterMore()
// FilterLess tells the stream to filter less
FilterLess()
}
// ParseLevel converts a string into a Level
func ParseLevel(value string) Level {
if level, ok := map[string]Level{
"NEVER": NEVER,
"TRACE": TRACE,
"DEBUG": DEBUG,
"INFO": INFO,
"WARN": WARN,
"ERROR": ERROR,
"FATAL": FATAL,
"ALWAYS": ALWAYS,
"UNSET": UNSET,
}[strings.ToUpper(value)]; ok {
return level
}
return NEVER
}
// GetLevelFromRecord retrieves the level from the given Record
func GetLevelFromRecord(record *Record) Level {
if record == nil {
return NEVER
}
if value, found := record.Find("level"); found {
if level, ok := value.(Level); ok {
return level
}
}
return NEVER
}
// Next returns the Level that follows the current one
//
// # If level is ALWAYS, it will return ALWAYS
//
// Example: TRACE.Next() will return DEBUG
func (level Level) Next() Level {
if level == ALWAYS {
return ALWAYS
}
return level 10
}
// Previous returns the Level that precedes the current one
//
// # If level is NEVER, it will return NEVER
//
// Example: DEBUG.Previous() will return TRACE
func (level Level) Previous() Level {
if level == NEVER {
return NEVER
}
return level - 10
}
// ShouldWrite tells if the current level is writeable when compared to the given filter level
//
// To be writeable, the current level must be higher than the filter level
func (level Level) ShouldWrite(filter Level) bool {
if level == NEVER || level == UNSET {
return false
}
return filter == ALWAYS || filter == UNSET || (filter != NEVER && level >= filter)
}
// String gets a string version
func (level Level) String() string {
// implements the fmt.Stringer interface
if level > FATAL {
return "ALWAYS"
}
if level == UNSET {
return "UNSET"
}
return []string{"NEVER", "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"}[level/10]
}