-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathformatter_test.go
113 lines (98 loc) · 2.65 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
113
package easy
import (
"bytes"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
)
func TestFormatterDefaultFormat(t *testing.T) {
f := Formatter{}
e := logrus.WithField("", "")
e.Message = "Test Message"
e.Level = logrus.WarnLevel
e.Time = time.Now()
b, _ := f.Format(e)
expected := strings.Join([]string{"[WARNING]:", e.Time.Format(time.RFC3339), "- Test Message"}, " ")
if string(b) != expected {
t.Errorf("formatting expected result was %q instead of %q", string(b), expected)
}
}
func TestFormatterFormatWithCustomData(t *testing.T) {
f := Formatter{}
testValues := []struct {
name string
format string
fields logrus.Fields
result string
}{
{
"Single custom param",
"[%lvl%]: %time% - %first%",
map[string]interface{}{"first": "First Custom Param"},
"[PANIC]: 0001-01-01T00:00:00Z - First Custom Param",
},
{
"Multiple custom params of type string",
"[%lvl%]: %time% - %first% %second%",
map[string]interface{}{"first": "First Custom Param", "second": "Second Custom Param"},
"[PANIC]: 0001-01-01T00:00:00Z - First Custom Param Second Custom Param",
},
{
"Multiple custom params of different type",
"[%lvl%]: %time% - %string%, %bool%, %int%",
map[string]interface{}{"string": "String param", "bool": true, "int": 42},
"[PANIC]: 0001-01-01T00:00:00Z - String param, true, 42",
},
{
"Omits fields not included in format",
"[%lvl%]: %time% - %first% %random%",
map[string]interface{}{"first": "String param", "not_included": "random string"},
"[PANIC]: 0001-01-01T00:00:00Z - String param %random%",
},
}
for _, tv := range testValues {
t.Run(tv.name, func(t *testing.T) {
f.LogFormat = tv.format
b, _ := f.Format(logrus.WithFields(tv.fields))
if string(b) != tv.result {
t.Errorf("formatting expected result was %q instead of %q", string(b), tv.result)
}
})
}
}
func TestFormatterFormatWithCustomDateFormat(t *testing.T) {
f := Formatter{}
testValues := []struct {
name string
timestampFormat string
}{
{
"Timestamp with RFC822 format",
time.RFC822,
},
{
"Timestamp with RFC850 format",
time.RFC850,
},
{
"Timestamp with `yyyy-mm-dd hh:mm:ss` format",
"2006-01-02 15:04:05",
},
{
"Timestamp with `yyyy-mm-dd` format",
"2006-01-02",
},
}
for _, tv := range testValues {
t.Run(tv.name, func(t *testing.T) {
f.TimestampFormat = tv.timestampFormat
e := logrus.WithField("", "")
e.Time = time.Now()
b, _ := f.Format(e)
if !bytes.Contains(b, []byte(e.Time.Format(tv.timestampFormat))) {
t.Errorf("formatting expected format date was %q instead of %q", string(b), tv.timestampFormat)
}
})
}
}