-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter_test.go
More file actions
202 lines (163 loc) · 5.05 KB
/
formatter_test.go
File metadata and controls
202 lines (163 loc) · 5.05 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
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
package appengine
import (
"encoding/json"
"errors"
"runtime"
"strings"
"testing"
"time"
log "github.com/sirupsen/logrus"
)
func TestErrorNotLost(t *testing.T) {
formatter := &Formatter{}
b, err := formatter.Format(log.WithField("error", errors.New("wild walrus")))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
entry := make(map[string]interface{})
err = json.Unmarshal(b, &entry)
if err != nil {
t.Fatal("Unable to unmarshal formatted entry: ", err)
}
if entry["error"] != "wild walrus" {
t.Fatal("Error field not set")
}
}
func TestErrorNotLostOnFieldNotNamedError(t *testing.T) {
formatter := &Formatter{}
b, err := formatter.Format(log.WithField("omg", errors.New("wild walrus")))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
entry := make(map[string]interface{})
err = json.Unmarshal(b, &entry)
if err != nil {
t.Fatal("Unable to unmarshal formatted entry: ", err)
}
if entry["omg"] != "wild walrus" {
t.Fatal("Error field not set")
}
}
func TestFieldClashWithTime(t *testing.T) {
formatter := &Formatter{}
b, err := formatter.Format(log.WithField("timestamp", "right now!"))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
entry := make(map[string]interface{})
err = json.Unmarshal(b, &entry)
if err != nil {
t.Fatal("Unable to unmarshal formatted entry: ", err)
}
if entry["fields.timestamp"] != "right now!" {
t.Fatal("fields.timestamp not set to original time field")
}
cur := &time.Time{}
if entry["timestamp"].(map[string]interface{})["seconds"] != float64(cur.Unix()) {
t.Fatalf("timestamp field not set to current time (%d), was: %+v", cur.Unix(), entry["timestamp"])
}
}
func TestFieldClashWithMsg(t *testing.T) {
formatter := &Formatter{}
b, err := formatter.Format(log.WithField("message", "something"))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
entry := make(map[string]interface{})
err = json.Unmarshal(b, &entry)
if err != nil {
t.Fatal("Unable to unmarshal formatted entry: ", err)
}
if entry["fields.message"] != "something" {
t.Fatal("fields.message not set to original message field")
}
}
func TestFieldClashWithLevel(t *testing.T) {
formatter := &Formatter{}
b, err := formatter.Format(log.WithField("level", "something"))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
entry := make(map[string]interface{})
err = json.Unmarshal(b, &entry)
if err != nil {
t.Fatal("Unable to unmarshal formatted entry: ", err)
}
if entry["fields.level"] != "something" {
t.Fatal("fields.level not set to original level field")
}
}
func TestJSONEntryEndsWithNewline(t *testing.T) {
formatter := &Formatter{}
b, err := formatter.Format(log.WithField("level", "something"))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
if b[len(b)-1] != '\n' {
t.Fatal("Expected JSON log entry to end with a newline")
}
}
func TestFieldDoesNotClashWithCaller(t *testing.T) {
log.SetReportCaller(false)
formatter := &Formatter{}
b, err := formatter.Format(log.WithField("func", "howdy pardner"))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
entry := make(map[string]interface{})
err = json.Unmarshal(b, &entry)
if err != nil {
t.Fatal("Unable to unmarshal formatted entry: ", err)
}
if entry["func"] != "howdy pardner" {
t.Fatal("func field replaced when ReportCaller=false")
}
}
func TestFieldClashWithCaller(t *testing.T) {
log.SetReportCaller(true)
formatter := &Formatter{}
e := log.WithField("logging.googleapis.com/sourceLocation", "howdy pardner")
e.Caller = &runtime.Frame{Function: "somefunc"}
b, err := formatter.Format(e)
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
entry := make(map[string]interface{})
err = json.Unmarshal(b, &entry)
if err != nil {
t.Fatal("Unable to unmarshal formatted entry: ", err)
}
if entry["fields.logging.googleapis.com/sourceLocation"] != "howdy pardner" {
t.Fatalf("fields.logging.googleapis.com/sourceLocation not set to original sourceLocation field when ReportCaller=true (got '%s')",
entry["fields.logging.googleapis.com/sourceLocation"])
}
if entry["logging.googleapis.com/sourceLocation"].(map[string]interface{})["function"] != "somefunc" {
t.Fatalf("logging.googleapis.com/sourceLocation.function not set as expected when ReportCaller=true (got '%s')",
entry["logging.googleapis.com/sourceLocation"].(map[string]interface{})["function"])
}
log.SetReportCaller(false) // return to default value
}
func TestJSONDisableTimestamp(t *testing.T) {
formatter := &Formatter{
DisableTimestamp: true,
}
b, err := formatter.Format(log.WithField("level", "something"))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
s := string(b)
if strings.Contains(s, log.FieldKeyTime) {
t.Error("Did not prevent timestamp", s)
}
}
func TestJSONEnableTimestamp(t *testing.T) {
formatter := &Formatter{}
b, err := formatter.Format(log.WithField("level", "something"))
if err != nil {
t.Fatal("Unable to format entry: ", err)
}
s := string(b)
if !strings.Contains(s, log.FieldKeyTime) {
t.Error("Timestamp not present", s)
}
}