summaryrefslogtreecommitdiff
path: root/middleware/logger.go
diff options
context:
space:
mode:
authorAlexander <a.kiryukhin@vk.team>2024-06-11 14:12:30 +0300
committerAlexander <a.kiryukhin@vk.team>2024-06-11 14:12:30 +0300
commit88a6b1cb628e4e334068e861ac5fb56274d88845 (patch)
tree312f2a3a7182b6e2fc45e87ec71ddde9861c77f8 /middleware/logger.go
InitialHEADmaster
Diffstat (limited to 'middleware/logger.go')
-rw-r--r--middleware/logger.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/middleware/logger.go b/middleware/logger.go
new file mode 100644
index 0000000..039bd19
--- /dev/null
+++ b/middleware/logger.go
@@ -0,0 +1,29 @@
+package middleware
+
+import (
+ "net/http"
+
+ "log/slog"
+)
+
+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...,
+ )
+ })
+ }
+}