-
Notifications
You must be signed in to change notification settings - Fork 5
/
std_formatter_test.go
179 lines (152 loc) · 4.06 KB
/
std_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
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
package log
import (
"bytes"
"errors"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestStdFormatting(t *testing.T) {
defer newStd()
tf := &StdFormatter{
DisableHostname: true,
}
testCases := []struct {
value string
expected string
}{
{`foo`, "0001/01/01 00:00:00 level=\"fatal\" data.test=\"foo\" caller=\"std_formatter_test.go:27 github.com/bdlm/log/v2.TestStdFormatting\"\n"},
}
for _, tc := range testCases {
b, _ := tf.Format(WithField("test", tc.value))
if string(b) != tc.expected {
t.Errorf(
"formatting expected for %q (result was %q instead of %q)",
tc.value,
string(b),
tc.expected,
)
}
}
}
func TestStdEscaping(t *testing.T) {
defer newStd()
tf := &StdFormatter{}
testCases := []struct {
value string
expected string
}{
{`ba"r`, `ba\\\"r`},
{`ba'r`, `ba'r`},
}
for _, tc := range testCases {
b, _ := tf.Format(WithField("test", tc.value))
if !bytes.Contains(b, []byte(tc.expected)) {
t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
}
}
}
func TestStdEscaping_Interface(t *testing.T) {
defer newStd()
tf := &StdFormatter{}
ts := time.Now()
testCases := []struct {
value interface{}
expected string
}{
{ts.Format(defaultTimestampFormat), ts.Format(defaultTimestampFormat)},
}
for _, tc := range testCases {
b, _ := tf.Format(WithField("test", tc.value))
if !bytes.Contains(b, []byte(tc.expected)) {
t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
}
}
}
func TestStdEscaping_Error(t *testing.T) {
defer newStd()
tf := &StdFormatter{}
testCases := []struct {
value interface{}
expected string
}{
{errors.New("error: something went wrong"), "\"error: something went wrong\""},
}
for _, tc := range testCases {
b, _ := tf.Format(WithError(tc.value.(error)))
if !bytes.Contains(b, []byte(tc.expected)) {
t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
}
}
}
func TestStdTimestampFormat(t *testing.T) {
defer newStd()
checkTimeStr := func(format string) {
customFormatter := &StdFormatter{TimestampFormat: format}
if "" == format {
customFormatter = &StdFormatter{}
format = "2006/01/02 15:04:05"
}
customStr, _ := customFormatter.Format(WithField("test", "test"))
timeStr := customStr[:len(format)]
if "" == format {
timeStr = customStr[:bytes.Index(customStr, []byte(" "))]
}
_, e := time.Parse(format, string(timeStr))
if e != nil {
t.Errorf(`failed to parse '%s'. format: '%s', err: %s`, timeStr, format, e)
}
}
checkTimeStr("2006-01-02T15:04:05.000000000Z08:00")
checkTimeStr("Mon Jan _2 15:04:05 2006")
checkTimeStr("")
}
func TestStdDisableTimestampWithColoredOutput(t *testing.T) {
defer newStd()
tf := &StdFormatter{DisableTimestamp: true}
b, _ := tf.Format(WithField("test", "test"))
if strings.Contains(string(b), "[0000]") {
t.Error("timestamp not expected when DisableTimestamp is true")
}
}
func TestStdTextFormatterFieldMap(t *testing.T) {
defer newStd()
formatter := &StdFormatter{
DisableHostname: true,
DisableCaller: true,
FieldMap: FieldMap{
LabelCaller: "caller-label",
LabelData: "data-label",
LabelHost: "host-label",
LabelLevel: "level-label",
LabelMsg: "msg-label",
LabelTime: "time-field-label",
},
}
entry := &Entry{
Message: "oh hi",
Level: WarnLevel,
Time: time.Date(1981, time.February, 24, 4, 28, 3, 100, time.UTC),
Data: Fields{
"field1": "f1",
"msg-label": "messageData",
"level-label": "levelData",
"time-field-label": "timeData",
},
}
b, err := formatter.Format(entry)
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
assert.Equal(
t,
`1981/02/24 04:28:03 oh hi `+
`level-label="warn" `+
`data-label.field1="f1" `+
`data-label.level-label="levelData" `+
`data-label.msg-label="messageData" `+
`data-label.time-field-label="timeData"`+"\n",
string(b),
"Formatted output doesn't respect FieldMap")
}