httpserver/logging.go

30 lines
607 B
Go
Raw Normal View History

2023-02-10 12:48:42 +01:00
package main
import (
"log"
"net/http"
"github.com/fatih/color"
)
var MethodColors = map[string]*color.Color{
"GET": color.New(color.FgBlue),
"POST": color.New(color.FgGreen),
"PUT": color.New(color.FgYellow),
"DELETE": color.New(color.FgRed),
}
func MethodToString(method string) string {
if str, ok := MethodColors[method]; ok {
return str.Sprint(method)
}
return method
}
func LogHandler(handler http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", MethodToString(r.Method), r.URL.String())
handler.ServeHTTP(w, r)
}
}