diff options
Diffstat (limited to 'logger.go')
-rw-r--r-- | logger.go | 33 |
1 files changed, 20 insertions, 13 deletions
@@ -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..., + ) + }) + } } |