forked from visionmedia/go-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter_test.go
112 lines (88 loc) · 2.86 KB
/
formatter_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
package debug
import (
"bytes"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTextFormatterFieldSpawnMultipleEnabledText(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
SetWriter(buf)
SetFormatterString("text")
Enable("foo*,bar*")
foo := Debug("foo").Spawn("child").Spawn("grandChild").WithFields(
Fields{"field": 1, "field2": "two", "field3": "multiple strings"})
foo.Log("foo")
foo.Log(func() string { return "foo lazy" })
bar := Debug("bar").Spawn("child").Spawn("grandChild")
bar.Log("bar")
bar.Log(func() string { return "bar lazy" })
if buf.Len() == 0 {
t.Fatalf("buffer should have output")
}
str := buf.String()
assert.Contains(t, str, "foo:child:grandChild")
assert.Contains(t, str, "field=1")
assert.Contains(t, str, "field2=two")
assert.Contains(t, str, `field3="multiple strings"`)
assert.Contains(t, str, "foo")
assert.Contains(t, str, "foo lazy")
assert.Contains(t, str, "bar:child:grandChild")
assert.Contains(t, str, "bar")
assert.Contains(t, str, "bar lazy")
}
func TestTextFormatterFieldSpawnMultipleEnabledJSON(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
SetWriter(buf)
SetFormatterString("json")
Enable("foo*,bar*")
foo := Debug("foo").Spawn("child").Spawn("grandChild").WithFields(
Fields{"field": 1, "field2": "two", "field3": "multiple strings"})
foo.Log("foo")
foo.Log(func() string { return "foo lazy" })
bar := Debug("bar").Spawn("child").Spawn("grandChild")
bar.Log("bar")
bar.Log(func() string { return "bar lazy" })
if buf.Len() == 0 {
t.Fatalf("buffer should have output")
}
str := buf.String()
// fmt.Println(str)
assert.Contains(t, str, `"field": 1`)
assert.Contains(t, str, `"field2": "two"`)
assert.Contains(t, str, `"field3": "multiple strings"`)
assert.Contains(t, str, `"msg": "foo"`)
assert.Contains(t, str, `"msg": "foo lazy"`)
assert.Contains(t, str, `"msg": "bar"`)
assert.Contains(t, str, `"msg": "bar lazy"`)
// namespaces exist
assert.Contains(t, str, "foo:child:grandChild", "namespace")
// assert.Contains(t, str, "bar:child:grandChild")
}
func TestBasicTextFormatterFieldsStrict(t *testing.T) {
var b []byte
buf := bytes.NewBuffer(b)
SetWriter(buf)
SetFormatterString("text")
Enable("foo*")
//nolint
reg, err := regexp.Compile(
`\d\d:\d\d:\d\d\.\d\d\d\s\d{1,4}(s|ms|us|ns).*foo.*-.*hello.*\n.*field=1 field2=two field3="multiple strings"`,
)
assert.Nil(t, err, "regex error")
foo := Debug("foo").WithFields(
Fields{"field": 1, "field2": "two", "field3": "multiple strings"})
foo.Log("hello")
if buf.Len() == 0 {
t.Fatalf("buffer should have output")
}
str := buf.String()
assert.Contains(t, str, "foo", "namespace")
assert.Contains(t, str, "hello", "message")
assert.Contains(t, str, "field=1")
assert.Contains(t, str, "field2=two")
assert.Contains(t, str, `field3="multiple strings"`)
assert.Regexp(t, reg, str, "strict match")
}