-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.go
More file actions
82 lines (65 loc) · 1.47 KB
/
level.go
File metadata and controls
82 lines (65 loc) · 1.47 KB
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
package log
import (
"os"
"strings"
)
// Level defines the logging level type
type Level int
// Log level controls the logging threshold, which log statements will be output.
// For example, Debug level will not output Trace statements, but will output Debug, Info, and the rest.
const (
// None is no logging
None Level = iota
// Trace logs verbose details and, really should be metricss
Trace
// Debug logs Debug and above
Debug
// Info logs Info and above
Info
// Warn logs Warn and above
Warn
// Error logs Error and above
Error
)
var (
level = Default()
levels = []string{"None", "Trace", "Debug", "Info", "Warn", "Error"}
)
// Default returns the default log level
// LOG_LEVEL env var overrides the default of Info
func Default() Level {
s := os.Getenv("LOG_LEVEL")
l := FromString(s)
if l == -1 {
return Info
}
return l
}
// String returns the level as a string
func (l Level) String() string {
if l >= None && l <= Error {
return levels[l]
}
return "unknown"
}
// FromString converts the string representation of level into its const
// It does a case-insensitive comparison
// Invalid level string returns -1
func FromString(s string) Level {
for l, str := range levels {
if strings.EqualFold(s, str) {
return Level(l)
}
}
return -1
}
// GetLevel returns the current logging level
func GetLevel() Level {
return level
}
// SetLevel sets the current logging level
func SetLevel(l Level) {
if l >= None && l <= Error {
level = l
}
}