-
Notifications
You must be signed in to change notification settings - Fork 5
/
caller_hook_test.go
178 lines (160 loc) · 4.38 KB
/
caller_hook_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package caller
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"log"
"sync"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
type logJson struct {
Level string
Msg string
Time time.Time
Src string
}
func TestCallerHook_ExportedInfo(t *testing.T) {
var buffer bytes.Buffer
// Caller hook
logrus.AddHook(NewHook(&CallerHookOptions{
Field: "src",
Flags: log.Lshortfile,
}))
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetOutput(&buffer)
testCallerHookExportedInfo(t, &buffer, "Testing")
testCallerHookExportedInfo(t, &buffer, "Testing 2")
testCallerHookExportedInfo(t, &buffer, "Testing 3")
// Test in goroutines
count := 50
var wg sync.WaitGroup
for i := 0; i < count; i++ {
wg.Add(1)
go func(t *testing.T, wg *sync.WaitGroup, i int) {
defer wg.Done()
logrus.Info(fmt.Sprintf("Testing for race conditions %d", i))
}(t, &wg, i)
}
// Wait for goroutines
wg.Wait()
}
func testCallerHookExportedInfo(t *testing.T, buffer *bytes.Buffer, msg string) {
var j logJson
logrus.Info(msg)
scanner := bufio.NewScanner(buffer)
scanner.Scan()
line := scanner.Text()
assert.NoError(t, scanner.Err(), "Scanner error")
json.Unmarshal([]byte(line), &j)
assert.Equal(t, "caller_test.go:54", j.Src, "%v", j)
assert.Equal(t, msg, j.Msg, "%v", j)
assert.Equal(t, "info", j.Level, "%v", j)
}
func TestCallerHook_ExportedEntryInfo(t *testing.T) {
var buffer bytes.Buffer
// Caller hook
logrus.AddHook(NewHook(&CallerHookOptions{
Field: "src",
Flags: log.Lshortfile,
}))
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetOutput(&buffer)
testCallerHookExportedEntryInfo(t, &buffer, "Testing")
testCallerHookExportedEntryInfo(t, &buffer, "Testing 2")
testCallerHookExportedEntryInfo(t, &buffer, "Testing 3")
// Test in goroutines
count := 50
var wg sync.WaitGroup
for i := 0; i < count; i++ {
wg.Add(1)
go func(t *testing.T, wg *sync.WaitGroup, i int) {
defer wg.Done()
logrus.Info(fmt.Sprintf("Testing for race conditions %d", i))
}(t, &wg, i)
}
// Wait for goroutines
wg.Wait()
}
func testCallerHookExportedEntryInfo(t *testing.T, buffer *bytes.Buffer, msg string) {
var j logJson
logrus.WithField("testcaller", "testcaller value").Info(msg)
scanner := bufio.NewScanner(buffer)
scanner.Scan()
line := scanner.Text()
assert.NoError(t, scanner.Err(), "Scanner error")
json.Unmarshal([]byte(line), &j)
assert.Equal(t, "caller_test.go:95", j.Src, "%v", j)
assert.Equal(t, msg, j.Msg, "%v", j)
assert.Equal(t, "info", j.Level, "%v", j)
}
func TestCallerHook_NewLoggerInfo(t *testing.T) {
var buffer bytes.Buffer
logr := logrus.New()
// Caller hook
logr.Hooks.Add(NewHook(&CallerHookOptions{
Field: "src",
Flags: log.Lshortfile,
}))
logr.Formatter = &logrus.JSONFormatter{}
logr.Out = &buffer
testCallerHookInfo(t, logr, "Testing")
testCallerHookInfo(t, logr, "Testing 2")
testCallerHookInfo(t, logr, "Testing 3")
// Test in goroutines
count := 50
var wg sync.WaitGroup
for i := 0; i < count; i++ {
wg.Add(1)
go func(t *testing.T, wg *sync.WaitGroup, logr *logrus.Logger, i int) {
defer wg.Done()
logr.Info(fmt.Sprintf("Testing for race conditions %d", i))
}(t, &wg, logr, i)
}
// Wait for goroutines
wg.Wait()
}
func TestCallerHook_LoggerInfo(t *testing.T) {
var buffer bytes.Buffer
logr := &logrus.Logger{
Out: &buffer,
Formatter: new(logrus.JSONFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}
// Caller hook
logr.Hooks.Add(NewHook(&CallerHookOptions{
Field: "src",
Flags: log.Lshortfile,
}))
testCallerHookInfo(t, logr, "Testing")
testCallerHookInfo(t, logr, "Testing 2")
testCallerHookInfo(t, logr, "Testing 3")
// Test in goroutines
count := 50
var wg sync.WaitGroup
for i := 0; i < count; i++ {
wg.Add(1)
go func(t *testing.T, wg *sync.WaitGroup, logr *logrus.Logger, i int) {
defer wg.Done()
logr.Info(fmt.Sprintf("Testing for race conditions %d", i))
}(t, &wg, logr, i)
}
// Wait for goroutines
wg.Wait()
}
func testCallerHookInfo(t *testing.T, logr *logrus.Logger, msg string) {
var j logJson
logr.Info(msg)
scanner := bufio.NewScanner(logr.Out.(*bytes.Buffer))
scanner.Scan()
line := scanner.Text()
assert.NoError(t, scanner.Err(), "Scanner error")
json.Unmarshal([]byte(line), &j)
assert.Equal(t, "caller_test.go:169", j.Src, "%v", j)
assert.Equal(t, msg, j.Msg, "%v", j)
assert.Equal(t, "info", j.Level, "%v", j)
}