-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathentry_test.go
190 lines (160 loc) · 4.89 KB
/
entry_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
179
180
181
182
183
184
185
186
187
188
189
190
package nxlog4go
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"testing"
"time"
"github.com/ccpaging/nxlog4go/driver"
"github.com/ccpaging/nxlog4go/patt"
)
func TestEntryWithFields(t *testing.T) {
buf := new(bytes.Buffer)
l := NewLogger(FINEST).SetOptions("format", "%L %S %M.%F").SetOutput(buf)
e := NewEntry(l).With()
fields, index := e.rec.Fields()
if got, want := len(fields), 0; got != want {
t.Errorf(" got %d", got)
t.Errorf(" want %d", want)
} else if got = len(index); got != want {
t.Errorf(" got index %d", got)
t.Errorf(" want index %d", want)
}
e = NewEntry(l).With("k1", "v1", "k2", "v2")
fields, index = e.rec.Fields()
if got, want := len(fields), 2; got != want {
t.Errorf(" got %d", got)
t.Errorf(" want %d", want)
} else if got = len(index); got != want {
t.Errorf(" got index %d", got)
t.Errorf(" want index %d", want)
}
}
func TestEntryFormatKeyValue(t *testing.T) {
errBoom := fmt.Errorf("boom time")
buf := new(bytes.Buffer)
l := NewLogger(INFO).SetOutput(buf).SetOptions("format", "%L %S %M.%F", "fieldsEncoder", "quote")
e := NewEntry(l)
e.With("err", errBoom, "k2", "v2", "k1", "v1").Log(1, ERROR, "kaboom")
want := "EROR nxlog4go/entry_test.go kaboom. err=\"boom time\" k2=\"v2\" k1=\"v1\"\n"
if got := buf.String(); got != want {
t.Errorf(" got %q", got)
t.Errorf(" want %q", want)
}
}
func TestEntryFieldsJson(t *testing.T) {
buf := new(bytes.Buffer)
l := NewLogger(INFO).SetOutput(buf).SetLayout(patt.NewJSONLayout("callerEncoder", "fullpath"))
e := NewEntry(l).SetPrefix("test").With("source", "TestEntryFieldsJson")
now := time.Now()
errBoom := fmt.Errorf("boom")
e.Log(1, ERROR, "kaboom", "error", errBoom)
b := buf.Bytes()
um := &struct {
driver.Recorder
Fields map[string]interface{}
}{}
if err := json.Unmarshal(b, &um); err != nil {
t.Errorf(" got %q", b)
t.Errorf(" error %v", err)
}
if got, want := um.Level, ERROR; got != want {
t.Errorf(" got %v", got)
t.Errorf(" want %v", want)
}
if got, want := um.Created, now; got.Unix() < want.Unix() {
t.Errorf(" got %v", got)
t.Errorf(" want %v", want)
}
if got, want := um.Prefix, "test"; got != want {
t.Errorf(" got %v", got)
t.Errorf(" want %v", want)
}
if got, want := um.Source, "entry_test.go"; strings.HasSuffix(got, want) == false {
t.Errorf(" got %v", got)
t.Errorf(" want %v", want)
}
if got, want := um.Line, 61; got != want {
t.Errorf(" got %v", got)
t.Errorf(" want %v", want)
}
if got, want := um.Message, "kaboom"; got != want {
t.Errorf(" got %v", got)
t.Errorf(" want %v", want)
}
if len(um.Fields) < 1 {
t.Errorf(" got %s", b)
t.Errorf("Missing Fields %v", um.Fields)
}
if want, ok := um.Fields["source"]; !ok {
t.Errorf("Missing want field %q", "source")
} else if wantStr, ok := want.(string); !ok {
t.Errorf("Missing want type [%T]", want)
} else if got, ok := um.Fields["source"]; !ok {
t.Errorf("Missing got field %q", "source")
} else if gotStr, ok := got.(string); !ok {
t.Errorf("Missing got type [%T]", got)
} else if gotStr != wantStr {
t.Errorf(" got %q", gotStr)
t.Errorf(" want %q", wantStr)
}
}
func TestEntryWithValues(t *testing.T) {
buf := new(bytes.Buffer)
l := NewLogger(FINEST).SetOptions("fields", false, "format", "%L %S %M.%F").SetOutput(buf)
e := NewEntry(l).With()
want := 0
if got := len(e.rec.Values); got != want {
t.Errorf(" got %d", got)
t.Errorf(" want %d", want)
}
e = NewEntry(l).With("v1", "v2", "k3")
want = 3
if got := len(e.rec.Values); got != want {
t.Errorf(" got %d", got)
t.Errorf(" want %d", want)
}
}
func TestEntryValuesJson(t *testing.T) {
buf := new(bytes.Buffer)
l := NewLogger(INFO).SetOptions("fields", false).SetOutput(buf).SetLayout(patt.NewJSONValueLayout("callerEncoder", "fullpath"))
e := NewEntry(l).SetPrefix("test").With("source", "TestEntryValuesJson")
errBoom := fmt.Errorf("boom")
e.Log(1, ERROR, "kaboom", "error", errBoom)
r := e.rec
b := buf.Bytes()
um := &driver.Recorder{}
if err := json.Unmarshal(b, &um); err != nil {
t.Errorf(" got %q", b)
t.Errorf(" error %v", err)
}
if got, want := um.Prefix, "test"; got != want {
t.Errorf(" got %v", got)
t.Errorf(" want %v", want)
}
if got, want := um.Message, "kaboom"; got != want {
t.Errorf(" got %v", got)
t.Errorf(" want %v", want)
}
if len(um.Values) != 4 {
t.Errorf(" got %s", b)
t.Errorf("Missing Values %v", um.Values)
}
v, v1 := r.Values[0], um.Values[0]
if wantStr, ok := v.(string); !ok {
t.Errorf("Missing want type [%T]", v)
} else if gotStr, ok := v1.(string); !ok {
t.Errorf("Missing got type [%T]", v1)
} else if gotStr != wantStr {
t.Errorf(" got %q", gotStr)
t.Errorf(" want %q", wantStr)
}
v = um.Values[3]
if gotStr, ok := v.(string); !ok {
t.Errorf("Missing got type [%T]", v)
} else if wantStr := "boom"; gotStr != wantStr {
t.Errorf(" got %q", gotStr)
t.Errorf(" want %q", wantStr)
}
}