|
1 | 1 | package abcmiddleware
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 | 4 | "fmt"
|
6 | 5 | "net/http"
|
7 | 6 | "time"
|
8 | 7 |
|
9 |
| - chimiddleware "github.com/go-chi/chi/middleware" |
10 | 8 | "go.uber.org/zap"
|
11 | 9 | )
|
12 | 10 |
|
13 |
| -// zapResponseWriter is a wrapper that includes that http status and size for logging |
14 |
| -type zapResponseWriter struct { |
15 |
| - http.ResponseWriter |
16 |
| - status int |
17 |
| - size int |
| 11 | +type zapLogMiddleware struct { |
| 12 | + logger *zap.Logger |
18 | 13 | }
|
19 | 14 |
|
20 |
| -// Zap middleware handles web request logging using Zap |
21 |
| -func (m Middleware) Zap(next http.Handler) http.Handler { |
22 |
| - fn := func(w http.ResponseWriter, r *http.Request) { |
23 |
| - t := time.Now() |
24 |
| - zw := &zapResponseWriter{ResponseWriter: w} |
25 |
| - |
26 |
| - // Serve the request |
27 |
| - next.ServeHTTP(zw, r) |
| 15 | +// ZapLog returns a logging middleware that outputs details about a request |
| 16 | +func ZapLog(logger *zap.Logger) MW { |
| 17 | + return zapLogMiddleware{logger: logger} |
| 18 | +} |
28 | 19 |
|
29 |
| - // Write the request log line |
30 |
| - writeZap(m.Log, r, t, zw.status, zw.size) |
31 |
| - } |
| 20 | +// Zap middleware handles web request logging using Zap |
| 21 | +func (z zapLogMiddleware) Wrap(next http.Handler) http.Handler { |
| 22 | + return zapLogger{mid: z, next: next} |
| 23 | +} |
32 | 24 |
|
33 |
| - return http.HandlerFunc(fn) |
| 25 | +type zapLogger struct { |
| 26 | + mid zapLogMiddleware |
| 27 | + next http.Handler |
34 | 28 | }
|
35 | 29 |
|
36 |
| -// RequestIDLogger middleware creates a derived logger to include logging of the |
37 |
| -// Request ID, and inserts it into the context object |
38 |
| -func (m Middleware) RequestIDLogger(next http.Handler) http.Handler { |
39 |
| - fn := func(w http.ResponseWriter, r *http.Request) { |
40 |
| - requestID := chimiddleware.GetReqID(r.Context()) |
41 |
| - derivedLogger := m.Log.With(zap.String("request_id", requestID)) |
42 |
| - r = r.WithContext(context.WithValue(r.Context(), CtxLoggerKey, derivedLogger)) |
43 |
| - next.ServeHTTP(w, r) |
44 |
| - } |
| 30 | +func (z zapLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 31 | + startTime := time.Now() |
| 32 | + zw := &zapResponseWriter{ResponseWriter: w} |
45 | 33 |
|
46 |
| - return http.HandlerFunc(fn) |
47 |
| -} |
| 34 | + // Serve the request |
| 35 | + z.next.ServeHTTP(zw, r) |
48 | 36 |
|
49 |
| -// Log returns the Request ID scoped logger from the request Context |
50 |
| -// and panics if it cannot be found. This function is only ever used |
51 |
| -// by your controllers if your app uses the RequestID middlewares, |
52 |
| -// otherwise you should use the controller's receiver logger directly. |
53 |
| -func Log(r *http.Request) *zap.Logger { |
54 |
| - v := r.Context().Value(CtxLoggerKey) |
55 |
| - log, ok := v.(*zap.Logger) |
56 |
| - if !ok { |
57 |
| - panic("cannot get derived request id logger from context object") |
58 |
| - } |
59 |
| - return log |
| 37 | + // Write the request log line |
| 38 | + z.writeZap(zw, r, startTime) |
60 | 39 | }
|
61 | 40 |
|
62 |
| -func writeZap(log *zap.Logger, r *http.Request, t time.Time, status int, size int) { |
63 |
| - elapsed := time.Now().Sub(t) |
| 41 | +func (z zapLogger) writeZap(zw *zapResponseWriter, r *http.Request, startTime time.Time) { |
| 42 | + elapsed := time.Now().Sub(startTime) |
64 | 43 | var protocol string
|
65 | 44 | if r.TLS == nil {
|
66 | 45 | protocol = "http"
|
67 | 46 | } else {
|
68 | 47 | protocol = "https"
|
69 | 48 | }
|
70 | 49 |
|
71 |
| - v := r.Context().Value(CtxLoggerKey) |
| 50 | + logger := z.mid.logger |
| 51 | + v := r.Context().Value(CTXKeyLogger) |
72 | 52 | if v != nil {
|
73 | 53 | var ok bool
|
74 |
| - log, ok = v.(*zap.Logger) |
| 54 | + logger, ok = v.(*zap.Logger) |
75 | 55 | if !ok {
|
76 | 56 | panic("cannot get derived request id logger from context object")
|
77 | 57 | }
|
78 | 58 | }
|
79 | 59 |
|
80 | 60 | // log all the fields
|
81 |
| - log.Info(fmt.Sprintf("%s request", protocol), |
82 |
| - zap.Int("status", status), |
| 61 | + logger.Info(fmt.Sprintf("%s request", protocol), |
| 62 | + zap.Int("status", zw.status), |
| 63 | + zap.Int("size", zw.size), |
| 64 | + zap.Bool("hijacked", zw.hijacked), |
83 | 65 | zap.String("method", r.Method),
|
84 | 66 | zap.String("uri", r.RequestURI),
|
85 | 67 | zap.Bool("tls", r.TLS != nil),
|
86 | 68 | zap.String("protocol", r.Proto),
|
87 | 69 | zap.String("host", r.Host),
|
88 | 70 | zap.String("remote_addr", r.RemoteAddr),
|
89 |
| - zap.Int("size", size), |
90 | 71 | zap.Duration("elapsed", elapsed),
|
91 | 72 | )
|
92 | 73 | }
|
93 |
| - |
94 |
| -func (z *zapResponseWriter) WriteHeader(code int) { |
95 |
| - z.status = code |
96 |
| - z.ResponseWriter.WriteHeader(code) |
97 |
| -} |
98 |
| - |
99 |
| -func (z *zapResponseWriter) Write(b []byte) (int, error) { |
100 |
| - size, err := z.ResponseWriter.Write(b) |
101 |
| - z.size += size |
102 |
| - return size, err |
103 |
| -} |
0 commit comments