-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggingResponseWriter.go
130 lines (117 loc) · 3.99 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
package server
import (
"net/http"
"runtime"
"time"
"github.com/sirupsen/logrus"
"encoding/xml"
"encoding/json"
"github.com/pkg/errors"
)
// LoggingResponseWriter describes methods of loggingResponseWriter helper
type LoggingResponseWriter interface {
// WriteHeader will set and log response status code
WriteHeader(code int)
// 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{})
// WriteXML is a helper that will set corresponding content type, encode your data and log response
WriteXML(data interface{})
}
type loggingResponseWriter struct {
rw http.ResponseWriter
remoteAddr string
origin string
uri string
method string
startTime time.Time
statusCode int
error error
logger logrus.FieldLogger
}
func (l *loggingResponseWriter) log() {
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
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)
l.log()
}
// WriteJSON is a helper that will set corresponding content type, encode your data and log response
func (l *loggingResponseWriter) WriteJSON(data interface{}) {
l.rw.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(l.rw).Encode(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{}) {
_, 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()
}
// ******************************************************************************************************
// 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{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
}