-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
33 lines (26 loc) · 858 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
package middleware
import (
"github.com/djordjev/auth/internal/utils"
"github.com/go-chi/chi/v5/middleware"
"golang.org/x/exp/slog"
"net/http"
)
type Wrapper func(next http.Handler) http.Handler
func Logger(logger *slog.Logger) Wrapper {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
entry := logger.With("method", r.Method, "path", r.URL.Path)
defer func() {
status := ww.Status()
if status >= http.StatusInternalServerError {
entry.Error("request failed", "status", status, "size", ww.BytesWritten())
} else {
entry.Info("request completed", "status", status, "size", ww.BytesWritten())
}
}()
next.ServeHTTP(ww, utils.InjectLoggerIntoContext(r, entry))
}
return http.HandlerFunc(fn)
}
}