mirror of
https://github.com/sorenisanerd/gotty.git
synced 2024-11-09 23:34:26 +00:00
Show RemoteAddr and HTTP status code in log
This commit is contained in:
parent
af1a80c962
commit
c88cf7a52d
@ -218,7 +218,7 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
conn, err := app.upgrader.Upgrade(w, r, nil)
|
conn, err := app.upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("Failed to upgrade connection")
|
log.Print("Failed to upgrade connection: " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,8 +269,9 @@ func (app *App) Exit() (firstCall bool) {
|
|||||||
|
|
||||||
func wrapLogger(handler http.Handler) http.Handler {
|
func wrapLogger(handler http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("%s %s", r.Method, r.URL.Path)
|
rw := &responseWrapper{w, 200}
|
||||||
handler.ServeHTTP(w, r)
|
handler.ServeHTTP(rw, r)
|
||||||
|
log.Printf("%s %d %s %s", r.RemoteAddr, rw.status, r.Method, r.URL.Path)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
23
app/http_logger.go
Normal file
23
app/http_logger.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type responseWrapper struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
status int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWrapper) WriteHeader(status int) {
|
||||||
|
w.status = status
|
||||||
|
w.ResponseWriter.WriteHeader(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||||
|
hj, _ := w.ResponseWriter.(http.Hijacker)
|
||||||
|
w.status = http.StatusSwitchingProtocols
|
||||||
|
return hj.Hijack()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user