mirror of
https://github.com/Luzifer/go_helpers.git
synced 2024-12-25 05:21:20 +00:00
Add HTTPLogHandler
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
4206589520
commit
998dbf0ac0
1 changed files with 35 additions and 0 deletions
35
http/logHandler.go
Normal file
35
http/logHandler.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Luzifer/go_helpers/accessLogger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HTTPLogHandler struct {
|
||||||
|
Handler http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHTTPLogHandler(h http.Handler) http.Handler {
|
||||||
|
return HTTPLogHandler{Handler: h}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l HTTPLogHandler) ServeHTTP(res http.ResponseWriter, r *http.Request) {
|
||||||
|
start := time.Now()
|
||||||
|
ares := accessLogger.New(res)
|
||||||
|
|
||||||
|
l.Handler.ServeHTTP(ares, r)
|
||||||
|
|
||||||
|
log.Printf("%s - \"%s %s\" %d %d \"%s\" \"%s\" %s",
|
||||||
|
r.RemoteAddr,
|
||||||
|
r.Method,
|
||||||
|
r.URL.Path,
|
||||||
|
ares.StatusCode,
|
||||||
|
ares.Size,
|
||||||
|
r.Header.Get("Referer"),
|
||||||
|
r.Header.Get("User-Agent"),
|
||||||
|
time.Since(start),
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in a new issue