-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_test.go
More file actions
170 lines (118 loc) · 5.04 KB
/
format_test.go
File metadata and controls
170 lines (118 loc) · 5.04 KB
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
package logged_test
import (
"encoding/json"
"testing"
"time"
"github.com/msales/logged"
"github.com/stretchr/testify/assert"
)
func TestJsonFormat(t *testing.T) {
f := logged.JSONFormat()
b := f.Format("some message", logged.Error, []interface{}{"x", 1, "y", 3.2, "bool", true,
"carriage_return", "bang" + string('\r') + "foo", "tab", "bar baz", "newline", "foo\nbar", "escape", string('\\')})
expect := []byte(`{"lvl":"eror","msg":"some message","x":1,"y":3.2,"bool":true,"carriage_return":"bang\rfoo","tab":"bar\tbaz","newline":"foo\nbar","escape":"\\"}` + "\n")
assert.Equal(t, expect, b)
m := map[string]interface{}{}
err := json.Unmarshal(b, &m)
assert.NoError(t, err)
}
func TestJsonFormat_KeyError(t *testing.T) {
f := logged.JSONFormat()
b := f.Format("some message", logged.Error, []interface{}{1, "y"})
expect := []byte(`{"lvl":"eror","msg":"some message","LOGGED_ERROR":1}` + "\n")
assert.Equal(t, expect, b)
m := map[string]interface{}{}
err := json.Unmarshal(b, &m)
assert.NoError(t, err)
}
func TestJsonFormat_Ints(t *testing.T) {
f := logged.JSONFormat()
b := f.Format("", logged.Error, []interface{}{"int", 1, "int8", int8(2), "int16", int16(3), "int32", int32(4), "int64", int64(5)})
expect := []byte(`{"lvl":"eror","msg":"","int":1,"int8":2,"int16":3,"int32":4,"int64":5}` + "\n")
assert.Equal(t, expect, b)
m := map[string]interface{}{}
err := json.Unmarshal(b, &m)
assert.NoError(t, err)
}
func TestJsonFormat_Uints(t *testing.T) {
f := logged.JSONFormat()
b := f.Format("", logged.Error, []interface{}{"uint", uint(1), "uint8", uint8(2), "uint16", uint16(3), "uint32", uint32(4), "uint64", uint64(5)})
expect := []byte(`{"lvl":"eror","msg":"","uint":1,"uint8":2,"uint16":3,"uint32":4,"uint64":5}` + "\n")
assert.Equal(t, expect, b)
m := map[string]interface{}{}
err := json.Unmarshal(b, &m)
assert.NoError(t, err)
}
func TestJsonFormat_Floats(t *testing.T) {
f := logged.JSONFormat()
b := f.Format("", logged.Error, []interface{}{"float32", float32(1), "float64", float64(4.56)})
expect := []byte(`{"lvl":"eror","msg":"","float32":1,"float64":4.56}` + "\n")
assert.Equal(t, expect, b)
m := map[string]interface{}{}
err := json.Unmarshal(b, &m)
assert.NoError(t, err)
}
func TestJsonFormat_Time(t *testing.T) {
f := logged.JSONFormat()
b := f.Format("", logged.Error, []interface{}{"time", time.Unix(1541573670, 0).UTC()})
expect := []byte(`{"lvl":"eror","msg":"","time":"2018-11-07T06:54:30+0000"}` + "\n")
assert.Equal(t, expect, b)
m := map[string]interface{}{}
err := json.Unmarshal(b, &m)
assert.NoError(t, err)
}
func TestJsonFormat_Unknown(t *testing.T) {
obj := struct {
Name string
}{Name: "test"}
f := logged.JSONFormat()
b := f.Format("", logged.Error, []interface{}{"what", obj, "nil", nil})
expect := []byte(`{"lvl":"eror","msg":"","what":"{Name:test}","nil":null}` + "\n")
assert.Equal(t, expect, b)
}
func TestLogfmtFormat(t *testing.T) {
f := logged.LogfmtFormat()
b := f.Format("some message", logged.Error, []interface{}{"x", 1, "y", 3.2, "bool", true, "equals", "=", "quote", "\"",
"carriage_return", "bang" + string('\r') + "foo", "tab", "bar baz", "newline", "foo\nbar", "escape", string('\\')})
expect := []byte(`lvl=eror msg="some message" x=1 y=3.200 bool=true equals="=" quote="\"" carriage_return="bang\rfoo" tab="bar\tbaz" newline="foo\nbar" escape=\\` + "\n")
assert.Equal(t, expect, b)
}
func TestLogfmtFormat_KeyError(t *testing.T) {
f := logged.LogfmtFormat()
b := f.Format("some message", logged.Error, []interface{}{1, "y"})
expect := []byte(`lvl=eror msg="some message" LOGGED_ERROR=1` + "\n")
assert.Equal(t, expect, b)
}
func TestLogfmtFormat_Ints(t *testing.T) {
f := logged.LogfmtFormat()
b := f.Format("", logged.Error, []interface{}{"int", 1, "int8", int8(2), "int16", int16(3), "int32", int32(4), "int64", int64(5)})
expect := []byte(`lvl=eror msg= int=1 int8=2 int16=3 int32=4 int64=5` + "\n")
assert.Equal(t, expect, b)
}
func TestLogfmtFormat_Uints(t *testing.T) {
f := logged.LogfmtFormat()
b := f.Format("", logged.Error, []interface{}{"uint", uint(1), "uint8", uint8(2), "uint16", uint16(3), "uint32", uint32(4), "uint64", uint64(5)})
expect := []byte(`lvl=eror msg= uint=1 uint8=2 uint16=3 uint32=4 uint64=5` + "\n")
assert.Equal(t, expect, b)
}
func TestLogfmtFormat_Floats(t *testing.T) {
f := logged.LogfmtFormat()
b := f.Format("", logged.Error, []interface{}{"float32", float32(1.23), "float64", float64(4.56)})
expect := []byte(`lvl=eror msg= float32=1.230 float64=4.560` + "\n")
assert.Equal(t, expect, b)
}
func TestLogfmtFormat_Time(t *testing.T) {
f := logged.LogfmtFormat()
b := f.Format("", logged.Error, []interface{}{"time", time.Unix(1541573670, 0).UTC()})
expect := []byte(`lvl=eror msg= time=2018-11-07T06:54:30+0000` + "\n")
assert.Equal(t, expect, b)
}
func TestLogfmtFormat_Unknown(t *testing.T) {
obj := struct {
Name string
}{Name: "test"}
f := logged.LogfmtFormat()
b := f.Format("", logged.Error, []interface{}{"what", obj, "nil", nil})
expect := []byte(`lvl=eror msg= what={Name:test} nil=` + "\n")
assert.Equal(t, expect, b)
}