-
Notifications
You must be signed in to change notification settings - Fork 14
/
appender_test.go
109 lines (79 loc) · 2.27 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
102
103
104
105
106
107
108
109
package logging
import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"os"
"path"
"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(), 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")
}
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")
}
func TestWriterAppender(t *testing.T) {
ClearAppenders()
SetDefaultLogLevel(DEBUG)
filepath := path.Join(os.TempDir(), "writerlogtest.txt")
f, _ := os.Create(filepath)
app := NewWriterAppender(f)
app.SetFormatter(GetFormatter(MINIMAL))
AddAppender(app)
app.SetLevel(INFO)
Info("one")
Debug("two")
WaitForIncoming()
PauseLogging() // data race if we don't pause
f.Close()
buf := bytes.NewBuffer(nil)
f, _ = os.Open(filepath)
io.Copy(buf, f)
f.Close()
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
}