| 17 | } |
| 18 | |
| 19 | func logging(logger *slog.Logger, next http.Handler) http.Handler { |
| 20 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 21 | wrapped := &wrappedResponseWriter{ |
| 22 | ResponseWriter: w, |
| 23 | } |
| 24 | |
| 25 | start := time.Now() |
| 26 | next.ServeHTTP(wrapped, r) |
| 27 | duration := time.Since(start) |
| 28 | |
| 29 | logger.Info( |
| 30 | "handled request", |
| 31 | slog.String("method", r.Method), |
| 32 | slog.String("path", r.URL.Path), |
| 33 | slog.Int64("duration_ns", duration.Nanoseconds()), |
| 34 | slog.Int("status", wrapped.statusCode), |
| 35 | ) |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | // Logging middleware is used to write log information out to the console |
| 40 | // on each request/response. |