-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.go
53 lines (44 loc) · 1.4 KB
/
server.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
package httplog
import (
"net/http"
"github.com/sirupsen/logrus"
)
// LoggingMiddlewareOpts are opts passed to LoggingMiddleware.
type LoggingMiddlewareOpts struct {
// UserAgent includes user agent in logs.
UserAgent bool
}
// LoggingMiddleware logs incoming requests and response status codes using logrus.
func LoggingMiddleware(next http.Handler, le *logrus.Entry, opts LoggingMiddlewareOpts) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Wrap the response writer to capture the status code
wrappedWriter := &statusCapturingResponseWriter{ResponseWriter: w}
// Call the next handler
next.ServeHTTP(wrappedWriter, r)
// Log the request and response status code
WithLoggerFields(le, r, wrappedWriter.statusCode).
Debug("handled request")
})
}
// WithLoggerFields builds the log fields for a request.
func WithLoggerFields(le *logrus.Entry, r *http.Request, status int) *logrus.Entry {
fields := logrus.Fields{
"method": r.Method,
"uri": r.RequestURI,
}
if userAgent := r.UserAgent(); userAgent != "" {
fields["user-agent"] = userAgent
}
if status != 0 {
fields["status"] = status
}
return le.WithFields(fields)
}
type statusCapturingResponseWriter struct {
http.ResponseWriter
statusCode int
}
func (w *statusCapturingResponseWriter) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
w.statusCode = statusCode
}