50 lines
766 B
Go
50 lines
766 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type StatusRecorder struct {
|
|
http.ResponseWriter
|
|
Status int
|
|
}
|
|
|
|
func (r *StatusRecorder) WriteHeader(status int) {
|
|
r.Status = status
|
|
r.ResponseWriter.WriteHeader(status)
|
|
}
|
|
|
|
func Logger(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
|
|
recorder := &StatusRecorder{
|
|
ResponseWriter: w,
|
|
Status: http.StatusOK,
|
|
}
|
|
|
|
next(recorder, r)
|
|
|
|
duration := time.Since(start)
|
|
|
|
icon := "✅"
|
|
if recorder.Status >= 400 {
|
|
icon = "⚠️"
|
|
}
|
|
if recorder.Status >= 500 {
|
|
icon = "🔥"
|
|
}
|
|
|
|
log.Printf("%s [%s] %s | %d | %v | %s",
|
|
icon,
|
|
r.Method,
|
|
r.URL.Path,
|
|
recorder.Status,
|
|
duration,
|
|
r.RemoteAddr,
|
|
)
|
|
}
|
|
}
|