Skip to content

Commit 5f64b88

Browse files
committed
Added ParseLevel
1 parent f7bae98 commit 5f64b88

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

lib.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@ package log
22

33
import (
44
golog "log"
5+
"strings"
56
)
67

78
// Level Log level to use in `SetLevel()` function
89
type Level int
910

11+
func (l Level) String() string {
12+
if l == LevelFatal {
13+
return "FATAL"
14+
} else if l == LevelError {
15+
return "ERROR"
16+
} else if l == LevelWarn {
17+
return "WARN"
18+
} else if l == LevelInfo {
19+
return "INFO"
20+
} else if l == LevelDebug {
21+
return "DEBUG"
22+
} else if l == LevelTrace {
23+
return "TRACE"
24+
}
25+
return "UNKNOWN"
26+
}
27+
1028
const (
1129
// LevelFatal to be used with `SetLevel(LevelFatal)`, to set log level to only Fatal calls
1230
LevelFatal Level = iota
@@ -38,6 +56,38 @@ func SetLevel(level Level) {
3856
logLevel = level
3957
}
4058

59+
// ParseLevel takes as string and return the corresponding level. The strLevel argument
60+
// is case insensitive. The strLevel argument just needs to begin with one of the logging
61+
// level names.
62+
// Logging level names are TRACE, DEBUG, INFO, WARN, ERROR and FATAL
63+
//
64+
// e.g.: ParseLevel("debug") // LevelDebug
65+
// e.g.: ParseLevel("DEBUG") // LevelDebug
66+
// e.g.: ParseLevel("debugging") // LevelDebug
67+
// e.g.: ParseLevel("warn") // LevelWarn
68+
// e.g.: ParseLevel("warni") // LevelWarn
69+
// e.g.: ParseLevel("warnin") // LevelWarn
70+
// e.g.: ParseLevel("warning") // LevelWarn
71+
// e.g.: ParseLevel("WARNING") // LevelWarn
72+
//
73+
func ParseLevel(strLevel string) Level {
74+
str := strings.ToLower(strLevel)
75+
if strings.HasPrefix(str, "trace") {
76+
return LevelTrace
77+
} else if strings.HasPrefix(str, "debug") {
78+
return LevelDebug
79+
} else if strings.HasPrefix(str, "info") {
80+
return LevelInfo
81+
} else if strings.HasPrefix(str, "warn") {
82+
return LevelWarn
83+
} else if strings.HasPrefix(str, "error") {
84+
return LevelError
85+
} else if strings.HasPrefix(str, "fatal") {
86+
return LevelFatal
87+
}
88+
return LevelInfo
89+
}
90+
4191
// Fatal is equivalent to Print() followed by a call to os.Exit(1).
4292
func Fatal(v ...interface{}) {
4393
golog.Fatal(v...)

lib_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package log
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSetLevel(t *testing.T) {
8+
// given
9+
levels := []Level{LevelTrace, LevelDebug, LevelInfo, LevelWarn, LevelError, LevelFatal}
10+
11+
for _, level := range levels {
12+
// when
13+
SetLevel(level)
14+
15+
// then
16+
if logLevel != level {
17+
t.Errorf("Expected %s, bug got %s", level, logLevel)
18+
}
19+
}
20+
}
21+
22+
func TestParseLevelLowerCase(t *testing.T) {
23+
// given
24+
levels := map[string]Level{
25+
"trace": LevelTrace,
26+
"tracei": LevelTrace,
27+
"tracein": LevelTrace,
28+
"traceing": LevelTrace,
29+
"debug": LevelDebug,
30+
"debugg": LevelDebug,
31+
"debuggi": LevelDebug,
32+
"debuggin": LevelDebug,
33+
"debugging": LevelDebug,
34+
"info": LevelInfo,
35+
"warn": LevelWarn,
36+
"warni": LevelWarn,
37+
"warnin": LevelWarn,
38+
"warning": LevelWarn,
39+
"error": LevelError,
40+
"fatal": LevelFatal,
41+
}
42+
43+
for str, level := range levels {
44+
// when
45+
result := ParseLevel(str)
46+
47+
// then
48+
if result != level {
49+
t.Errorf("Expected %s, bug got %s", level, result)
50+
}
51+
}
52+
}
53+
54+
func TestParseLevelUpperCase(t *testing.T) {
55+
// given
56+
levels := map[string]Level{
57+
"TRACE": LevelTrace,
58+
"TRACEI": LevelTrace,
59+
"TRACEIN": LevelTrace,
60+
"TRACEING": LevelTrace,
61+
"DEBUG": LevelDebug,
62+
"DEBUGG": LevelDebug,
63+
"DEBUGGI": LevelDebug,
64+
"DEBUGGIN": LevelDebug,
65+
"DEBUGGING": LevelDebug,
66+
"INFO": LevelInfo,
67+
"WARN": LevelWarn,
68+
"WARNI": LevelWarn,
69+
"WARNIN": LevelWarn,
70+
"WARNING": LevelWarn,
71+
"ERROR": LevelError,
72+
"FATAL": LevelFatal,
73+
}
74+
75+
for str, level := range levels {
76+
// when
77+
result := ParseLevel(str)
78+
79+
// then
80+
if result != level {
81+
t.Errorf("Expected %s, bug got %s", level, result)
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)