clustvirt/router/middleware/logger.go

31 lines
801 B
Go
Raw Normal View History

package middleware
import (
"net/http"
"time"
"git.staur.ca/stobbsm/clustvirt/lib/log"
)
// Logger uses the in package log module to handle route logging
func Logger(next http.Handler) http.Handler {
log.Info("logger.Logger").Str("middleware", "Logger").Msg("middleware loaded")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer func() {
log.Info("router.middleware.Logger").
Str("httpMethod", r.Method).
Str("host", r.Host).
Str("uri", r.URL.RequestURI()).
Str("user_agent", r.UserAgent()).
Str("referer", r.Referer()).
Strs("transfer-encoding", r.TransferEncoding).
Int64("length", r.ContentLength).
Dur("elasped_ms", time.Since(start)).
Msg("incoming connection")
}()
next.ServeHTTP(w, r)
})
}