forked from glitchdotcom/logging
-
Notifications
You must be signed in to change notification settings - Fork 4
/
appender_test.go
101 lines (72 loc) · 2.14 KB
/
appender_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
package logging
import (
"bytes"
"github.com/stretchr/testify/assert"
"testing"
)
func TestAppenderLevel(t *testing.T) {
logger, memory := setup()
logger.SetLogLevel(DEBUG)
memory.SetLevel(WARN)
secondAppender := NewMemoryAppender()
secondAppender.SetLevel(DEBUG)
AddAppender(secondAppender)
logger.Error("error")
logger.Info("info")
WaitForIncoming()
assert.Equal(t, len(memory.GetLoggedMessages()), 1, "Appender should filter messages.")
assert.Equal(t, len(secondAppender.GetLoggedMessages()), 2, "Appender should work separately.")
}
func TestNullAppender(t *testing.T) {
ClearAppenders()
app := NewNullAppender()
AddAppender(app)
SetDefaultLogLevel(INFO)
Info("one")
Debug("two")
WaitForIncoming()
assert.Equal(t, app.Count(), int64(1), "Null appender should check levels appropriately")
}
func TestAppenderCheckLevel(t *testing.T) { //not sure how to test std err without subproc so this is for coverage
ClearAppenders()
app := NewStdErrAppender()
AddAppender(app)
app.SetLevel(INFO)
assert.True(t, app.CheckLevel(ERROR), "error is allowed")
assert.True(t, app.CheckLevel(INFO), "info is allowed")
assert.False(t, app.CheckLevel(DEBUG), "debug is not allowed")
}
func TestStdErrAppender(t *testing.T) { //not sure how to test std err without subproc so this is for coverage
ClearAppenders()
app := NewStdErrAppender()
AddAppender(app)
SetDefaultLogLevel(INFO)
Info("one")
Debug("two")
WaitForIncoming()
}
func TestStdOutAppender(t *testing.T) { //not sure how to test std out without subproc so this is for coverage
ClearAppenders()
app := NewStdOutAppender()
AddAppender(app)
SetDefaultLogLevel(INFO)
Info("one")
Debug("two")
WaitForIncoming()
}
func TestWriterAppender(t *testing.T) {
ClearAppenders()
SetDefaultLogLevel(DEBUG)
var buf bytes.Buffer
app := NewWriterAppender(&buf)
app.SetFormatter(GetFormatter(MINIMAL))
AddAppender(app)
app.SetLevel(INFO)
Info("one")
Debug("two")
WaitForIncoming()
PauseLogging() // data race if we don't pause
s := string(buf.Bytes())
assert.Equal(t, s, "one\n", "File should contain a single entry for writer appender")
RestartLogging() //don't leave logging off
}