-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogging.go
89 lines (72 loc) · 2.03 KB
/
logging.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
package main
import (
"context"
"net/http"
"time"
"github.com/google/uuid"
"go.uber.org/zap"
)
// logged is a middleware that logs some basic information about a response
// (url, status code, response time, etc.) and attaches a request-specific
// logger to the request's context.
//
// Panics will be automatically logged.
func logged(rootLogger *zap.Logger, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestId := uuid.New()
// Create our specialized logger and attach it to the request
logger := rootLogger.With(zap.Any("request-id", requestId))
r = r.WithContext(context.WithValue(r.Context(), loggerKey{}, logger))
defer func() {
if err := recover(); err != nil {
logger.Error(
"Handler panicked",
zap.Any("error", err),
zap.StackSkip("stack", 1),
)
}
}()
spy := spyWriter{inner: w, code: http.StatusOK}
start := time.Now()
handler.ServeHTTP(&spy, r)
duration := time.Since(start)
logger.Info(
"Served a request",
zap.Any("status-code", spy.code),
zap.Any("status-text", http.StatusText(spy.code)),
zap.Any("bytes-written", spy.bytesWritten),
zap.Any("url", r.URL),
zap.Any("method", r.Method),
zap.Any("duration", duration),
zap.Any("user-agent", r.UserAgent()),
zap.Any("remote-addr", r.RemoteAddr),
)
})
}
type loggerKey struct{}
// getLogger returns the logger specific to this request.
func getLogger(r *http.Request) *zap.Logger {
logger, ok := r.Context().Value(loggerKey{}).(*zap.Logger)
if !ok {
// fall back to the global logger
return zap.L()
}
return logger
}
type spyWriter struct {
inner http.ResponseWriter
bytesWritten int
code int
}
func (w *spyWriter) Header() http.Header {
return w.inner.Header()
}
func (w *spyWriter) Write(data []byte) (int, error) {
bytesWritten, err := w.inner.Write(data)
w.bytesWritten += bytesWritten
return bytesWritten, err
}
func (w *spyWriter) WriteHeader(statusCode int) {
w.code = statusCode
w.inner.WriteHeader(statusCode)
}