-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_middleware.go
102 lines (90 loc) · 2.91 KB
/
logging_middleware.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
package http
import (
stdhttp "net/http"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/diamcircle/go/support/http/mutil"
"github.com/diamcircle/go/support/log"
)
// Options allow the middleware logger to accept additional information.
type Options struct {
ExtraHeaders []string
}
// SetLogger is a middleware that sets a logger on the context.
func SetLoggerMiddleware(l *log.Entry) func(stdhttp.Handler) stdhttp.Handler {
return func(next stdhttp.Handler) stdhttp.Handler {
return stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
ctx := r.Context()
ctx = log.Set(ctx, l)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}
}
// LoggingMiddleware is a middleware that logs requests to the logger.
func LoggingMiddleware(next stdhttp.Handler) stdhttp.Handler {
return LoggingMiddlewareWithOptions(Options{})(next)
}
// LoggingMiddlewareWithOptions is a middleware that logs requests to the logger.
// Requires an Options struct to accept additional information.
func LoggingMiddlewareWithOptions(options Options) func(stdhttp.Handler) stdhttp.Handler {
return func(next stdhttp.Handler) stdhttp.Handler {
return stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
mw := mutil.WrapWriter(w)
ctx := log.PushContext(r.Context(), func(l *log.Entry) *log.Entry {
return l.WithFields(log.F{
"req": middleware.GetReqID(r.Context()),
})
})
r = r.WithContext(ctx)
logStartOfRequest(r, options.ExtraHeaders)
then := time.Now()
next.ServeHTTP(mw, r)
duration := time.Since(then)
logEndOfRequest(r, duration, mw)
})
}
}
// logStartOfRequest emits the logline that reports that an http request is
// beginning processing.
func logStartOfRequest(
r *stdhttp.Request,
extraHeaders []string,
) {
fields := log.F{}
for _, header := range extraHeaders {
// Strips "-" characters and lowercases new logrus.Fields keys to be uniform with the other keys in the logger.
// Simplifies querying extended fields.
var headerkey = strings.ToLower(strings.ReplaceAll(header, "-", ""))
fields[headerkey] = r.Header.Get(header)
}
fields["subsys"] = "http"
fields["path"] = r.URL.String()
fields["method"] = r.Method
fields["ip"] = r.RemoteAddr
fields["host"] = r.Host
fields["useragent"] = r.Header.Get("User-Agent")
l := log.Ctx(r.Context()).WithFields(fields)
l.Info("starting request")
}
// logEndOfRequest emits the logline for the end of the request
func logEndOfRequest(
r *stdhttp.Request,
duration time.Duration,
mw mutil.WriterProxy,
) {
l := log.Ctx(r.Context()).WithFields(log.F{
"subsys": "http",
"path": r.URL.String(),
"method": r.Method,
"status": mw.Status(),
"bytes": mw.BytesWritten(),
"duration": duration,
})
if routeContext := chi.RouteContext(r.Context()); routeContext != nil {
l = l.WithField("route", routeContext.RoutePattern())
}
l.Info("finished request")
}