-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger_test.go
110 lines (94 loc) · 3.2 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
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
package log4g
import (
. "gopkg.in/check.v1"
"time"
)
type loggerSuite struct {
loggerName string
}
var _ = Suite(&loggerSuite{})
func (s *loggerSuite) TestApplyNewLevelToLoggers(c *C) {
rootLLS := &logLevelSetting{rootLoggerName, INFO}
loggers := make(map[string]*logger)
loggers["a"] = &logger{"a", rootLLS, nil, INFO}
loggers["a.b"] = &logger{"a.b", rootLLS, nil, INFO}
loggers["a.b.c"] = &logger{"a.b.c", rootLLS, nil, INFO}
loggers["a.b.c.d"] = &logger{"a.b.c.d", rootLLS, nil, INFO}
applyNewLevelToLoggers(&logLevelSetting{"a.b", DEBUG}, loggers)
c.Assert(loggers["a"].logLevel, Equals, INFO)
c.Assert(loggers["a.b"].logLevel, Equals, DEBUG)
c.Assert(loggers["a.b.c"].logLevel, Equals, DEBUG)
c.Assert(loggers["a.b.c.d"].logLevel, Equals, DEBUG)
applyNewLevelToLoggers(&logLevelSetting{"a.b.c", TRACE}, loggers)
applyNewLevelToLoggers(&logLevelSetting{"a.b", ERROR}, loggers)
c.Assert(loggers["a"].logLevel, Equals, INFO)
c.Assert(loggers["a.b"].logLevel, Equals, ERROR)
c.Assert(loggers["a.b.c"].logLevel, Equals, TRACE)
c.Assert(loggers["a.b.c.d"].logLevel, Equals, TRACE)
}
func (s *loggerSuite) TestLog(c *C) {
lctx := &logContext{eventsCh: make(chan *LogEvent, 1)}
l := &logger{"a", nil, lctx, INFO}
l.Log(INFO, "Hello")
go waitThenClose(500, lctx)
le, ok := <-lctx.eventsCh
c.Assert(ok, Equals, true)
c.Assert(le.Payload.(string), Equals, "Hello")
c.Assert(le.Level, Equals, INFO)
c.Assert(le.LoggerName, Equals, "a")
}
func (s *loggerSuite) TestLogDisabled(c *C) {
lctx := &logContext{eventsCh: make(chan *LogEvent, 1)}
l := &logger{"a", nil, lctx, INFO}
l.Log(DEBUG, "Hello")
go waitThenClose(50, lctx)
_, ok := <-lctx.eventsCh
c.Assert(ok, Equals, false)
}
func (s *loggerSuite) TestLogf(c *C) {
lctx := &logContext{eventsCh: make(chan *LogEvent, 2)}
l := &logger{"a", nil, lctx, INFO}
l.Logf(INFO, "Hello %s")
l.Logf(INFO, "Hello %s", "World!")
go waitThenClose(500, lctx)
le, ok := <-lctx.eventsCh
c.Assert(ok, Equals, true)
c.Assert(le.Payload.(string), Equals, "Hello %s")
le, ok = <-lctx.eventsCh
c.Assert(ok, Equals, true)
c.Assert(le.Payload.(string), Equals, "Hello World!")
}
func (s *loggerSuite) TestLogp(c *C) {
lctx := &logContext{eventsCh: make(chan *LogEvent, 1)}
l := &logger{"a", nil, lctx, INFO}
l.Logp(INFO, lctx)
go waitThenClose(500, lctx)
le, ok := <-lctx.eventsCh
c.Assert(ok, Equals, true)
c.Assert(le.Payload, Equals, lctx)
}
func (s *loggerSuite) TestMessages(c *C) {
lctx := &logContext{eventsCh: make(chan *LogEvent, 10)}
l := &logger{"a", nil, lctx, TRACE}
l.Info(INFO)
l.Warn(WARN)
l.Debug(DEBUG)
l.Trace(TRACE)
l.Error(ERROR)
l.Fatal(FATAL)
go waitThenClose(500, lctx)
c.Assert((<-lctx.eventsCh).Level, Equals, INFO)
c.Assert((<-lctx.eventsCh).Level, Equals, WARN)
c.Assert((<-lctx.eventsCh).Level, Equals, DEBUG)
c.Assert((<-lctx.eventsCh).Level, Equals, TRACE)
c.Assert((<-lctx.eventsCh).Level, Equals, ERROR)
c.Assert((<-lctx.eventsCh).Level, Equals, FATAL)
}
func (s *loggerSuite) TestLevelsOrder(c *C) {
ok := FATAL < ERROR && ERROR < WARN && WARN < INFO && INFO < DEBUG && DEBUG < TRACE
c.Assert(ok, Equals, true)
}
func waitThenClose(mSec time.Duration, lctx *logContext) {
time.Sleep(time.Millisecond * mSec)
close(lctx.eventsCh)
}