/
loggingResponseWriter.go
188 lines (167 loc) · 5.3 KB
/
loggingResponseWriter.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
package server
import (
"encoding/json"
"encoding/xml"
"net/http"
"reflect"
"runtime"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// LoggingResponseWriter describes methods of loggingResponseWriter helper
type LoggingResponseWriter interface {
http.ResponseWriter
// Log will log request result
Log()
// WriteHeaderWithErr same as WriteHeader but also will set and Log error with metadata
WriteHeaderWithErr(code int, err error)
// WriteJSON is a helper that will set corresponding content type, encode your data and Log response
WriteJSON(data interface{}, status ...int)
// WriteXML is a helper that will set corresponding content type, encode your data and Log response
WriteXML(data interface{}, status ...int)
}
type loggingResponseWriter struct {
logged bool
rw http.ResponseWriter
remoteAddr string
origin string
uri string
method string
startTime time.Time
statusCode int
error error
logger logrus.FieldLogger
}
func (l *loggingResponseWriter) Header() http.Header {
return l.rw.Header()
}
func (l *loggingResponseWriter) Write(p []byte) (n int, err error) {
return l.rw.Write(p)
}
func (l *loggingResponseWriter) Log() {
l.logged = true
if l.error != nil {
pc, fn, line, _ := runtime.Caller(3)
l.logger.WithFields(logrus.Fields{
"func": runtime.FuncForPC(pc).Name(),
"file": fn,
"line": line,
}).Warnf("%s (%d %s) -> %s", l.uri, l.statusCode, http.StatusText(l.statusCode), l.error)
} else {
l.logger.Infof("%s (%d %s)", l.uri, l.statusCode, http.StatusText(l.statusCode))
}
}
// WriteHeader will set and log response status code
func (l *loggingResponseWriter) WriteHeader(code int) {
l.statusCode = code
if code == http.StatusNotFound {
l.rw.Header().Set("X-ServiceFabric", "ResourceNotFound")
}
l.rw.WriteHeader(code)
l.Log()
}
// WriteHeaderWithErr same as WriteHeader but also will set and log error with metadata
func (l *loggingResponseWriter) WriteHeaderWithErr(code int, err error) {
l.error = err
l.statusCode = code
l.rw.WriteHeader(code)
if code == http.StatusNotFound {
l.rw.Header().Set("X-ServiceFabric", "ResourceNotFound")
}
l.Log()
}
// WriteJSON is a helper that will set corresponding content type, encode your data and log response
func (l *loggingResponseWriter) WriteJSON(data interface{}, status ...int) {
l.rw.Header().Set("Content-Type", "application/json")
if len(status) != 0 {
l.statusCode = status[0]
if status[0] == http.StatusNotFound {
l.rw.Header().Set("X-ServiceFabric", "ResourceNotFound")
}
l.rw.WriteHeader(status[0])
}
err := json.NewEncoder(l.rw).Encode(checkForNil(data))
if err != nil {
l.WriteHeaderWithErr(http.StatusInternalServerError, errors.Wrap(err, "failed to write response"))
return
}
l.Log()
}
// WriteXML is a helper that will set corresponding content type, encode your data and log response
func (l *loggingResponseWriter) WriteXML(data interface{}, status ...int) {
l.rw.Header().Set("Content-Type", "application/xml")
if len(status) != 0 {
l.statusCode = status[0]
if status[0] == http.StatusNotFound {
l.rw.Header().Set("X-ServiceFabric", "ResourceNotFound")
}
l.rw.WriteHeader(status[0])
}
_, err := l.rw.Write([]byte(xml.Header))
if err != nil {
l.WriteHeaderWithErr(http.StatusInternalServerError, errors.Wrap(err, "failed to write xml header"))
return
}
err = xml.NewEncoder(l.rw).Encode(data)
if err != nil {
l.WriteHeaderWithErr(http.StatusInternalServerError, errors.Wrap(err, "failed to write response"))
return
}
l.Log()
}
func checkForNil(d interface{}) interface{} {
switch d.(type) {
case bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128, string:
return d
}
rVal := reflect.ValueOf(d)
if rVal.Kind() == reflect.Struct {
return d
}
if rVal.IsNil() {
switch rVal.Kind() {
case reflect.Slice:
return []struct{}{}
default:
return struct{}{}
}
}
return d
}
// ******************************************************************************************************
// We can put it in base implementation when list of errors will be stabilized and moved to separate repo
// ******************************************************************************************************
//// CheckBLError will check error from BL layer and will send corresponding http status on not nil
//// It will return false if error == nil
//func (l *loggingResponseWriter) CheckBLError(err error) bool {
// if err == nil {
// return false
// }
//
// switch err {
// case ErrEntityNotFound:
// l.WriteHeader(http.StatusNotFound)
// return true
// case ErrInvalidData:
// l.WriteHeader(http.StatusBadRequest)
// return true
// case ErrDataConflict:
// l.WriteHeader(http.StatusConflict)
// return true
// }
// l.WriteHeaderWithErr(http.StatusInternalServerError, err)
// return true
//}
func newLoggingResponseWriter(logger logrus.FieldLogger, remoteAddr, origin, uri, method string, w http.ResponseWriter) *loggingResponseWriter {
l := &loggingResponseWriter{false, w, remoteAddr, origin, uri, method, time.Now(), http.StatusOK, nil, nil}
l.logger = logger.WithFields(logrus.Fields{
"ip": remoteAddr,
"origin": origin,
"uri": uri,
"method": method,
"executed_start": l.startTime,
"type": "endpoint",
})
return l
}