* Define and use an interface for logging. This allows alternative implementations to use whatever logging package they want. Currently the interface just mimicks what logrus already implements, with minor modifications to not rely on logrus itself. I think the interface is pretty solid in terms of logging implementations being able to do what they need to. * Make tracing interface to coalesce logging/tracing Allows us to share data between the tracer and the logger so we can simplify log/trace handling wher we generally want data to go both places.
32 lines
661 B
Go
32 lines
661 B
Go
package api
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/cpuguy83/strongerrors/status"
|
|
"github.com/virtual-kubelet/virtual-kubelet/log"
|
|
)
|
|
|
|
type handlerFunc func(http.ResponseWriter, *http.Request) error
|
|
|
|
func handleError(f handlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
err := f(w, req)
|
|
if err == nil {
|
|
return
|
|
}
|
|
|
|
code, _ := status.HTTPCode(err)
|
|
w.WriteHeader(code)
|
|
io.WriteString(w, err.Error())
|
|
logger := log.G(req.Context()).WithError(err).WithField("httpStatusCode", code)
|
|
|
|
if code >= 500 {
|
|
logger.Error("Internal server error on request")
|
|
} else {
|
|
logger.Debug("Error on request")
|
|
}
|
|
}
|
|
}
|