-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
60 lines (53 loc) · 1.67 KB
/
logger_test.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
package flog
import (
"bytes"
"strings"
"testing"
"github.com/sirupsen/logrus"
)
func TestLogger(t *testing.T) {
tLog := NewLogger("tLog")
out := bytes.NewBuffer(nil)
assertOut := func(f func(outS string) bool) {
outS := out.String()
out.Reset()
t.Logf("out log: %s", outS)
if !f(outS) {
t.Fatal("check failed")
}
}
tLog.Setup(func(l *Logger) {
l.SetOutput(out)
l.SetLevel(logrus.ErrorLevel)
})
tLog.Error("log in global")
assertOut(func(outS string) bool { return strings.Contains(outS, "log in global") })
module1Log := tLog.GetLogger("module1")
// inherit log level from parent
module1Log.Info("log in module1")
assertOut(func(outS string) bool { return outS == "" })
module1Log.Error("log in module1")
assertOut(func(outS string) bool {
return strings.Contains(outS, "log in module1") && strings.Contains(outS, "logger_name=tLog.module1")
})
// change log level from parent
tLog.Setup(func(l *Logger) {
l.SetLevel(logrus.InfoLevel)
}, "module1")
module1Log.Info("log in module1")
assertOut(func(outS string) bool { return strings.Contains(outS, "log in module1") })
module1Log.Debug("log in module1")
assertOut(func(outS string) bool { return outS == "" })
// change own log level
module1Log.Setup(func(l *Logger) { l.SetLevel(logrus.DebugLevel) })
module1Log.Debug("log in module1")
assertOut(func(outS string) bool { return strings.Contains(outS, "log in module1") })
// change all loggers
tLog.Setup(func(l *Logger) {
l.SetLevel(logrus.PanicLevel)
}, AllLoggers)
tLog.Error("log in global")
assertOut(func(outS string) bool { return outS == "" })
module1Log.Debug("log in module1")
assertOut(func(outS string) bool { return outS == "" })
}