1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00
imgproxy/router/logging.go
2021-09-30 20:23:30 +06:00

56 lines
981 B
Go

package router
import (
"net/http"
"github.com/imgproxy/imgproxy/v3/ierrors"
log "github.com/sirupsen/logrus"
)
func LogRequest(reqID string, r *http.Request) {
path := r.RequestURI
log.WithFields(log.Fields{
"request_id": reqID,
"method": r.Method,
}).Infof("Started %s", path)
}
func LogResponse(reqID string, r *http.Request, status int, err *ierrors.Error, additional ...log.Fields) {
var level log.Level
switch {
case status >= 500:
level = log.ErrorLevel
case status >= 400:
level = log.WarnLevel
default:
level = log.InfoLevel
}
fields := log.Fields{
"request_id": reqID,
"method": r.Method,
"status": status,
}
if err != nil {
fields["error"] = err
if stack := err.FormatStack(); len(stack) > 0 {
fields["stack"] = stack
}
}
for _, f := range additional {
for k, v := range f {
fields[k] = v
}
}
log.WithFields(fields).Logf(
level,
"Completed in %s %s", ctxTime(r.Context()), r.RequestURI,
)
}