-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathulog_test.go
162 lines (143 loc) · 4.08 KB
/
ulog_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
package ulog
import (
"bufio"
"os"
"reflect"
"runtime"
"strings"
"testing"
"golang.org/x/net/context"
)
type testAdapter struct {
lastEntry Entry
lastCallerFile string
lastCallerLine int
}
func (c *testAdapter) Handle(e Entry) {
c.lastEntry = e
_, file, line, ok := runtime.Caller(c.lastEntry.CallDepth())
if !ok {
panic("could not get caller")
}
c.lastCallerFile = file
c.lastCallerLine = line
}
func TestEntryLevel(t *testing.T) {
var c testAdapter
ctx := context.Background()
logger := Logger(ctx).WithAdapter(&c)
testSets := []struct {
levelFunc func(args ...interface{})
levelfFunc func(format string, args ...interface{})
wantLevel Level
}{{
levelFunc: logger.Debug,
levelfFunc: logger.Debugf,
wantLevel: DebugLevel,
}, {
levelFunc: logger.Info,
levelfFunc: logger.Infof,
wantLevel: InfoLevel,
}, {
levelFunc: logger.Warn,
levelfFunc: logger.Warnf,
wantLevel: WarnLevel,
}, {
levelFunc: logger.Error,
levelfFunc: logger.Errorf,
wantLevel: ErrorLevel,
}}
const testMessage = "test message"
for _, ts := range testSets {
ts.levelFunc("test", "message")
if c.lastEntry.Level != ts.wantLevel {
t.Errorf("invalid level given. want %s actual %s", ts.wantLevel, c.lastEntry.Level)
}
if c.lastEntry.Message != testMessage {
t.Errorf("invalid message. want %s actual %s", testMessage, c.lastEntry.Message)
}
ts.levelfFunc("test %s", "message")
if c.lastEntry.Level != ts.wantLevel {
t.Errorf("invalid level given. want %s actual %s", ts.wantLevel, c.lastEntry.Level)
}
if c.lastEntry.Message != testMessage {
t.Errorf("invalid message. want %s actual %s", testMessage, c.lastEntry.Message)
}
}
}
func infoRecursive(ctx context.Context, depth int, message string) {
if depth == 1 {
Logger(ctx).Info(message)
return
}
infoRecursive(ctx, depth-1, message)
}
func TestCallDepth(t *testing.T) {
var c testAdapter
ctx := context.Background()
logger := Logger(ctx).WithAdapter(&c)
checkDepthAndMarker := func(t *testing.T, wantDepth int, marker string) {
if wantDepth != c.lastEntry.CallDepth() {
t.Errorf("invalid callDepth want %d, actual %d", wantDepth, c.lastEntry.CallDepth())
}
f, err := os.Open(c.lastCallerFile)
if err != nil {
t.Fatal(err)
}
r := bufio.NewReader(f)
var aline string
// read lines
for i := 0; i < c.lastCallerLine; i++ {
l, _, err := r.ReadLine()
if err != nil {
t.Fatal("error while reading file", err)
}
aline = string(l)
}
if !strings.Contains(aline, marker) {
t.Errorf("%s L%d : %s doesn't contains marker %s", c.lastCallerFile, c.lastCallerLine, aline, marker)
}
}
logger.Info("mark normal depth")
checkDepthAndMarker(t, defaultCallDepth, "mark normal depth")
i := logger.WithCallDepth(1).Info
i("mark closure depth")
checkDepthAndMarker(t, defaultCallDepth+1, "mark closure depth")
infoRecursive(logger.WithCallDepth(1), 1, "mark depth 1")
checkDepthAndMarker(t, defaultCallDepth+1, "mark depth 1")
infoRecursive(logger.WithCallDepth(2), 2, "mark depth 2")
checkDepthAndMarker(t, defaultCallDepth+2, "mark depth 2")
infoRecursive(logger.WithCallDepth(5).WithCallDepth(7), 12, "mark depth 5+7")
checkDepthAndMarker(t, defaultCallDepth+12, "mark depth 5+7")
}
func TestFields(t *testing.T) {
var c testAdapter
ctx := context.Background()
logger := Logger(ctx).WithAdapter(&c)
testSet := []struct {
setup func(l LoggerContext) LoggerContext
want []Field
}{{
setup: func(l LoggerContext) LoggerContext {
return l.WithField("key1", 123)
},
want: []Field{{"key1", 123}},
}, {
setup: func(l LoggerContext) LoggerContext {
return l.WithField("key1", 123).WithField("key2", 456)
},
want: []Field{{"key1", 123}, {"key2", 456}},
}, {
setup: func(l LoggerContext) LoggerContext {
return l.WithField("key1", 123).WithField("key2", 456).WithField("key1", 789)
},
want: []Field{{"key1", 789}, {"key2", 456}},
}}
for _, ts := range testSet {
l := ts.setup(logger)
l.Info("message")
if actual := c.lastEntry.Fields(); !reflect.DeepEqual(actual, ts.want) {
t.Errorf("invalid fields want %v, actual %v", ts.want, actual)
}
}
}