-
Notifications
You must be signed in to change notification settings - Fork 7
/
zap.go
41 lines (34 loc) · 912 Bytes
/
zap.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 adapter
import (
"net/http"
"time"
"github.com/Code-Hex/fast-service/internal/logger"
"github.com/rs/xid"
"go.uber.org/zap"
)
type delegator struct {
http.ResponseWriter
Status int
}
// ZapAdapter returns Adapter which adapt logging middleware.
func ZapAdapter(l *zap.Logger) Adapter {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
ctx := logger.ToContext(
r.Context(),
l.With(zap.Stringer("request_id", xid.New())),
)
d := &delegator{ResponseWriter: w}
next.ServeHTTP(d, r.WithContext(ctx))
logger.Info(ctx, "request",
zap.String("host", r.Host),
zap.String("path", r.URL.Path),
zap.Int("status", d.Status),
zap.Duration("duration", time.Now().Sub(start)),
zap.String("method", r.Method),
zap.String("user_agent", r.UserAgent()),
)
})
}
}