Converted logging middleware to use new logger

This commit is contained in:
2025-02-09 17:38:35 +11:00
parent b2ed646ffd
commit 49d9806bd6
4 changed files with 22 additions and 7 deletions

View File

@@ -1,9 +1,10 @@
package middleware
import (
"log"
"net/http"
"time"
"github.com/rs/zerolog"
)
// Wraps the http.ResponseWriter, adding a statusCode field
@@ -19,7 +20,7 @@ func (w *wrappedWriter) WriteHeader(statusCode int) {
}
// Middleware to add logs to console with details of the request
func Logging(next http.Handler) http.Handler {
func Logging(logger *zerolog.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
wrapped := &wrappedWriter{
@@ -27,6 +28,10 @@ func Logging(next http.Handler) http.Handler {
statusCode: http.StatusOK,
}
next.ServeHTTP(wrapped, r)
log.Println(wrapped.statusCode, r.Method, r.URL.Path, time.Since(start))
logger.Info().
Int("status", wrapped.statusCode).
Str("method", r.Method).
Str("resource", r.URL.Path).
Dur("time_elapsed", time.Since(start)).Msg("Served")
})
}