Files
vctp2/server/middleware/logging.go
Nathan Coad 5a00f4a8c7
Some checks are pending
CI / Lint (push) Waiting to run
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
continuous-integration/drone/push Build is passing
improve disk change detection
2024-09-27 13:58:33 +10:00

36 lines
840 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)
l.logger.Debug(
"Request recieved",
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
slog.String("remote", r.RemoteAddr),
slog.Duration("duration", time.Since(start)),
)
}