-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_test.go
158 lines (129 loc) · 4 KB
/
logging_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package logging
import (
"io/ioutil"
"os"
"strings"
"sync"
"testing"
"github.com/nsqio/go-nsq"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
type mockLogFile struct {
out *os.File
in *os.File
}
func newMockLogFile(t *testing.T) mockLogFile {
r, w, err := os.Pipe()
assert.NoError(t, err)
return mockLogFile{r, w}
}
func (f mockLogFile) getLogFileContent(t *testing.T) string {
err := f.in.Close()
assert.NoError(t, err)
out, err := ioutil.ReadAll(f.out)
assert.NoError(t, err)
return string(out)
}
var testGetLogrusLogLevelData = []struct {
in string
out logrus.Level
}{
{"", logrus.InfoLevel},
{"ashtashtnn212rn2h1h12hxxz", logrus.InfoLevel},
{"ERROR", logrus.ErrorLevel},
{"WARNING", logrus.WarnLevel},
{"INFO", logrus.InfoLevel},
{"DEBUG", logrus.DebugLevel},
}
func TestMain(m *testing.M) {
Init(LoggingConfig{})
os.Exit(m.Run())
}
func TestGetLogrusLogLevel(t *testing.T) {
for _, test := range testGetLogrusLogLevelData {
if getLogrusLogLevel(test.in) != test.out {
t.Errorf("failed to get default log level of [%s]", test.in)
}
}
}
func TestNew(t *testing.T) {
logFile := newMockLogFile(t)
Log.Out = logFile.in
Log.Level = logrus.InfoLevel
Log.Debug("test debug")
Log.Info("test info")
Log.Warning("test warning")
Log.Error("test error")
_log := logFile.getLogFileContent(t)
if strings.Contains(_log, "test debug") {
t.Error("log should not contain debug since the level is ignored")
}
if !strings.Contains(_log, "test info") {
t.Error("failed to log info")
}
if !strings.Contains(_log, "test warning") {
t.Error("failed to log warning")
}
if !strings.Contains(_log, "test error") {
t.Error("failed to log error")
}
}
func TestConcurrentUseOfEntry(t *testing.T) {
logFile := newMockLogFile(t)
Log.Logger.Out = logFile.in
Log.Logger.Level = logrus.DebugLevel
entry := Log.NewEntry()
userEntry := entry.WithUser(10)
wg := sync.WaitGroup{}
wg.Add(5)
go func() { defer wg.Done(); userEntry.WithChannel("asdf").Info("test1") }()
go func() { defer wg.Done(); entry.WithChannel("asdgegege").Info("test2") }()
go func() { defer wg.Done(); entry.WithChannel("asdgegege").Debug("test3") }()
go func() { defer wg.Done(); entry.WithChannel("asdgegege").Error("test4") }()
go func() { defer wg.Done(); Log.Info("test5") }()
wg.Wait()
logFileContent := logFile.getLogFileContent(t)
assert.Contains(t, logFileContent, "test1")
assert.Contains(t, logFileContent, "test2")
assert.Contains(t, logFileContent, "test3")
assert.Contains(t, logFileContent, "test4")
assert.Contains(t, logFileContent, "test5")
}
func TestWithStringFieldIgnoreEmpty(t *testing.T) {
Log.Logger.Level = logrus.DebugLevel
t.Run("no field if string empty", func(t *testing.T) {
logFile := newMockLogFile(t)
Log.Logger.Out = logFile.in
Log.NewEntry().WithStringFieldIgnoreEmpty("nsq", "").Info("crap")
logFileContent := logFile.getLogFileContent(t)
assert.NotContains(t, logFileContent, "nsq", "nsq should not be in the log since value is empty")
})
t.Run("field present if string is non empty", func(t *testing.T) {
logFile := newMockLogFile(t)
Log.Logger.Out = logFile.in
Log.NewEntry().WithStringFieldIgnoreEmpty("nsq", "asdf").Info("crap")
logFileContent := logFile.getLogFileContent(t)
assert.Contains(t, logFileContent, "nsq", "nsq should be in the log since value is non empty")
})
}
func TestNSQLogger(t *testing.T) {
Log.Level = logrus.DebugLevel
_, level := Log.NSQLogger()
assert.EqualValues(t, level, nsq.LogLevelDebug)
Log.Level = logrus.InfoLevel
_, level = Log.NSQLogger()
assert.EqualValues(t, level, nsq.LogLevelInfo)
Log.Level = logrus.WarnLevel
_, level = Log.NSQLogger()
assert.EqualValues(t, level, nsq.LogLevelWarning)
Log.Level = logrus.ErrorLevel
_, level = Log.NSQLogger()
assert.EqualValues(t, level, nsq.LogLevelError)
Log.Level = logrus.FatalLevel
_, level = Log.NSQLogger()
assert.EqualValues(t, level, nsq.LogLevelError)
Log.Level = logrus.PanicLevel
_, level = Log.NSQLogger()
assert.EqualValues(t, level, nsq.LogLevelError)
}