summaryrefslogtreecommitdiff
path: root/logger.go
diff options
context:
space:
mode:
Diffstat (limited to 'logger.go')
-rw-r--r--logger.go33
1 files changed, 20 insertions, 13 deletions
diff --git a/logger.go b/logger.go
index 6a8ce7a..039bd19 100644
--- a/logger.go
+++ b/logger.go
@@ -6,17 +6,24 @@ import (
"log/slog"
)
-func Logger(handler http.Handler, logger *slog.Logger) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- handler.ServeHTTP(w, r)
- requestID := GetRequestID(r)
- logger.InfoContext(
- r.Context(),
- "request",
- slog.String("proto", r.Proto),
- slog.String("method", r.Method),
- slog.String("request_uri", r.RequestURI),
- slog.String("request_id", requestID),
- )
- })
+func Logger(logger *slog.Logger) Middleware {
+ return func(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ next.ServeHTTP(w, r)
+ requestID := GetRequestID(r)
+ args := []any{
+ slog.String("proto", r.Proto),
+ slog.String("method", r.Method),
+ slog.String("request_uri", r.RequestURI),
+ }
+ if requestID != "" {
+ args = append(args, slog.String("request_id", requestID))
+ }
+ logger.InfoContext(
+ r.Context(),
+ "request",
+ args...,
+ )
+ })
+ }
}