Files
vctp2/server/middleware/logging.go
Nathan Coad b297b8293c
All checks were successful
continuous-integration/drone/push Build is passing
adjustments to reporting
2026-01-14 10:23:25 +11:00

41 lines
931 B
Go

package middleware
import (
"log/slog"
"net/http"
"time"
)
// LoggingMiddleware represents a logging middleware.
type LoggingMiddleware struct {
logger *slog.Logger
handler http.Handler
}
// NewLoggingMiddleware creates a new logging middleware with the given logger and handler.
func NewLoggingMiddleware(logger *slog.Logger, handler http.Handler) *LoggingMiddleware {
return &LoggingMiddleware{
logger: logger,
handler: handler,
}
}
// ServeHTTP logs the request and calls the next handler.
func (l *LoggingMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := time.Now()
l.handler.ServeHTTP(w, r)
query := r.URL.RawQuery
if query == "" {
query = "-"
}
l.logger.Debug(
"Request recieved",
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
slog.String("query", query),
slog.String("remote", r.RemoteAddr),
slog.Duration("duration", time.Since(start)),
)
}