mirror of
https://github.com/Luzifer/go_helpers.git
synced 2024-12-25 13:31: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:
parent
e7a1201f43
commit
171f74b174
1 changed files with 9 additions and 1 deletions
|
@ -3,18 +3,26 @@ package http
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Luzifer/go_helpers/v2/accessLogger"
|
"github.com/Luzifer/go_helpers/v2/accessLogger"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPLogHandler struct {
|
type HTTPLogHandler struct {
|
||||||
Handler http.Handler
|
Handler http.Handler
|
||||||
TrustedIPHeaders []string
|
TrustedIPHeaders []string
|
||||||
|
|
||||||
|
logger logrus.StdLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPLogHandler(h http.Handler) http.Handler {
|
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{
|
return HTTPLogHandler{
|
||||||
Handler: h,
|
Handler: h,
|
||||||
TrustedIPHeaders: []string{"X-Forwarded-For", "RemoteAddr", "X-Real-IP"},
|
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
|
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),
|
l.findIP(r),
|
||||||
r.Method,
|
r.Method,
|
||||||
path,
|
path,
|
||||||
|
|
Loading…
Reference in a new issue