-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttplog.go
316 lines (267 loc) · 10.5 KB
/
httplog.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Package httplog logs http requests and responses. It’s highly configurable,
// e.g. in production, log all response and requests, but don’t log
// the body or headers, in your dev environment log everything and so
// on. httplog also has different ways to log depending on your
// preference — structured logging via JSON, relational database
// logging or just plain standard library logging. httplog has logic
// to turn on/off logging based on options you can either pass in to
// the middleware handler or from a JSON input file included with the
// library. httplog offers three middleware choices, each of which
// adhere to fairly common middleware patterns: a simple HandlerFunc
// (`LogHandlerFunc`), a function (`LogHandler`) that takes a handler
// and returns a handler (aka Constructor) (`func (http.Handler) http.Handler`)
// often used with alice (https://github.com/justinas/alice) and finally a
// function (`LogAdapter`) that returns an Adapter type. An `httplog.Adapt`
// function and `httplog.Adapter` type are provided. Beyond logging request
// and response elements, httplog creates a unique id for each incoming
// request (using xid (https://github.com/rs/xid)) and sets it (and a few
// other key request elements) into the request context. You can access
// these context items using provided helper functions, including a function
// that returns an audit struct you can add to response payloads that provide
// clients with helpful information for support.
//
// !!!!WARNING!!!! - This package works, but is something I wrote a long time
// ago and really needs to be updated. I logged Issue #8 to some day address
// this.
package httplog
import (
"database/sql"
"net/http"
"net/http/httptest"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/gilcrest/httplog/errs"
)
// LogHandlerFunc middleware records and logs as much as possible about an
// incoming HTTP request and response
func LogHandlerFunc(next http.HandlerFunc, logger zerolog.Logger, db *sql.DB, o *Opts) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
var (
opts *Opts
err error
)
// If o is passed in, then use it, otherwise opts will
// remain its zero value and seeing as the elements within are
// all booleans, all will be false (false is the boolean zero value)
if o != nil {
opts = o
} else {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, "gilcrest/httplog unsupported: nil cannot be passed currently for *Opts until bug #6 has been resolved"))
return
// opts, err = FileOpts()
// if err != nil {
// errStr := fmt.Sprintf("Unable to load logging options from file, error = %s", err.Error())
// http.Error(w, errStr, http.StatusBadRequest)
// return
// }
}
// Pull the context from the request
ctx := req.Context()
// Create an instance of APIaudit and pass it to startTimer
// to begin the API response timer
ctx, aud, err := newAPIAudit(ctx, logger, req)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, "Unable to log request"))
return
}
aud.startTimer()
ctx = setRequest2Context(ctx, aud)
// RequestLogController determines which of the logging methods
// you wish to use will be employed (based on the options passed in)
err = requestLogController(ctx, logger, aud, req, opts)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, "Unable to log request"))
return
}
rec := httptest.NewRecorder()
next.ServeHTTP(rec, req.WithContext(ctx))
// copy everything from response recorder
// to actual response writer
for k, v := range rec.Header() {
w.Header()[k] = v
}
w.WriteHeader(rec.Code)
// pull out the response body and write it
// back to the response writer
b := rec.Body.Bytes()
w.Write(b)
aud.stopTimer()
// write the data back to the recorder buffer as
// it's needed for SetResponse
rec.Body.Write(b)
// set the response data in the APIAudit object
err = aud.setResponse(logger, rec)
if err != nil {
log.Warn().Err(err).Msg("Error from setResponse in httplog")
}
// call responseLogController to determine if and where to log
err = responseLogController(ctx, logger, db, aud, opts)
if err != nil {
log.Warn().Err(err).Msg("Error from responseLogController in httplog")
}
}
}
// LogHandler records and logs as much as possible about an
// incoming HTTP request and response
func LogHandler(logger zerolog.Logger, db *sql.DB, o *Opts) (mw func(http.Handler) http.Handler) {
mw = func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var (
opts *Opts
err error
)
// If o is passed in, then use it, otherwise opts will
// remain its zero value and seeing as the elements within are
// all booleans, all will be false (false is the boolean zero value)
if o != nil {
opts = o
} else {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, "gilcrest/httplog unsupported: nil cannot be passed currently for *Opts until bug #6 has been resolved"))
return
// opts, err = FileOpts()
// if err != nil {
// errStr := fmt.Sprintf("Unable to load logging options from file, error = %s", err.Error())
// http.Error(w, errStr, http.StatusBadRequest)
// return
// }
}
// Pull the context from the request
ctx := req.Context()
// Create an instance of APIaudit and pass it to startTimer
// to begin the API response timer
ctx, aud, err := newAPIAudit(ctx, logger, req)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, "Unable to log request"))
return
}
aud.startTimer()
ctx = setRequest2Context(ctx, aud)
// RequestLogController determines which of the logging methods
// you wish to use will be employed (based on the options passed in)
err = requestLogController(ctx, logger, aud, req, opts)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, "Unable to log request"))
return
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req.WithContext(ctx))
// copy everything from response recorder
// to actual response writer
for k, v := range rec.Header() {
w.Header()[k] = v
}
w.WriteHeader(rec.Code)
// pull out the response body and write it
// back to the response writer
b := rec.Body.Bytes()
w.Write(b)
aud.stopTimer()
// write the data back to the recorder buffer as
// it's needed for SetResponse
rec.Body.Write(b)
// set the response data in the APIAudit object
err = aud.setResponse(logger, rec)
if err != nil {
log.Warn().Err(err).Msg("Error from setResponse in httplog")
}
// call responseLogController to determine if and where to log
err = responseLogController(ctx, logger, db, aud, opts)
if err != nil {
log.Warn().Err(err).Msg("Error from responseLogController in httplog")
}
})
}
return
}
// LogAdapter records and logs as much as possible about an
// incoming HTTP request and response using the Adapter pattern
// Found adapter pattern in a Mat Ryer post
func LogAdapter(logger zerolog.Logger, db *sql.DB, o *Opts) Adapter {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var (
opts *Opts
err error
)
// If o is passed in, then use it, otherwise opts will
// remain its zero value and seeing as the elements within are
// all booleans, all will be false (false is the boolean zero value)
if o != nil {
opts = o
} else {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, "Unable to log request"))
return
// opts, err = FileOpts()
// if err != nil {
// errStr := fmt.Sprintf("Unable to load logging options from file, error = %s", err.Error())
// http.Error(w, errStr, http.StatusBadRequest)
// return
// }
}
// Pull the context from the request
ctx := req.Context()
// Create an instance of APIaudit and pass it to startTimer
// to begin the API response timer
ctx, aud, err := newAPIAudit(ctx, logger, req)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, "Unable to log request"))
return
}
aud.startTimer()
ctx = setRequest2Context(ctx, aud)
// RequestLogController determines which of the logging methods
// you wish to use will be employed (based on the options passed in)
// It will also populate the APIAudit struct based on the incoming request
err = requestLogController(ctx, logger, aud, req, opts)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, "Unable to log request"))
return
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req.WithContext(ctx))
// copy everything from response recorder
// to actual response writer
for k, v := range rec.Header() {
w.Header()[k] = v
}
w.WriteHeader(rec.Code)
// pull out the response body and write it
// back to the response writer
b := rec.Body.Bytes()
w.Write(b)
aud.stopTimer()
// write the data back to the recorder buffer as
// it's needed for SetResponse
rec.Body.Write(b)
// set the response data in the APIAudit object
err = aud.setResponse(logger, rec)
if err != nil {
log.Warn().Err(err).Msg("Error from setResponse in httplog")
}
// call responseLogController to determine if and where to log
err = responseLogController(ctx, logger, db, aud, opts)
if err != nil {
log.Warn().Err(err).Msg("Error from responseLogController in httplog")
}
})
}
}
// Adapter type (it gets its name from the adapter pattern — also known as the
// decorator pattern) above is a function that both takes in and returns an
// http.Handler. This is the essence of the wrapper; we will pass in an existing
// http.Handler, the Adapter will adapt it, and return a new (probably wrapped)
// http.Handler for us to use in its place. So far this is not much different
// from just wrapping http.HandlerFunc types, however, now, we can instead write
// functions that themselves return an Adapter. - Mat Ryer @matryer
type Adapter func(http.Handler) http.Handler
// Adapt function takes the handler you want to adapt, and a list of our
// Adapter types. The result of any wrapper function should be an
// acceptable Adapter. Our Adapt function will simply iterate over all
// adapters, calling them one by one (in reverse order) in a chained manner,
// returning the result of the first adapter. - Mat Ryer @matryer
func Adapt(h http.Handler, adapters ...Adapter) http.Handler {
for _, adapter := range adapters {
h = adapter(h)
}
return h
}