From 88a6b1cb628e4e334068e861ac5fb56274d88845 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 11 Jun 2024 14:12:30 +0300 Subject: Initial --- middleware/logger.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 middleware/logger.go (limited to 'middleware/logger.go') 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..., + ) + }) + } +} -- cgit v1.2.3