-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
25 lines (20 loc) · 810 Bytes
/
middleware.go
File metadata and controls
25 lines (20 loc) · 810 Bytes
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
package main
import (
"math/rand"
"net/http"
"github.com/go-logrusutil/logrusutil/logctx"
)
func logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// add a random reqID field for each request
reqID := rand.Int()
logEntry := logctx.Default.WithField("reqID", reqID)
logEntry.Info("request started")
// Output: time="2019-11-11T20:47:29.5326335+01:00" level=info msg="request started" app=example reqID=5577006791947779410
// setting contextual log entry for the handler
ctx := logctx.New(r.Context(), logEntry)
next.ServeHTTP(w, r.WithContext(ctx))
logEntry.Info("request finished")
// Output: time="2019-11-11T20:47:29.5746316+01:00" level=info msg="request finished" app=example reqID=5577006791947779410
})
}