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:
parent
e7a1201f43
commit
171f74b174
1 changed files with 9 additions and 1 deletions
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue