github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/server/httputils/logkit.go (about) 1 package httputils 2 3 import ( 4 "fmt" 5 "github.com/go-kit/log" 6 "github.com/go-kit/log/level" 7 "net/http" 8 ) 9 10 type logKitImpl struct { 11 l log.Logger 12 } 13 14 func NewLogKitErrorUtils(l log.Logger) ErrorUtils { 15 return &logKitImpl{ 16 l: level.Error(l), 17 } 18 } 19 20 func (i *logKitImpl) WriteInvalidMethodError(r *http.Request, w http.ResponseWriter) { 21 i.writeErrorMessage(r, w, http.StatusMethodNotAllowed, "method not allowed") 22 } 23 24 func (i *logKitImpl) WriteInvalidParameterError(r *http.Request, w http.ResponseWriter, err error) { 25 i.WriteError(r, w, http.StatusBadRequest, err, "invalid parameter") 26 } 27 28 func (i *logKitImpl) WriteInternalServerError(r *http.Request, w http.ResponseWriter, err error, msg string) { 29 i.WriteError(r, w, http.StatusInternalServerError, err, msg) 30 } 31 32 func (i *logKitImpl) WriteJSONEncodeError(r *http.Request, w http.ResponseWriter, err error) { 33 i.WriteInternalServerError(r, w, err, "encoding response body") 34 } 35 36 func (i *logKitImpl) WriteError(r *http.Request, w http.ResponseWriter, code int, err error, msg string) { 37 _ = i.l.Log( 38 "err", err, 39 "msg", msg, 40 ) 41 i.writeMessage(r, w, code, "%s: %q", msg, err) 42 } 43 44 func (i *logKitImpl) writeErrorMessage(r *http.Request, w http.ResponseWriter, code int, msg string) { 45 _ = i.l.Log(msg) 46 i.writeMessage(r, w, code, msg) 47 } 48 49 func (*logKitImpl) writeMessage(_ *http.Request, w http.ResponseWriter, code int, format string, args ...interface{}) { 50 w.WriteHeader(code) 51 _, _ = fmt.Fprintf(w, format, args...) 52 _, _ = fmt.Fprintln(w) 53 }