/
request.go
41 lines (37 loc) · 1.1 KB
/
request.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
package zapmw
import (
"net/http"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// OptionRequest is an option to add fields for request log.
type OptionRequest func(r *http.Request) []zap.Field
// Request a middleware that logs the start and end of each request.
func Request(lvl zapcore.Level, msg string, opts ...OptionRequest) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ww := NewWrapResponseWriter(w, r.ProtoMajor)
defer func(begin time.Time) {
logger := GetZap(r)
fields := []zap.Field{
zap.String("method", r.Method),
zap.String("url", r.URL.String()),
zap.String("proto", r.Proto),
zap.Int("status", ww.Status()),
zap.String("ip", r.RemoteAddr),
zap.Int("byte", ww.BytesWritten()),
zap.Duration("took", time.Since(begin)),
}
for _, o := range opts {
fields = append(fields, o(r)...)
}
if ce := logger.Check(lvl, msg); ce != nil {
ce.Write(fields...)
}
}(time.Now())
next.ServeHTTP(ww, r)
}
return http.HandlerFunc(fn)
}
}