-
Notifications
You must be signed in to change notification settings - Fork 5
/
text_formatter_test.go
216 lines (190 loc) · 5.75 KB
/
text_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package log
import (
"bytes"
"errors"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFormatting(t *testing.T) {
defer newStd()
tf := &TextFormatter{
DisableHostname: true,
DisableTTY: true,
}
testCases := []struct {
value string
expected string
}{
{`foo`, "time=\"0001-01-01T00:00:00.000Z\" level=\"fatal\" msg=\"\" data.test=\"foo\" caller=\"text_formatter_test.go:28 github.com/bdlm/log/v2.TestFormatting\"\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 TestEscaping(t *testing.T) {
defer newStd()
tf := &TextFormatter{DisableTTY: true}
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 TestEscaping_Interface(t *testing.T) {
defer newStd()
tf := &TextFormatter{DisableTTY: true}
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 TestEscaping_Error(t *testing.T) {
defer newStd()
tf := &TextFormatter{DisableTTY: true, DisableHostname: true}
testCases := []struct {
value interface{}
expected string
}{
{errors.New("error: something went wrong"), "time=\"0001-01-01T00:00:00.000Z\" level=\"fatal\" msg=\"\" error=\"error: something went wrong\" caller=\"text_formatter_test.go:94 github.com/bdlm/log/v2.TestEscaping_Error\"\n"},
}
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 TestTimestampFormat(t *testing.T) {
defer newStd()
checkTimeStr := func(format string) {
customFormatter := &TextFormatter{DisableTTY: true, TimestampFormat: format}
customStr, _ := customFormatter.Format(WithField("test", "test"))
timeStart := bytes.Index(customStr, ([]byte)("time=\""))
timeEnd := bytes.Index(customStr, ([]byte)("level="))
timeStr := customStr[timeStart+5+len("\"") : timeEnd-1-len("\"")]
if format == "" {
format = time.RFC3339
}
_, e := time.Parse(format, (string)(timeStr))
if e != nil {
t.Errorf(`time string '%s' did not match provided time format '%s': %s`, timeStr, format, e)
}
}
checkTimeStr("2006-01-02T15:04:05.000000000Z07:00")
checkTimeStr("Mon Jan _2 15:04:05 2006")
checkTimeStr("")
}
//func TestDisableLevelTruncation(t *testing.T) {
// defer newStd()
// entry := &Entry{
// Time: time.Now(),
// Message: "testing",
// }
// keys := []string{}
// timestampFormat := "Mon Jan 2 15:04:05 -0700 MST 2006"
// checkDisableTruncation := func(disabled bool, level Level) {
// tf := &TextFormatter{DisableLevelTruncation: disabled}
// var b bytes.Buffer
// entry.Level = level
// tf.printColored(&b, entry, keys, timestampFormat)
// logLine := (&b).String()
// if disabled {
// expected := strings.ToUpper(level.String())
// if !strings.Contains(logLine, expected) {
// t.Errorf("level string expected to be %s when truncation disabled", expected)
// }
// } else {
// expected := strings.ToUpper(level.String())
// if len(level.String()) > 4 {
// if strings.Contains(logLine, expected) {
// t.Errorf("level string %s expected to be truncated to %s when truncation is enabled", expected, expected[0:4])
// }
// } else {
// if !strings.Contains(logLine, expected) {
// t.Errorf("level string expected to be %s when truncation is enabled and level string is below truncation threshold", expected)
// }
// }
// }
// }
//
// checkDisableTruncation(true, DebugLevel)
// checkDisableTruncation(true, InfoLevel)
// checkDisableTruncation(false, ErrorLevel)
// checkDisableTruncation(false, InfoLevel)
//}
func TestDisableTimestampWithColoredOutput(t *testing.T) {
defer newStd()
tf := &TextFormatter{DisableTimestamp: true, ForceTTY: true}
b, _ := tf.Format(WithField("test", "test"))
if strings.Contains(string(b), "[0000]") {
t.Error("timestamp not expected when DisableTimestamp is true")
}
}
func TestTextFormatterFieldMap(t *testing.T) {
defer newStd()
formatter := &TextFormatter{
DisableTTY: true,
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": "messagefield",
"level-label": "levelfield",
"time-field-label": "timefield",
},
}
b, err := formatter.Format(entry)
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
assert.Equal(
t,
`time-field-label="1981-02-24T04:28:03.000Z" `+
`level-label="warn" msg-label="oh hi" `+
`data-label.field1="f1" `+
`data-label.level-label="levelfield" `+
`data-label.msg-label="messagefield" `+
`data-label.time-field-label="timefield"`+"\n",
string(b),
"Formatted output doesn't respect FieldMap")
}