/
logger.go
35 lines (29 loc) · 1019 Bytes
/
logger.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
package middleware
import (
"net/http"
"time"
"github.com/rs/zerolog/log"
"github.com/justinas/alice"
"github.com/rs/zerolog/hlog"
)
// NewLoggerHandlerFunc returns a http.Handler that adds log related middlewares.
func NewLoggerHandlerFunc() func(http.Handler) http.Handler {
c := alice.New()
c = c.Append(hlog.NewHandler(log.Logger))
// Install some provided extra handler to set some request's context fields.
// Thanks to that handler, all our logs will come with some pre-populated fields.
c = c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Info().
Str("method", r.Method).
Stringer("url", r.URL).
Int("status", status).
Int("size", size).
Dur("duration", duration).
Msg("HTTP request")
}))
c = c.Append(hlog.RemoteAddrHandler("ip"))
c = c.Append(hlog.UserAgentHandler("user_agent"))
c = c.Append(hlog.RefererHandler("referer"))
c = c.Append(hlog.RequestIDHandler("req_id", "Request-Id"))
return c.Then
}