1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-12-24 13:01:21 +00:00

Allow to pass in a logger for HTTP logs

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-02-06 22:39:56 +01:00
parent e7a1201f43
commit 171f74b174
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

View file

@ -3,18 +3,26 @@ package http
import (
"log"
"net/http"
"os"
"strings"
"time"
"github.com/Luzifer/go_helpers/v2/accessLogger"
"github.com/sirupsen/logrus"
)
type HTTPLogHandler struct {
Handler http.Handler
TrustedIPHeaders []string
logger logrus.StdLogger
}
func NewHTTPLogHandler(h http.Handler) http.Handler {
return NewHTTPLogHandlerWithLogger(h, log.New(os.Stderr, "", log.LstdFlags))
}
func NewHTTPLogHandlerWithLogger(h http.Handler, l logrus.StdLogger) http.Handler {
return HTTPLogHandler{
Handler: h,
TrustedIPHeaders: []string{"X-Forwarded-For", "RemoteAddr", "X-Real-IP"},
@ -32,7 +40,7 @@ func (l HTTPLogHandler) ServeHTTP(res http.ResponseWriter, r *http.Request) {
path = path + "?" + q
}
log.Printf("%s - \"%s %s\" %d %d \"%s\" \"%s\" %s",
l.logger.Printf("%s - \"%s %s\" %d %d \"%s\" \"%s\" %s",
l.findIP(r),
r.Method,
path,