-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from nrfta/feature/seng-804
chore: add slog http middleware
- Loading branch information
Showing
6 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
package log | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/go-chi/chi/middleware" | ||
) | ||
|
||
// NewSLogChiMiddleware is used to log http request information. It takes | ||
// a pointer to an slog.Logger to use. If `l` is nil, it uses the | ||
// default logger | ||
func NewSLogChiMiddleware(l *slog.Logger) func(http.Handler) http.Handler { | ||
if l == nil { | ||
l = slog.Default() | ||
} | ||
|
||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor) | ||
|
||
defer func(start time.Time) { | ||
l.LogAttrs( | ||
r.Context(), | ||
slog.LevelInfo, | ||
"HTTP Request Served", | ||
slog.String("proto", r.Proto), | ||
slog.String("path", r.URL.Path), | ||
slog.Duration("duration", time.Since(start)), | ||
slog.Int("status", ww.Status()), | ||
slog.Int("size", ww.BytesWritten()), | ||
slog.String("ip", r.RemoteAddr), | ||
) | ||
}(time.Now()) | ||
|
||
next.ServeHTTP(ww, r) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
package log_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
|
||
"github.com/nrfta/go-log" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Logger", func() { | ||
Describe("NewSlogHTTPMiddleware", func() { | ||
It("should log http request information", func() { | ||
var ( | ||
buf bytes.Buffer | ||
logger = slog.New(slog.NewJSONHandler(&buf, nil)) | ||
mw = log.NewSLogChiMiddleware(logger) | ||
res = "test" | ||
hf = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(res)) | ||
}) | ||
) | ||
|
||
ts := httptest.NewServer(mw(hf)) | ||
defer ts.Close() | ||
|
||
_, err := http.Get(ts.URL) | ||
Expect(err).To(Succeed()) | ||
|
||
type logOutput struct { | ||
Msg string `json:"msg"` | ||
Proto string `json:"proto"` | ||
Path string `json:"path"` | ||
Duration int `json:"duration"` | ||
Status int `json:"status"` | ||
Size int `json:"size"` | ||
IP string `json:"ip"` | ||
} | ||
|
||
var lo logOutput | ||
err = json.Unmarshal(buf.Bytes(), &lo) | ||
Expect(err).To(Succeed()) | ||
|
||
Expect(lo.Msg).To(Equal("HTTP Request Served")) | ||
Expect(lo.Proto).To(Equal("HTTP/1.1")) | ||
Expect(lo.Path).To(Equal("/")) | ||
Expect(lo.Duration).To(BeNumerically(">", 0)) | ||
Expect(lo.Status).To(Equal(200)) | ||
Expect(lo.Size).To(Equal(len(res))) | ||
Expect(strings.Split(lo.IP, ":")[0]).To(Equal("127.0.0.1")) | ||
}) | ||
}) | ||
}) |