Skip to content

Commit 1d899ad

Browse files
committed
adding new Is* methods that can be used to check log level to ensure log args aren't evaluated in certain conditions
1 parent a08686f commit 1d899ad

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed

consolelog.go

+28
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,31 @@ func (l *ConsoleLogger) Print(lvl int, v ...interface{}) {
110110
func (l *ConsoleLogger) Printf(lvl int, format string, v ...interface{}) {
111111
l.output(&Message{lvl, fmt.Sprintf(format, v...), time.Now()})
112112
}
113+
114+
func (l *ConsoleLogger) GetLevel() int {
115+
return l.outLevel
116+
}
117+
118+
func (l *ConsoleLogger) IsFatal() bool {
119+
return l.outLevel <= FATAL
120+
}
121+
122+
func (l *ConsoleLogger) IsError() bool {
123+
return l.outLevel <= ERROR
124+
}
125+
126+
func (l *ConsoleLogger) IsWarn() bool {
127+
return l.outLevel <= WARN
128+
}
129+
130+
func (l *ConsoleLogger) IsInfo() bool {
131+
return l.outLevel <= INFO
132+
}
133+
134+
func (l *ConsoleLogger) IsDebug() bool {
135+
return l.outLevel <= DEBUG
136+
}
137+
138+
func (l *ConsoleLogger) IsTrace() bool {
139+
return l.outLevel <= TRACE
140+
}

filelog.go

+28
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,31 @@ func (l *FileLogger) Print(lvl int, v ...interface{}) {
332332
func (l *FileLogger) Printf(lvl int, format string, v ...interface{}) {
333333
l.output(&Message{lvl, fmt.Sprintf(format, v...), time.Now()})
334334
}
335+
336+
func (l *FileLogger) GetLevel() int {
337+
return l.outLevel
338+
}
339+
340+
func (l *FileLogger) IsFatal() bool {
341+
return l.outLevel <= FATAL
342+
}
343+
344+
func (l *FileLogger) IsError() bool {
345+
return l.outLevel <= ERROR
346+
}
347+
348+
func (l *FileLogger) IsWarn() bool {
349+
return l.outLevel <= WARN
350+
}
351+
352+
func (l *FileLogger) IsInfo() bool {
353+
return l.outLevel <= INFO
354+
}
355+
356+
func (l *FileLogger) IsDebug() bool {
357+
return l.outLevel <= DEBUG
358+
}
359+
360+
func (l *FileLogger) IsTrace() bool {
361+
return l.outLevel <= TRACE
362+
}

lumber.go

+37
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ type Logger interface {
3333
Info(string, ...interface{})
3434
Debug(string, ...interface{})
3535
Trace(string, ...interface{})
36+
37+
IsFatal() bool
38+
IsError() bool
39+
IsWarn() bool
40+
IsInfo() bool
41+
IsDebug() bool
42+
IsTrace() bool
43+
GetLevel() int
44+
3645
Print(int, ...interface{})
3746
Printf(int, string, ...interface{})
3847
Level(int)
@@ -108,3 +117,31 @@ func Print(lvl int, v ...interface{}) {
108117
func Printf(lvl int, format string, v ...interface{}) {
109118
stdLog.output(&Message{lvl, fmt.Sprintf(format, v...), time.Now()})
110119
}
120+
121+
func GetLevel() int {
122+
return stdLog.GetLevel()
123+
}
124+
125+
func IsFatal() bool {
126+
return stdLog.IsFatal()
127+
}
128+
129+
func IsError() bool {
130+
return stdLog.IsError()
131+
}
132+
133+
func IsWarn() bool {
134+
return stdLog.IsWarn()
135+
}
136+
137+
func IsInfo() bool {
138+
return stdLog.IsInfo()
139+
}
140+
141+
func IsDebug() bool {
142+
return stdLog.IsDebug()
143+
}
144+
145+
func IsTrace() bool {
146+
return stdLog.IsTrace()
147+
}

lumber_test.go

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package lumber
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIsStar(t *testing.T) {
8+
9+
log := NewConsoleLogger(FATAL)
10+
if !log.IsFatal() {
11+
t.Fatal("Fatal should be logged")
12+
}
13+
if log.IsError() {
14+
t.Fatal("Error should not be logged")
15+
}
16+
if log.IsWarn() {
17+
t.Fatal("Warn should not be logged")
18+
}
19+
if log.IsInfo() {
20+
t.Fatal("Info should not be logged")
21+
}
22+
if log.IsDebug() {
23+
t.Fatal("Debug should not be logged")
24+
}
25+
if log.IsTrace() {
26+
t.Fatal("Trace should not be logged")
27+
28+
}
29+
30+
log.Level(ERROR)
31+
if !log.IsFatal() {
32+
t.Fatal("Fatal should be logged")
33+
}
34+
if !log.IsError() {
35+
t.Fatal("Error should be logged")
36+
}
37+
if log.IsWarn() {
38+
t.Fatal("Warn should not be logged")
39+
}
40+
if log.IsInfo() {
41+
t.Fatal("Info should not be logged")
42+
}
43+
if log.IsDebug() {
44+
t.Fatal("Debug should not be logged")
45+
}
46+
if log.IsTrace() {
47+
t.Fatal("Trace should not be logged")
48+
49+
}
50+
51+
log.Level(WARN)
52+
if !log.IsFatal() {
53+
t.Fatal("Fatal should be logged")
54+
}
55+
if !log.IsError() {
56+
t.Fatal("Error should be logged")
57+
}
58+
if !log.IsWarn() {
59+
t.Fatal("Warn should be logged")
60+
}
61+
if log.IsInfo() {
62+
t.Fatal("Info should not be logged")
63+
}
64+
if log.IsDebug() {
65+
t.Fatal("Debug should not be logged")
66+
}
67+
if log.IsTrace() {
68+
t.Fatal("Trace should not be logged")
69+
}
70+
71+
log.Level(INFO)
72+
if !log.IsFatal() {
73+
t.Fatal("Fatal should be logged")
74+
}
75+
if !log.IsError() {
76+
t.Fatal("Error should be logged")
77+
}
78+
if !log.IsWarn() {
79+
t.Fatal("Warn should be logged")
80+
}
81+
if !log.IsInfo() {
82+
t.Fatal("Info should be logged")
83+
}
84+
if log.IsDebug() {
85+
t.Fatal("Debug should not be logged")
86+
}
87+
if log.IsTrace() {
88+
t.Fatal("Trace should not be logged")
89+
}
90+
91+
log.Level(DEBUG)
92+
if !log.IsFatal() {
93+
t.Fatal("Fatal should be logged")
94+
}
95+
if !log.IsError() {
96+
t.Fatal("Error should be logged")
97+
}
98+
if !log.IsWarn() {
99+
t.Fatal("Warn should be logged")
100+
}
101+
if !log.IsInfo() {
102+
t.Fatal("Info should be logged")
103+
}
104+
if !log.IsDebug() {
105+
t.Fatal("Debug should be logged")
106+
}
107+
if log.IsTrace() {
108+
t.Fatal("Trace should not be logged")
109+
}
110+
111+
log.Level(TRACE)
112+
if !log.IsFatal() {
113+
t.Fatal("Fatal should be logged")
114+
}
115+
if !log.IsError() {
116+
t.Fatal("Error should be logged")
117+
}
118+
if !log.IsWarn() {
119+
t.Fatal("Warn should be logged")
120+
}
121+
if !log.IsInfo() {
122+
t.Fatal("Info should be logged")
123+
}
124+
if !log.IsDebug() {
125+
t.Fatal("Debug should be logged")
126+
}
127+
if !log.IsTrace() {
128+
t.Fatal("Trace should be logged")
129+
}
130+
}
131+
132+
func TestMultiIS(t *testing.T) {
133+
log := NewMultiLogger()
134+
log.AddLoggers(NewConsoleLogger(WARN))
135+
log.AddLoggers(NewConsoleLogger(INFO))
136+
137+
if log.IsTrace() {
138+
t.Fatal("Logger should return trace")
139+
}
140+
if !log.IsInfo() {
141+
t.Fatal("Logger should return info")
142+
}
143+
if !log.IsWarn() {
144+
t.Fatal("Logger should return warn")
145+
}
146+
if !log.IsError() {
147+
t.Fatal("Logger should return error")
148+
}
149+
if !log.IsFatal() {
150+
t.Fatal("Logger should return fatal")
151+
}
152+
}

multilog.go

+34
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,37 @@ func (p *MultiLogger) Print(lvl int, v ...interface{}) {
9494
func (p *MultiLogger) Printf(lvl int, format string, v ...interface{}) {
9595
p.output(&Message{lvl, fmt.Sprintf(format, v...), time.Now()})
9696
}
97+
98+
func (p *MultiLogger) GetLevel() int {
99+
level := FATAL
100+
for _, logger := range p.loggers {
101+
if logger.GetLevel() <= level {
102+
level = logger.GetLevel()
103+
}
104+
}
105+
return level
106+
}
107+
108+
func (p *MultiLogger) IsFatal() bool {
109+
return p.GetLevel() <= FATAL
110+
}
111+
112+
func (p *MultiLogger) IsError() bool {
113+
return p.GetLevel() <= ERROR
114+
}
115+
116+
func (p *MultiLogger) IsWarn() bool {
117+
return p.GetLevel() <= WARN
118+
}
119+
120+
func (p *MultiLogger) IsInfo() bool {
121+
return p.GetLevel() <= INFO
122+
}
123+
124+
func (p *MultiLogger) IsDebug() bool {
125+
return p.GetLevel() <= DEBUG
126+
}
127+
128+
func (p *MultiLogger) IsTrace() bool {
129+
return p.GetLevel() <= TRACE
130+
}

0 commit comments

Comments
 (0)